0
fitLine(points_vec, vec, CV_DIST_L1, 0, 0.01, 0.01);
line( img, Point(vec[2],vec[3]), Point(vec[2]+vec[0]*50,vec[3]+vec[1]*50),1);

Even though I have long lines in the images I am using for some reason fitLine recgonizes lines between points that are not on the same line. This is odd since points_vec includes many points that are on the same line

I tried changing the distance function,reps and aep but the line parameters that are returned define lines that are not real lines in the image. points_vec has ~600 points. Any suggestions?

user3872358
  • 105
  • 2
  • 10
  • 1
    fitLine isnt line detection but line fitting! So it assumes that (mostly) all the points you provide to the function lie on a single line. if there are a lot of outlier you should better use more robust techniques like RANSAC. – Micka Jan 25 '15 at 18:59
  • 1
    fitline uses ransac with variable CV_DIST_L1 according to http://stackoverflow.com/questions/11722569/opencv-line-fitting-algorithm – user3872358 Jan 26 '15 at 09:04
  • it is "some sort of RANSAC" if the post is right... it only chooses 20 iterations... How many points do you provide and how many of those points belong to a single line (approx)? – Micka Jan 26 '15 at 09:14
  • according to your linked posting there are 10 random points chosen and standard least squares fitting is done with them. It's quite likely that most of the 10 points don't belong to a single line, so the fitting won't work there. – Micka Jan 26 '15 at 09:34
  • I would suggest to write a simple RANSAC version yourself: 1. choose 2 points randomly and build a line from them. 2. Compute the distance of each sample point to the line and decide whether it is an inlier or an outlier. 3. count inlier. 4. repeat the whole process until you are sure that you've tried enough combinations, then 5. choose again the line with the most inlier and do a `cv::fitLine` with all the inlier! It really is simple to implement, but a bit harder to optimize if you need faster performance. – Micka Jan 26 '15 at 09:34
  • sorry,made a mistake I have about 650 points and each line contains about 110 points – user3872358 Jan 26 '15 at 10:03
  • should work the same way for real ransac and gives the same problems for `cv::fitLine` since in each iteration there are 10 random samples and only approx 2 of them belong to a single line. – Micka Jan 26 '15 at 10:13
  • maybe you can use or adapt this answer that is hidden in a question ^^ http://stackoverflow.com/questions/15840629/ransac-linear-regression-in-2d-robust-line-fit – Micka Jan 26 '15 at 10:15

0 Answers0