Configure Line (OpenCV)

I use OpenCV to set a line from a set of points using cvFitLine()

cvFitLine() returns a normalized vector that is linear along the line and point on the line. More here

Using this information, how can I get a line equation to draw a line?

+7
math geometry opencv
source share
4 answers

If cvFitLine() returns the normalized vector (vx,vy) and point (x0,y0) , then the equation of the line

(x, y) = (x0, y0) + t * (vx, vy)

where t runs from & minus; & infin; to + & infin ;.

This is what you asked for, but probably doesn’t immediately help in drawing the line. You would like to copy it either to the borders of the screen, or, possibly, to the bounding box of the original set of points. To copy a line into a rectangle, just solve for the t values ​​where the line intersects the border of the rectangle.

+7
source share

Just draw a large line instead of a border solution. eg:

 cv.Line(img, (x0-m*vx[0], y0-m*vy[0]), (x0+m*vx[0], y0+m*vy[0]), (0,0,0)) 

will do this, for example .. for m big enough :)

+9
source share

I used a strategy similar to Karpathy, but used an extra feature. As you can see, I use cvClipLine to crop the line to the size of the image, which is not necessary, but adds a bit of pleasantness.

Also, the factor here is defined as Mult = max (img-> height, img-> width), so we do not get numbers that can one day overflow or something else.

 void drawLine(IplImage * img, float line[4], int thickness,CvScalar color) { double theMult = max(img->height,img->width); // calculate start point CvPoint startPoint; startPoint.x = line[2]- theMult*line[0];// x0 startPoint.y = line[3] - theMult*line[1];// y0 // calculate end point CvPoint endPoint; endPoint.x = line[2]+ theMult*line[0];//x[1] endPoint.y = line[3] + theMult*line[1];//y[1] // draw overlay of bottom lines on image cvClipLine(cvGetSize(img), &startPoint, &endPoint); cvLine(img, startPoint, endPoint, color, thickness, 8, 0); } 
+4
source share

we use "Vec4f fitedLine"; for the equipped line in fitLine we have 4 parameters if we consider the linear relation az below: Y - Y0 = M (X - X0)

we have Y0 = FitedLine [3]; X0 = FitedLine [2]; m = FitedLine [1] / FitedLine [0];

therefore, we have the equation of the Line, you can find other points on it.

0
source share

All Articles