/// <summary>
        /// 根据图片的纵横灰度情况截取图片
        /// 使用Aforge类库
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static Rectangle GetByGrayPoint(Bitmap image)
        {
            //灰度化
            Grayscale g = new Grayscale(0.2125, 0.7154, 0.0721);
            // apply the filter
            var grayImg = g.Apply(image);
            //二值化
            var threshold = new Threshold();

            threshold.ThresholdValue = 80;
            var thresholdImage = threshold.Apply(grayImg);

            //找出纵横灰度值
            HorizontalIntensityStatistics hs = new HorizontalIntensityStatistics(thresholdImage);
            VerticalIntensityStatistics   vs = new VerticalIntensityStatistics(thresholdImage);
            var vThreshold = (image.Height / 4) * 255;
            var hThreshold = (image.Width / 4) * 255;
            var hGrays     = hs.Gray.Values.ToList();
            var vGrays     = vs.Gray.Values.ToList();
            var hmin       = FindPaperBorder(hGrays, true, hThreshold, 0);
            var hmax       = FindPaperBorder(hGrays, false, hThreshold, 0);
            var vmin       = FindPaperBorder(vGrays, true, vThreshold, 0);;
            var vmax       = FindPaperBorder(vGrays, false, vThreshold, 0);

            return(new Rectangle(hmin, vmin, hmax - hmin, vmax - vmin));
        }
Example #2
0
        private void DoHistogram()
        {
            swHistogram.Reset();
            swHistogram.Start();
            Log("Building Histogram");

            AForge.Math.Histogram grayhist;
            AForge.Math.Histogram Redhist;
            AForge.Math.Histogram Greenhist;
            AForge.Math.Histogram Bluehist;
            // collect statistics
            HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(new Bitmap(pbImage.Image));

            // get gray histogram (for grayscale image)
            if (his.IsGrayscale)
            {
                grayhist = his.Gray;
            }
            else
            {
                Redhist   = his.Red;
                Greenhist = his.Green;
                Bluehist  = his.Blue;
                DoRGBHistogram(Redhist, Greenhist, Bluehist);
            }

            // output some histogram's information
            //Log("Histogram Mean = " + hist.Mean);
            //Log("Histogram Min = " + hist.Min);
            //Log("Histogram Max = " + hist.Max);

            swHistogram.Stop();
            Log("Histogram built in " + swHistogram.Elapsed);
        }
        public string decodeBitmap(Bitmap img)
        {
            /////////图像处理
            //转为灰度图
            Grayscale grayscaleFilter = new Grayscale(0.299, 0.587, 0.114);
            Bitmap    bitmapGrayscale = grayscaleFilter.Apply(img);
            //二值化
            Threshold thresholdFilter = new Threshold(128);
            Bitmap    bitmapThreshold = thresholdFilter.Apply(bitmapGrayscale);
            ////////中值滤波
            Median mediafil  = new Median();
            Bitmap medianBit = mediafil.Apply(bitmapThreshold);
            //锐化
            Sharpen sharpPic = new Sharpen();
            Bitmap  sharpOut = sharpPic.Apply(medianBit);
            //统计点数
            HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(sharpOut);
            Histogram histogram = his.Gray;

            int[] dato = histogram.Values;
            /////分割图像
            charCut(dato);
            shunfengdecoder decode   = new shunfengdecoder();
            string          backcode = decode.Decoder(sharpOut, div);

            return(backcode);
        }
Example #4
0
        public string decoder(Bitmap img)
        {
            //灰度
            Grayscale grayscaleFilter = new Grayscale(0.299, 0.587, 0.114);
            Bitmap    bitmapGrayscale = grayscaleFilter.Apply(img);
            //二值化
            Threshold thresholdFilter = new Threshold(128);
            Bitmap    bitmapThreshold = thresholdFilter.Apply(bitmapGrayscale);

            /////统计点数分布

            HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(bitmapThreshold);


            Histogram histogram = his.Gray;

            int[] dato = histogram.Values;

            /////分割验证码字符
            charCut(dato);
            ////////解码
            yuantongdecoder decode     = new yuantongdecoder();
            string          backNumber = decode.Decoder(bitmapThreshold, div);

            return(backNumber);
        }
Example #5
0
        public int decode()
        {
            initBitmap(img);
            //bmp.Save("that.png");
            HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(img);


            Histogram histogram = his.Blue;

            int[] dato = histogram.Values;
            charCut(dato);
            return(Decoder(img));
        }
