Ejemplo n.º 1
0
        // transform image to binary image
        // and then iterate through area to get the coordinates
        // of detected object
        private Image <Gray, Byte> bwareaopen(Image <Gray, Byte> Input_Image, int threshold)
        {
            listOfDetectedCoor.Clear();
            Image <Gray, Byte> bwresults = Input_Image.Copy();

            using (MemStorage storage = new MemStorage())
            {
                for (Contour <Point> contours = Input_Image.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_LIST, storage); contours != null; contours = contours.HNext)
                {
                    Contour <Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, storage);
                    if (currentContour.Area < threshold)
                    {
                        for (int i = currentContour.BoundingRectangle.X; i < currentContour.BoundingRectangle.X + currentContour.BoundingRectangle.Width; i++)
                        {
                            for (int j = currentContour.BoundingRectangle.Y; j < currentContour.BoundingRectangle.Y + currentContour.BoundingRectangle.Height; j++)
                            {
                                bwresults.Data[j, i, 0] = 0;
                            }
                        }
                    }
                    else if (currentContour.Area > threshold)
                    {
                        int x = currentContour.BoundingRectangle.X;
                        int y = currentContour.BoundingRectangle.Y;

                        int width  = currentContour.BoundingRectangle.Width;
                        int height = currentContour.BoundingRectangle.Height;

                        RectangleData recData = new RectangleData(x, y, width, height);

                        int x_center = recData.getXCenter();
                        int y_center = recData.getYCenter();

                        try
                        {
                            listOfDetectedCoor.Add(recData);
                        }
                        catch (Exception e)
                        {
                            Console.Write(e.StackTrace);
                        }
                    }
                }
            }
            return(bwresults);
        }
Ejemplo n.º 2
0
        void searchByHSV(Image <Bgr, Byte> input)
        {
            using (Image <Hsv, Byte> imgHSV = input.Convert <Hsv, Byte>())
            {
                Image <Gray, Byte>[] channels = imgHSV.Split();

                try
                {
                    //channels[0] is the mask for hue less than 20 or larger than 160
                    CvInvoke.cvInRangeS(channels[0], new MCvScalar(20), new MCvScalar(160), channels[0]);
                    channels[0]._Not();

                    //channels[1] is the mask for satuation of at least 10, this is mainly used to filter out white pixels
                    channels[1]._ThresholdBinary(new Gray(10), new Gray(255.0));

                    CvInvoke.cvAnd(channels[0], channels[1], channels[0], IntPtr.Zero);
                }
                finally
                {
                    channels[1].Dispose();
                    channels[2].Dispose();
                }

                Image <Gray, Byte> smoothedRedMask = channels[0];

                //Use Dilate followed by Erode to eliminate small gaps in some countour.
                smoothedRedMask._Dilate(1);
                smoothedRedMask._Erode(1);

                using (Image <Gray, Byte> canny = smoothedRedMask.Canny(new Gray(100), new Gray(50)))
                    using (MemStorage stor = new MemStorage())
                    {
                        for (Contour <Point> contours = canny.FindContours(
                                 Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE,
                                 Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, stor);
                             contours != null;
                             contours = contours.HNext)
                        {
                            Contour <Point> currentContour = contours.ApproxPoly(contours.Perimeter * 0.05, stor);
                            int             x = currentContour.BoundingRectangle.X;
                            int             y = currentContour.BoundingRectangle.Y;

                            int width  = currentContour.BoundingRectangle.Width;
                            int height = currentContour.BoundingRectangle.Height;

                            RectangleData recData = new RectangleData(x, y, width, height);

                            int x_center = recData.getXCenter();
                            int y_center = recData.getYCenter();

                            try
                            {
                                listOfDetectedCoor.Add(recData);
                            }
                            catch (Exception e)
                            {
                                Console.Write(e.StackTrace);
                            }
                        }
                        CamImageBox.Image = canny;
                    }
            }
        }