原理
#include<iostream>
#include<opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat image = imread("D:/2015project/picture/building.jpg");
if (image.empty())
{
cout << "error";
return -1;
}
Mat cannyImage;
Canny(image, cannyImage, 50, 100);
imshow("canny image", cannyImage);
//用霍夫变换检测直线
vector<Vec2f>lines;
HoughLines(cannyImage, lines, 1, CV_PI / 180, 180);
//声明迭代器
vector<Vec2f>::const_iterator it = lines.begin();
while (it != lines.end())
{
float rho = (*it)[0];//lines的第一个元素是距离rho
float theta = (*it)[1];//liens的第二个元素是角度theta
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(image, pt1, pt2, Scalar(0, 0, 255));
++it;
}
imshow("霍夫变换图像", image);
waitKey(0);
}