Example #6
0
        private string decode(Bitmap img)
        {
            initBitmap(img);

            HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(img);


            Histogram histogram = his.Blue;

            int[] dato = histogram.Values;
            charCut(dato);
            return(Decoder(img));
        }
        /// <summary>
        /// Build the Histogram for Supplied Image
        /// </summary>
        /// <param name="image">
        /// String: Path to image that histogram is to be created out of
        /// </param>
        public void DoHistogram(string image)
        {
            CurrentImage = image;               // Used for re-generating the histogram
            bool    IsGrayScale         = AForge.Imaging.Image.IsGrayscale(new Bitmap(image));
            dynamic IntensityStatistics = null; // Use dynamic (a little like var) to assign this variable which maybe of different types.

            swHistogram.Reset();
            swHistogram.Start();
            histogramStatus("Creating Histogram");

            AForge.Math.Histogram grayhist;
            AForge.Math.Histogram Redhist;
            AForge.Math.Histogram Greenhist;
            AForge.Math.Histogram Bluehist;
            // collect statistics
            //NOTE: We have to use the braces on these statements see: http://stackoverflow.com/questions/2496589/variable-declarations-following-if-statements
            if (IsHorizontalIntensity)
            {
                histogramStatus("Using HorizontalIntensityStatistics");
                IntensityStatistics = new HorizontalIntensityStatistics(new Bitmap(image));
            }
            else
            {
                histogramStatus("Using VerticalIntensityStatistics");
                IntensityStatistics = new VerticalIntensityStatistics(new Bitmap(image));
            }

            // get gray histogram (for grayscale image)
            if (IsGrayScale)
            {
                grayhist = IntensityStatistics.Gray;
                //TODO: DoGrayHistogram();
                histogramStatus("Grayscale Histogram");
            }
            else
            {
                Redhist   = IntensityStatistics.Red;
                Greenhist = IntensityStatistics.Green;
                Bluehist  = IntensityStatistics.Blue;
                DoRGBHistogram(Redhist, Greenhist, Bluehist);
                histogramStatus("RGB Histogram");
            }

            swHistogram.Stop();
            histogramCompleted("Histogram built in " + swHistogram.Elapsed);
        }
Example #8
0
        private void SaveImage(Bitmap bmp)
        {
            seq++;
            var fname = $@"D:\tmp\images\{seq}.bmp";

            bmp.Save(fname);

            var s = new ImageStatistics(bmp);
            var v = new VerticalIntensityStatistics(bmp);
            var h = new HorizontalIntensityStatistics(bmp);

            File.AppendAllText(
                @"D:\tmp\images\stat.csv",
                $"{seq},"
                + $"{s.Gray.Mean},{s.Gray.StdDev},"
                + $"{v.Gray.Mean},{v.Gray.StdDev},"
                + $"{h.Gray.Mean},{h.Gray.StdDev},"
                + "\r\n");
        }
Example #9
0
        /// <summary>
        /// Gets the content.
        /// </summary>
        /// <param name="mincross">Минимальная разница яркости ячейки с кретсиком</param>
        /// <param name="maxcross">Максимальная разница яркости ячейки с кретсиком</param>
        public void GetContent(double mincross, double maxcross)
        {
            _content      = false;
            _neurocontent = CellContent.Free;

            List <double> _lrange           = new List <double>();
            BitmapData    data              = _parentimage.LockBits(_rect, ImageLockMode.ReadWrite, _parentimage.PixelFormat);
            VerticalIntensityStatistics vis = new VerticalIntensityStatistics(data);

            histogram = vis.Gray;


            HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(data);
            Histogram     hhistogram          = his.Gray;
            List <double> _hrange             = new List <double>();


            _parentimage.UnlockBits(data);

            for (int i = 8; i <= 15; i++)
            {
                _lrange.Add((histogram.Values[i] + hhistogram.Values[i]) / 2);
                // _hrange.Add(hhistogram.Values[i]);
            }
            // _britnessdispertion = (1 - RecogCore.Statistics.Mean(_lrange) / histogram.Values.Max()) + (1 - RecogCore.Statistics.Mean(_hrange) / hhistogram.Values.Max());
            _britnessdispertion = 1 - RecogCore.Statistics.Mean(_lrange) / histogram.Values.Max();
            if (_britnessdispertion <= mincross)
            {
                _neurocontent = CellContent.Free; _content = false;
            }
            if (_britnessdispertion > mincross & _britnessdispertion <= maxcross)
            {
                _neurocontent = CellContent.Cross; _content = true;
            }
            if (_britnessdispertion > maxcross)
            {
                _neurocontent = CellContent.Miss; _content = false;
            }
        }
