1 #include "opencv2/highgui/highgui.hpp" 2 #include "opencv2/imgproc/imgproc.hpp" 3 4 #include <iostream> 5 6 using namespace cv; 7 using namespace std; 8 9 void help() 10 { 11 cout << "\nThis program demonstrates line finding with the Hough transform.\n" 12 "Usage:\n" 13 "./houghlines <image_name>, Default is pic1.jpg\n" << endl; 14 } 15 16 int main(int argc, char** argv) 17 { 18 const char* filename = argc >= 2 ? argv[1] : "pic1.jpg"; 19 20 Mat src = imread(filename, 0); 21 if(src.empty()) 22 { 23 help(); 24 cout << "can not open " << filename << endl; 25 return -1; 26 } 27 28 Mat dst, cdst; 29 Canny(src, dst, 50, 200, 3); 30 cvtColor(dst, cdst, CV_GRAY2BGR); 31 32 #if 0 33 vector<Vec2f> lines; 34 HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 ); 35 36 for( size_t i = 0; i < lines.size(); i++ ) 37 { 38 float rho = lines[i][0], theta = lines[i][1]; 39 Point pt1, pt2; 40 double a = cos(theta), b = sin(theta); 41 double x0 = a*rho, y0 = b*rho; 42 pt1.x = cvRound(x0 + 1000*(-b)); 43 pt1.y = cvRound(y0 + 1000*(a)); 44 pt2.x = cvRound(x0 - 1000*(-b)); 45 pt2.y = cvRound(y0 - 1000*(a)); 46 line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA); 47 } 48 #else 49 vector<Vec4i> lines; 50 HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 ); 51 for( size_t i = 0; i < lines.size(); i++ ) 52 { 53 Vec4i l = lines[i]; 54 line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA); 55 } 56 #endif 57 imshow("source", src); 58 imshow("detected lines", cdst); 59 60 waitKey(); 61 62 return 0; 63 }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)