Exemple #1
0
        /// <summary>
        /// Returns a List of Shapes found in the gray Image and returns Linesegments and color of shape
        /// </summary>
        /// <param name="inImg">Gray Image</param>
        /// <param name="refImg">Color Image for shapecolor</param>
        /// <param name="lines">outputarray for lines</param>
        /// <param name="colors">outputarry for linecolors</param>
        /// <returns></returns>
        public List <ShapeColorObject> findShapesinGrayImg(Emgu.CV.Image <Gray, Byte> inImg, Emgu.CV.Image <Hsv, Byte> refImg, int index)
        {
            if (inImg == null || refImg == null)
            {
                return(null);
            }

            List <ShapeColorObject> funkshapes = new List <ShapeColorObject>();

            for (Contour <Point> contours = inImg.FindContours(); contours != null; contours = contours.HNext)
            {
                Contour <Point> current = contours.ApproxPoly(contours.Perimeter * 0.008);


                if (current.Area > 200)
                {
                    Point[] points = current.ToArray();

                    int meanX = 0;
                    int meanY = 0;

                    foreach (Point p in points)
                    {
                        meanX += p.X;
                        meanY += p.Y;
                    }

                    meanX /= current.Total;
                    meanY /= current.Total;
                    ShapeColorObject newObj;
                    Hsv col;
                    if (current.Total == 3) //3 vertices
                    {
                        col            = new Hsv(0, 255, 220);
                        newObj         = new ShapeColorObject(current.Area, ShapeColorObject.shape.tri, refImg[meanY, meanX], meanX, meanY);
                        newObj.imIndex = index;
                        funkshapes.Add(newObj);
                    }
                    else if (current.Total == 4 && current.Convex) //4 vertices convex
                    {
                        col            = new Hsv(45, 255, 220);
                        newObj         = new ShapeColorObject(current.Area, ShapeColorObject.shape.rect, refImg[meanY, meanX], meanX, meanY);
                        newObj.imIndex = index;
                        funkshapes.Add(newObj);
                    }
                    else if (current.Total > 8 && current.Convex) //8+ vertices (circle) convex
                    {
                        col            = new Hsv(90, 255, 220);
                        newObj         = new ShapeColorObject(current.Area, ShapeColorObject.shape.circle, refImg[meanY, meanX], meanX, meanY);
                        newObj.imIndex = index;
                        funkshapes.Add(newObj);
                    }
                    else //other shapes
                    {
                        col            = new Hsv(135, 255, 220);
                        newObj         = new ShapeColorObject(current.Area, ShapeColorObject.shape.undef, refImg[meanY, meanX], meanX, meanY);
                        newObj.imIndex = index;
                        funkshapes.Add(newObj);
                    }

                    for (int i = 0; i < current.Total - 1; i++)
                    {
                        newObj.lineSegments.Add(new LineSegment2D(points[i], points[i + 1]));  //add contour line segments to new ShapeColorObject
                    }

                    newObj.lineSegments.Add(new LineSegment2D(points[current.Total - 1], points[0]));

                    double cirumf = 0;
                    foreach (LineSegment2D s in newObj.lineSegments)
                    {
                        cirumf += s.Length;
                    }


                    newObj.circularity = (float)(cirumf * cirumf / current.Area);
                } //ende if(200>area)
            }     //ende von for(contours....)

            return(funkshapes);
        }
        public int apply(string fileName, string output)
        {
            int counter = 0;

            Emgu.CV.Image<Bgr, Byte> imgS = new Emgu.CV.Image<Bgr, Byte>(fileName);

            Emgu.CV.Image<Gray, Byte> img = new Emgu.CV.Image<Gray, Byte>(fileName);

            //Emgu.CV.Image<Gray, Byte> imgGray = new Image<Gray, byte>(img.Width, img.Height);
            //CvInvoke.cvCvtColor(img, imgGray, COLOR_CONVERSION.BGR2GRAY);

            int thresh = 1;
            int max_thresh = 255;
            img = img.ThresholdBinary(new Gray(thresh), new Gray(max_thresh));

            img.Save(output.Replace(".", "_binary."));

            Contour<Point> contur = img.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP);
            Emgu.CV.CvInvoke.cvDrawContours(imgS, contur, new MCvScalar(0, 0, 255), new MCvScalar(0, 0, 255), 1, 1, LINE_TYPE.EIGHT_CONNECTED, new Point(0, 0));

            contur = img.FindContours(Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_CCOMP);

            while (contur != null && contur.HNext != null)
            {
                if (counter == 0) { counter++; }

                contur = contur.HNext;
                counter++;
            }

            MCvFont font = new MCvFont(Emgu.CV.CvEnum.FONT.CV_FONT_HERSHEY_SIMPLEX, 0.8f, 0.8f);
            MCvScalar color = new MCvScalar(255, 255, 255);

            CvInvoke.cvPutText(imgS, "counter:" + counter, new Point(10, 20), ref font, color);

            imgS.Save(output);

            return counter;
        }