c++ - How to filter only the longest line after Hough Transform -
i'm using hough transform straight lines. there lot of lines detected. can know how filter , longest line output?
houghlinesp(dst, lines, 1, cv_pi/180, 50, 20, 10 ); //left lane for( size_t = 0; < lines.size(); i++ ) { vec4i l = lines[i]; double theta1,theta2, hyp, result; theta1 = (l[3]-l[1]); theta2 = (l[2]-l[0]); hyp = hypot(theta1,theta2); line( cdst, point(l[0], l[1]), point(l[2], l[3]), scalar(255,0,0), 3, cv_aa); } imshow("detected lines", cdst);
}
as far can see, you're literally step away:
the hypot
function gives distance between start , end points. now, find longest such distance, , corresponding line longest.
vec4i max_l; double max_dist = -1.0; for( size_t = 0; < lines.size(); i++ ) { vec4i l = lines[i]; double theta1,theta2, hyp, result; theta1 = (l[3]-l[1]); theta2 = (l[2]-l[0]); hyp = hypot(theta1,theta2); if (max_dist < hyp) { max_l = l; max_dist = hyp; } } // max_l has line of maximum length line( cdst, point(max_l[0], max_l[1]), point(max_l[2], max_l[3]), scalar(255,0,0), 3, cv_aa); // else max_l