Example #10
0
        // Draw a histogram.
        private void DrawHistogram(Bitmap img)
        {
            Color back_color = Color.White;

            Invert filter = new Invert();
            // apply the filter
            Bitmap inv = filter.Apply(img);

            HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(inv);

            AForge.Math.Histogram histogram = his.Gray;

            int[] values = histogram.Values;



            Color[] Colors = new Color[] {
                Color.Red, Color.LightGreen, Color.Blue,
                Color.Pink, Color.Green, Color.LightBlue,
                Color.Orange, Color.Yellow, Color.Purple
            };



            gr.Clear(back_color);

            // Draw the histogram.
            using (Pen thin_pen = new Pen(Color.Black, 0))
            {
                for (int i = 0; i < values.Length; i++)
                {
                    float f2 = 650F;
                    float fl = (float)values[i] / f2;
                    gr.DrawLine(thin_pen, i, 0, i, fl);
                }
            }
        }
Example #11
0
        /// <summary>
        /// Performs hand detection calculations
        /// </summary>
        /// <param name="bmp">The camera frame to be processed</param>
        /// <returns>A bitmap representation of the detected hands</returns>
        public static Bitmap Findhands(Bitmap bmp)
        {
            Bitmap   test = new Bitmap(bmp.Width, bmp.Height);
            Graphics g    = Graphics.FromImage(test);

            for (int j = 0; j < 2; j++)
            {
                Bitmap partial = bmp.Clone(new System.Drawing.Rectangle(j * bmp.Width / 2, 0, bmp.Width / 2, bmp.Height), bmp.PixelFormat);
                HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(partial);
                VerticalIntensityStatistics   vis = new VerticalIntensityStatistics(partial);
                Histogram gray  = his.Gray;
                Histogram GrayY = vis.Gray;

                double meanX;
                double meanY;

                int[] hisValues = (int[])his.Gray.Values.Clone();
                int[] visValues = (int[])vis.Gray.Values.Clone();

                List <int> lX = new List <int>();
                List <int> lY = new List <int>();

                for (int i = 0; i < hisValues.Length; i++)
                {
                    if (hisValues[i] != 0)
                    {
                        hisValues[i] = (hisValues[i] / hisValues.Max()) * 255;
                    }


                    if (hisValues[i] == 0)
                    {
                        lX.Add(i + (j * test.Width / 2));
                    }
                }
                for (int i = 0; i < visValues.Length; i++)
                {
                    if (visValues[i] != 0)
                    {
                        visValues[i] = (visValues[i] / visValues.Max()) * 255;
                    }


                    if (visValues[i] == 0)
                    {
                        lY.Add(i);
                    }
                }

                if (lX.Count != 0)
                {
                    meanX = lX.Average();
                }
                else
                {
                    meanX = 0;
                }
                if (lY.Count != 0)
                {
                    meanY = lY.Average();
                }
                else
                {
                    meanY = 0;
                }
                Rectangle r = new Rectangle((int)meanX, (int)meanY, 10, 10);
                g.DrawRectangle(new Pen(Color.Aqua), r);
            }
            return(test);
        }
Example #12
0
        /// <summary>
        /// Performs hand detection calculations within the given areas.
        /// </summary>
        /// <param name="bmp">The camera frame to be processed</param>
        /// <param name="areas">A list of rectangles in which the hand detection is to be calculated</param>
        /// <returns>A list of coordinates representing the location of each hand</returns>
        public static List <Point> GetHandLocations(Bitmap bmp, List <Rectangle> areas)
        {
            List <Point> hands = new List <Point>();

            foreach (Rectangle rect in areas)
            {
                Bitmap partial = bmp.Clone(rect, bmp.PixelFormat);
                HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(partial);
                VerticalIntensityStatistics   vis = new VerticalIntensityStatistics(partial);
                Histogram gray  = his.Gray;
                Histogram GrayY = vis.Gray;

                double meanX;
                double meanY;

                int[] hisValues = (int[])his.Gray.Values.Clone();
                int[] visValues = (int[])vis.Gray.Values.Clone();

                List <int> lX = new List <int>();
                List <int> lY = new List <int>();

                for (int i = 0; i < hisValues.Length; i++)
                {
                    if (hisValues[i] != 0)
                    {
                        hisValues[i] = (hisValues[i] / hisValues.Max()) * 255;
                    }
                    if (hisValues[i] == 0)
                    {
                        lX.Add(i + rect.X);
                    }
                }
                for (int i = 0; i < visValues.Length; i++)
                {
                    if (visValues[i] != 0)
                    {
                        visValues[i] = (visValues[i] / visValues.Max()) * 255;
                    }


                    if (visValues[i] == 0)
                    {
                        lY.Add(i + rect.Y);
                    }
                }

                if (lX.Count != 0)
                {
                    meanX = lX.Average();
                }
                else
                {
                    meanX = 0;
                }
                if (lY.Count != 0)
                {
                    meanY = lY.Average();
                }
                else
                {
                    meanY = 0;
                }
                hands.Add(new Point((int)meanX, (int)meanY));
            }
            return(hands);
        }
