示例#1
0
        public IEnumerable <Rectangle> GetAllRectangles(Bitmap img, Rectangle canny, Color color, int colorThreshold,
                                                        ContourAcceptance acceptance)
        {
            var imgFiltered = FilterImage(new Image <Bgr, byte>(img), new FilterParam(color, colorThreshold));

            var result = new List <Rectangle>();

            using (var contours = new VectorOfVectorOfPoint())
            {
                CvInvoke.FindContours(imgFiltered, contours, null, RetrType.List, ChainApproxMethod.LinkRuns);

                for (var i = 0; i < contours.Size; i++)
                {
                    var contour = contours[i];

                    if (!acceptance.ValideSize(contour.Size))
                    {
                        continue;
                    }

                    var area = CvInvoke.MinAreaRect(contour).MinAreaRect();

                    if (acceptance.ValideHeight(area.Height) && acceptance.ValideWidth(area.Width))
                    {
                        result.Add(new Rectangle(new Point(area.X, area.Y), new Size(area.Width, area.Height)));
                    }
                }
            }

            return(result);
        }
示例#2
0
        public Rectangle GetRectangle(Bitmap img, Rectangle canny, Color color, int colorThreshold, ContourAcceptance acceptance)
        {
            DebugWindow.SetImageStartFishingRaw((Bitmap)img.Clone());

            var imgFiltered = FilterImage(new Image <Bgr, byte>(img), new FilterParam(color, colorThreshold));

            DebugWindow.SetImageStartFishingFiltered(imgFiltered.ToBitmap());

            using (var contours = new VectorOfVectorOfPoint())
            {
                CvInvoke.FindContours(imgFiltered, contours, null, RetrType.List, ChainApproxMethod.LinkRuns);

                for (var i = 0; i < contours.Size; i++)
                {
                    var contour = contours[i];

                    if (!acceptance.ValideSize(contour.Size))
                    {
                        continue;
                    }

                    var area = CvInvoke.MinAreaRect(contour).MinAreaRect();

                    if (acceptance.ValideHeight(area.Height) && acceptance.ValideWidth(area.Width))
                    {
                        return(new Rectangle(new Point(area.X, area.Y), new Size(area.Width, area.Height)));
                    }
                }
            }

            return(Rectangle.Empty);
        }