1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
void fitLineRansac(const std::vector<cv::Point2f>& points, cv::Vec4f &line, int iterations = 1000, double sigma = 1., double k_min = -7., double k_max = 7.) { unsigned int n = points.size();
if(n<2) { return; }
cv::RNG rng; double bestScore = -1.; for(int k=0; k<iterations; k++) { int i1=0, i2=0; while(i1==i2) { i1 = rng(n); i2 = rng(n); } const cv::Point2f& p1 = points[i1]; const cv::Point2f& p2 = points[i2];
cv::Point2f dp = p2-p1; dp *= 1./norm(dp); double score = 0;
if(dp.y/dp.x<=k_max && dp.y/dp.x>=k_min ) { for(uint i=0; i<n; i++) { cv::Point2f v = points[i]-p1; double d = v.y*dp.x - v.x*dp.y; if( fabs(d)<sigma ) score += 1; } } if(score > bestScore) { line = cv::Vec4f(dp.x, dp.y, p1.x, p1.y); bestScore = score; } } }
|