您的位置:首页 > 编程语言 > C语言/C++

hough 变换检测直线 c++

2017-11-09 10:18 459 查看
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 <iostream>
#include <ctype h="">
#include <opencv2 imgproc="" types_c="" h="">
#include <tchar h="">
#include <fstream>
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<vec2f> 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;
}

</vec2f></fstream></tchar></opencv2></ctype></iostream>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ hough变换