hough 变换检测直线 c++

opencv3.0Beta+VS2012的hough变换检测直线,如果opencv版本不同引入的库也会不一样,大家注意!!!

#include "opencv2/opencv.hpp" #include "opencv2/video/tracking.hpp" #include "opencv2/highgui/highgui.hpp" #include "cv.h" #include "highgui.h" #include #include #include #include #include using namespace std; using namespace cv; int main() { int n=0,m=0; Mat srcImage =imread("e:\\data\\10.jpg");//根据自己图片的位置更改路径 Mat midImage,dstImage; Canny(srcImage, midImage, 50, 200, 3);//边缘检测 cvtColor(midImage,dstImage, CV_GRAY2BGR);//颜色空间转换 vector lines; HoughLines(midImage, lines, 1, CV_PI / 180, 150, 0, 0);//hough变换 for( size_t i = 0; i < lines.size(); i++ )//画hough变换检测后的线 { float rho = lines[i][0]; float theta = lines[i][1]; Point pt1, pt2; double a = cos(theta), b = sin(theta); double x0 = a*rho, y0 = b*rho; pt1.x = cvRound(x0 + 1000*(-b)); pt1.y = cvRound(y0 + 1000*(a)); pt2.x = cvRound(x0 - 1000*(-b)); pt2.y = cvRound(y0 - 1000*(a)); line( dstImage, pt1, pt2, Scalar(55,100,195), 1, CV_AA); } imshow("original", srcImage);//显示图片的窗口 imshow("canny", midImage); imshow("hough", dstImage); waitKey(0); return 0; }

你可能感兴趣的:(opencv相关代码)