#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; int main(int argc, char** argv) { // 读取图像 Mat src = imread(argv[1], CV_LOAD_IMAGE_COLOR); if(!src.data) { printf("Could not open or find the image\n"); return -1; } // 转为灰度图 Mat gray; cvtColor(src, gray, CV_BGR2GRAY); // 使用Canny边缘检测 Mat edges; Canny(gray, edges, 50, 200, 3); // 使用Hough变换检测直线 vector<Vec4i> lines; HoughLinesP(edges, lines, 1, CV_PI/180, 100, 30, 10, 0); // 在原图上画出检测到的直线 for( size_t i = 0; i < lines.size(); i++ ) { Vec4i l = lines[i]; line( src, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA); } // 显示结果图像 namedWindow("Source Image", CV_WINDOW_AUTOSIZE); namedWindow("Detected Lines", CV_WINDOW_AUTOSIZE); imshow("Source Image", src); imshow("Detected Lines", edges); waitKey(); return 0; }
##
标签:src,Mat,直线,lines,c++,opencv,edges,gray,CV From: https://www.cnblogs.com/herd/p/17961739