Android OpenCV检索交叉点

时间:2015-05-06 09:33:01

标签: android c opencv

我试图将以下OpenCV代码修改为Java。

    std::vector<cv::Vec4i> lines;  
    cv::HoughLinesP(bw,lines,1,CV_PI/180,70,30,10);   

    for (unsigned int i = 0;i<lines.size();i++)  
    {  
       cv::Vec4i v = lines[i];  
       lines[i][0] = 0;  
       lines[i][1] = ((float)v[1] - v[3])/(v[0] - v[2])* -v[0] + v[1];  
       lines[i][2] = src.cols;  
       lines[i][3] = ((float)v[1] - v[3])/(v[0] - v[2])*(src.cols - v[2]) + v[3];    
    }  

   std::vector<cv::Point2f> corners;
   for (unsigned int i = 0;i<lines.size();i++)  
   {  
       for (unsigned int j=i+1;j<lines.size();j++)  
       {  
           cv::Point2f pt = computeIntersect(lines[i],lines[j]);  
           if (pt.x >= 0 && pt.y >=0)  
           {  
               corners.push_back(pt);  
           }  
       }  
   }  

1 个答案:

答案 0 :(得分:0)

所以你有两个线段(lines[i]lines[j])由它们的终点给出,你有兴趣估计它们的交集(函数computeIntersect()):

computeIntersect(lines[i], lines[j]);

通过求解两个简单的方程给出了线段相交的点。您应该阅读description here了解更多详情。在这里你对这个问题有一些好的SO answer

/**
 * Computes the intersection between two lines. The calculated point is approximate, 
 * since integers are used. If you need a more precise result, use doubles
 * everywhere. 
 * (c) 2007 Alexander Hristov. Use Freely (LGPL license). http://www.ahristov.com
 *
 * @param x1 Point 1 of Line 1
 * @param y1 Point 1 of Line 1
 * @param x2 Point 2 of Line 1
 * @param y2 Point 2 of Line 1
 * @param x3 Point 1 of Line 2
 * @param y3 Point 1 of Line 2
 * @param x4 Point 2 of Line 2
 * @param y4 Point 2 of Line 2
 * @return Point where the segments intersect, or null if they don't
 */
public Point intersection(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
    int d = (x1-x2)*(y3-y4) - (y1-y2)*(x3-x4);
    if (d == 0) return null;

    int xi = ((x3-x4)*(x1*y2-y1*x2)-(x1-x2)*(x3*y4-y3*x4))/d;
    int yi = ((y3-y4)*(x1*y2-y1*x2)-(y1-y2)*(x3*y4-y3*x4))/d;

    return new Point(xi,yi);
}