本文主要是介绍HoughLines霍夫变换实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
原理:
实现:
/*** @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));}}
}
代码传送门:https://github.com/taifyang/OpenCV-algorithm
这篇关于HoughLines霍夫变换实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!