提醒
HoughLines输出线:

HoughLinesP输出线:

问题
如何将HoughLinesP行格式转换为HoughLines行格式?
Details我已经建立了一个基于HoughLines输出行的算法,所以我真的不需要段结束点信息。
发布于 2016-11-17 00:39:01
这是简单的几何学。你需要从两点(x1, y1), (x2, y2)定义的一条线传递到极坐标方程(rho, theta)。
您可以从两点rho (其中(x0, y0) = (0,0))找到计算(x0, y0)的公式。以及计算θ这里的公式。请注意,您需要计算与点定义的直线垂直的角度。
您可以在下面这个小测试代码中的函数twoPoints2Polar中找到代码:
#include <opencv2/opencv.hpp>
cv::Vec2f twoPoints2Polar(const cv::Vec4i& line)
{
// Get points from the vector
cv::Point2f p1(line[0], line[1]);
cv::Point2f p2(line[2], line[3]);
// Compute 'rho' and 'theta'
float rho = abs(p2.x*p1.y - p2.y*p1.x) / cv::norm(p2 - p1);
float theta = -atan2((p2.x - p1.x) , (p2.y - p1.y));
// You can have a negative distance from the center
// when the angle is negative
if (theta < 0) {
rho = -rho;
}
return cv::Vec2f(rho, theta);
}
int main()
{
cv::Mat3b img(300, 300, cv::Vec3b(0,0));
// x1, y1, x2, y3
cv::Vec4i u(50, 130, 250, 80);
// Get rho, theta
cv::Vec2f v = twoPoints2Polar(u);
// Draw u in blue
cv::line(img, cv::Point(u[0], u[1]), cv::Point(u[2], u[3]), cv::Scalar(255, 0, 0), 3);
// Draw v in red
float rho = v[0];
float theta = v[1];
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
cv::Point pt1( cvRound(x0 + 1000 * (-b)),
cvRound(y0 + 1000 * (a)));
cv::Point pt2( cvRound(x0 - 1000 * (-b)),
cvRound(y0 - 1000 * (a)));
cv::line(img, pt1, pt2, cv::Scalar(0, 0, 255), 1, 8);
cv::imshow("Lines", img);
cv::waitKey();
return 0;
}https://stackoverflow.com/questions/40638778
复制相似问题