// Line Detection by Hough Line Transform
// http://opencvexamples.blogspot.com/2013/10/line-detection-by-hough-line-transform.html
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#define HIST
using namespace cv;
using namespace std;
int main(int argc, char *argv[])
{
Mat src;
if (argc >= 1){
src = imread(argv[1]);
} else {
printf("Usage : HoughLine_example <read_file_name>\n");
exit(1);
}
Mat dst, cdst;
Mat src_g;
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
cvtColor(src, src_g, CV_BGR2GRAY);
#ifdef HIST
Mat eqst;
equalizeHist(src_g, eqst);
Canny(eqst, dst, 150, 250, 3); // for equalizeHist
#else
//Canny(src_g, dst, 50, 200, 3); // Normal
Canny(src_g, dst, 200, 300, 3); // Normal2
#endif
//gettimeofday(&end_time, NULL);
cvtColor(dst, cdst, CV_GRAY2BGR);
vector<Vec2f> lines;
// detect lines
HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0);
gettimeofday(&end_time, NULL);
// draw lines
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], 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( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
}
if (end_time.tv_usec < start_time.tv_usec) {
printf("total time = %ld.%06ld sec\n", end_time.tv_sec - start_time.tv_sec - 1, 1000000 + end_time.tv_usec - start_time.tv_usec);
}
else {
printf("total time = %ld.%06ld sec\n", end_time.tv_sec - start_time.tv_sec, end_time.tv_usec - start_time.tv_usec);
}
#ifdef HIST
imshow("equalizeHist", eqst);
#endif
imshow("Canny", dst);
imshow("source", src);
imshow("detected lines", cdst);
waitKey();
waitKey();
waitKey();
waitKey();
return 0;
}
日 | 月 | 火 | 水 | 木 | 金 | 土 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 | - | - | - | - |