
#include <iostream>
#include <filesystem>
#include <string>
#include <windows.h>
#include <opencv2/opencv.hpp>
namespace fs = std::filesystem;
using namespace cv;
using namespace std;
// 霍夫变换检测直线
void test()
{
cv::Mat src;
src = cv::imread("F:/opencv/console/x64/Debug/jiang.png");
cv::imshow("src", src);
cv::Mat canny, dst;
cv::Canny(src, canny, 150, 200); // 配合canny算法使用
cv::cvtColor(canny, dst, cv::COLOR_GRAY2BGR); // 灰度图转换为彩色图
cv::imshow("edge", canny);
std::vector<cv::Vec4f> plines;
cv::HoughLinesP(canny, plines, 1, CV_PI / 180.0, 5, 0, 10);
cv::Scalar color = cv::Scalar(0, 0, 255);
for (size_t i = 0; i < plines.size(); i++) {
cv::Vec4f hline = plines[i];
cv::line(dst, cv::Point(hline[0], hline[1]), cv::Point(hline[2], hline[3]), color, 3, cv::LINE_AA);
}
cv::imshow("output", dst);
}
int main()
{
test();
waitKey(0);
destroyAllWindows();
return 0;
}
// 霍夫变换直线检测
// 参数说明
/*
image:输入图像。8 bit 灰度图。
lines:存储线段极坐标的容器,每一条线由具有四个元素的矢量(x_1, y_1, x_2, y_2) 表示,其中(x_1, y_1)和(x_2, y_2) 是每个检测到的线段的结束点。
rho:生成极坐标的像素扫描步长。
theta:生成极坐标的角度步长,一般是π / 180。
threshold:要“检测” 一条直线所需最少的的曲线交点。
minLineLength:默认值 0,表示最低线段的长度,比这个设定参数短的线段就不能被显现出来。
maxLineGap:默认值 0,允许将同一行点与点之间连接起来的最大的距离。
*/