Example #13
0
        void SelectedCam_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap   bmp         = new Bitmap(100, 100);
            Graphics placeholder = Graphics.FromImage(bmp);

            placeholder.FillRectangle(Brushes.White, 0, 0, bmp.Width, bmp.Height);
            Bitmap image         = (Bitmap)eventArgs.Frame.Clone();
            Bitmap originalImage = (Bitmap)eventArgs.Frame.Clone();


            // set color ranges to keep red-orange
            filter.Hue        = new IntRange(0, 35);
            filter.Saturation = new Range(0.19f, 1);
            //filter.Luminance = new Range(0.31f, 255);
            // apply the filter
            filter.ApplyInPlace(image);
            openingFilter.ApplyInPlace(image);
            Bitmap greyimage = grayscaleFilter.Apply(image);

            thresholdFilter.ApplyInPlace(greyimage);
            erosionFilter.Apply(greyimage);



            // process binary image
            bc.ProcessImage(image);
            bc.MinWidth     = 50;
            bc.MinHeight    = 50;
            bc.FilterBlobs  = true;
            bc.ObjectsOrder = ObjectsOrder.Size;
            Rectangle[] rects = bc.GetObjectsRectangles();
            if (rects.Length > 0)
            {
                bmp = new Bitmap(rects[0].Width, rects[0].Height);


                Graphics g = Graphics.FromImage(bmp);
                g.DrawImage(greyimage, 0, 0, rects[0], GraphicsUnit.Pixel);

                g.Dispose();



                // process blobs
                foreach (Rectangle rect in rects)
                {
                    Graphics g1 = Graphics.FromImage(originalImage);


                    using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
                    {
                        g1.DrawRectangle(pen, rect);
                    }

                    g1.Dispose();
                }
            }

            Crop cropFilter = new Crop(new Rectangle(0, (int)(0.15 * bmp.Height), bmp.Width, (int)(0.25 * bmp.Height)));


            Bitmap handImage = cropFilter.Apply(bmp);
            Bitmap overlay   = new Bitmap(handImage.Width, handImage.Height);

            using (Graphics g = Graphics.FromImage(overlay))
            {
                g.FillRectangle(Brushes.White, 0, 0, overlay.Width, overlay.Height);
            }
            Subtract subfilter   = new Subtract(handImage);
            Bitmap   resultImage = subfilter.Apply(overlay);
            HorizontalIntensityStatistics his = new HorizontalIntensityStatistics(handImage);


            double palmlimit = 0.4 * handImage.Height;

            int[] hisValues = (int[])his.Red.Values.Clone();
            for (int i = 0; i < handImage.Width; i++)
            {
                hisValues[i] = ((double)hisValues[i] / 255 > palmlimit) ? 1 : 0;
            }


            histogram.Values  = his.Red.Values;
            histogram1.Values = hisValues;
            //display number of fingers

            Graphics num  = Graphics.FromImage(image);
            string   text = "0";

            if (bmp.Height > 1.5 * bmp.Width)
            {
                text = string.Format(countfingers(hisValues));
            }
            else
            {
                text = "0";
            }
            Font       drawFont  = new Font("Courier", 18, FontStyle.Bold);
            SolidBrush drawBrush = new SolidBrush(Color.White);

            num.DrawString(text, drawFont, drawBrush, new PointF(0, 5));

            drawFont.Dispose();
            drawBrush.Dispose();

            num.Dispose();

            // display in picture box
            pictureBox1.Image = originalImage;
            original.Image    = image;
            crop.Image        = handImage;
        }