原理:
实现:
/**
* @description: 霍夫变换
* @param edge 输入边缘
* @param lines 检测直线
* @param threshold 阈值
*/
void houghlines(cv::Mat& edge, std::vector<cv::Vec2f>& lines, int threshold)
{
cv::Mat H = cv::Mat::zeros(2 * edge.rows + 2 * edge.cols, 180, CV_16S);
float theta, rho;
for (int i = 0; i < edge.rows; i++)
{
for (int j = 0; j < edge.cols; j++)
{
if (edge.at<uchar>(i, j) > 0)
{
for (theta = 0; theta < 180; ++theta)
{
rho = round(i*sin(theta*CV_PI / 180) + j*cos(theta*CV_PI / 180));
H.at<short>(rho + edge.rows + edge.cols, theta) += 1;
}
}
}
}
for (int i = 0; i < H.rows; ++i)
{
for (int j = 0; j < H.cols; ++j)
{
if (H.at<short>(i, j) > threshold)
lines.push_back(cv::Vec2f(i - edge.rows - edge.cols, j*CV_PI / 180));
}
}
}