cv::fitline for C++

asked 2015-09-22 04:02:26 -0500

zms gravatar image

updated 2015-09-22 04:11:05 -0500

berak gravatar image

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
edit retag flag offensive close merge delete

Comments

2

just guessing, i: it takes a single contour, not all of them, so fitLine(contours[i],...

berak gravatar imageberak ( 2015-09-22 04:15:41 -0500 )edit
2

Maybe there are no contours... try if (!contours.empty())

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-09-22 04:16:03 -0500 )edit

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)

zms gravatar imagezms ( 2015-09-28 03:46:21 -0500 )edit

"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 gravatar imagethdrksdfthmn ( 2015-09-28 09:19:34 -0500 )edit
1

@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 );

zms gravatar imagezms ( 2015-09-28 23:42:51 -0500 )edit

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 gravatar imagethdrksdfthmn ( 2015-09-29 02:34:35 -0500 )edit

@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++

zms gravatar imagezms ( 2015-09-30 03:23:20 -0500 )edit

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

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-09-30 03:50:48 -0500 )edit