# cv::fitline for C++

Hello, I try the fitline with this coding as I understand that the Input vector of 2D or 3D points, stored in std::vector<> or Mat. The contours is already in vector 2D form yet I still get the error.

Unhandled exception at 0x00007ff93cada1c8 in momentcontour.exe: Microsoft C++ exception: cv::Exception at memory location 0x00f9d7b0..

Any point that I'm missing?

```
vector< vector<Point> > contours;
vector<Moments> mu(contours.size() );
for( int i = 0; i < contours.size(); i++ )
{ mu[i] = moments( contours[i], false );
TOTALM00 = TOTALM00 + mu[i].m00;
}
Vec4f line;
cv::fitLine(contours,line,CV_DIST_L2,0,0.01,0.01);
//HU Moments
```

just guessing, i: it takes a

singlecontour, not all of them, so`fitLine(contours[i],...`

Maybe there are no contours... try

`if (!contours.empty())`

I had tested and seems like it will need only one contour. But anyway, how to draw a line from Vec4f line? The example in the documentation are using point

void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)

"Output line parameters. In case of 2D fitting, it should be a vector of 4 elements (like Vec4f) - (vx, vy, x0, y0), where (vx, vy) is a normalized vector collinear to the line and (x0, y0) is a point on the line. In case of 3D fitting, it should be a vector of 6 elements (like Vec6f) - (vx, vy, vz, x0, y0, z0), where (vx, vy, vz) is a normalized vector collinear to the line and (x0, y0, z0) is a point on the line" docs

@thdrksdfthmn, I had put the variable (vx, vy, x0, y0) into the line draw statement but failed to initialized. I write the Vec4f line into the console and get this result Line [0.00322647, -0.999995, 93.5153, 189.507]

I knew that I must have something that is not right but I don't know how to put directly the values of Vec4f into the line function. So I put manually like this to see the result. Is there any faster way by not putting it manually?

line( image, Point(0.00322647,-0.999995), Point(93.5153,189.507), Scalar( 110, 220, 0 ), 2, 8 );

IMHO the orientation of the line is the orientation of the vector (0.00322647, -0.999995) (with approximations). For obtaining the angle of the line:

`double angle = std::atan(vy/vx)`

And now you have a point on the line (x = 93.5153, y = 189.507) and the angle, so you can draw the line. It is strange that the fitline gives you these infos and there is no implementation of line for that case...@thdrksdfthmn , I got your point now, I cross checked with phyton tutorial and seems like it draw using the information given. But curious on how the implemetntion in c++

You could do the same thing in C++, you do not need the atan and what I said before :)