gpt4 book ai didi

Android OpenCV 检索交点

转载 作者:行者123 更新时间:2023-11-30 17:15:02 25 4
gpt4 key购买 nike

我正在尝试将以下 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);
}
}
}

最佳答案

因此,您有两条线段(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);
}

关于Android OpenCV 检索交点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30072854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com