Exemple #1
0
        public FitLine()
        {
            CvSize imageSize = new CvSize(500, 500);

            // cvFitLine
            CvPoint2D32f[] points = GetRandomPoints(20, imageSize);
            CvLine2D       line   = Cv.FitLine2D(points, DistanceType.L2, 0, 0.01, 0.01);

            using (IplImage img = new IplImage(imageSize, BitDepth.U8, 3))
            {
                img.Zero();

                // draw line
                {
                    CvPoint pt1, pt2;
                    line.FitSize(img.Width, img.Height, out pt1, out pt2);
                    img.Line(pt1, pt2, CvColor.Green, 1, LineType.Link8);
                }

                // draw points and distances
                using (CvFont font = new CvFont(FontFace.HersheySimplex, 0.33, 0.33))
                {
                    foreach (CvPoint2D32f p in points)
                    {
                        double d = line.Distance(p);

                        img.Circle(p, 2, CvColor.White, -1, LineType.AntiAlias);
                        img.PutText(string.Format("{0:F1}", d), new CvPoint((int)(p.X + 3), (int)(p.Y + 3)), font, CvColor.Green);
                    }
                }

                CvWindow.ShowImages(img);
            }
        }
Exemple #2
0
        private void extendLines(CvLineSegmentPoint[] lines, double ext)
        {
            // TODO : this is stupid way to extend a line, does 2 sqrts and is generally non-comprehensible
            // Better just make it parametric, like x = x0 + t * x1, y = y0 + t * y1 where for t=0, {x,y} = P1 & for t=1, {x,y} = P2
            // so we can increase in size by percent, like t = +-0.5 (+50%)

            //Transfer 2-point line segments to type "a*x=b" CvLine2D format
            //TODO : this should probably be redone manually without OpenCV's ultra-generic slow function. We only have 2 points after all.
            List <CvLine2D> fitLinesV = new List <CvLine2D>(lines.Length);

            CvPoint[] forFitline = new CvPoint[2];
            for (int i = 0; i < lines.Length; ++i)
            {
                forFitline[0] = lines[i].P1;
                forFitline[1] = lines[i].P2;
                CvLine2D fitLinef = Cv.FitLine2D(forFitline, DistanceType.L2, 0, 0.01, 0.01);
                fitLinesV.Add(fitLinef);
            }

            CvPoint p1, p2, p3, p4;

            for (int i = 0; i < lines.Length; i++)
            {
                CvLineSegmentPoint lineSegm = lines[i];
                CvLine2D           fitLine  = fitLinesV[i];
                int fitLineVx = (int)(fitLine.Vx * ext);
                int fitLineVy = (int)(fitLine.Vy * ext);
                p1 = new CvPoint(lineSegm.P1.X + fitLineVx, lineSegm.P1.Y + fitLineVy);
                p2 = new CvPoint(lineSegm.P2.X - fitLineVx, lineSegm.P2.Y - fitLineVy);
                p3 = new CvPoint(lineSegm.P1.X - fitLineVx, lineSegm.P1.Y - fitLineVy);
                p4 = new CvPoint(lineSegm.P2.X + fitLineVx, lineSegm.P2.Y + fitLineVy);
                if (p1.DistanceTo(p2) > p3.DistanceTo(p4))
                {
                    lineSegm.P1.X = p1.X;
                    lineSegm.P1.Y = p1.Y;
                    lineSegm.P2.X = p2.X;
                    lineSegm.P2.Y = p2.Y;
                }
                else
                {
                    lineSegm.P1.X = p3.X;
                    lineSegm.P1.Y = p3.Y;
                    lineSegm.P2.X = p4.X;
                    lineSegm.P2.Y = p4.Y;
                }
            }
        }