2011-06-29 03:32:48 +08:00
|
|
|
/**
|
|
|
|
* @file HoughLines_Demo.cpp
|
|
|
|
* @brief Demo code for Hough Transform
|
|
|
|
* @author OpenCV team
|
|
|
|
*/
|
|
|
|
|
2014-07-04 22:48:15 +08:00
|
|
|
#include "opencv2/imgcodecs.hpp"
|
2016-02-15 21:37:29 +08:00
|
|
|
#include "opencv2/highgui.hpp"
|
|
|
|
#include "opencv2/imgproc.hpp"
|
2011-06-29 03:32:48 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
/// Global variables
|
|
|
|
|
|
|
|
/** General variables */
|
2012-10-17 07:18:30 +08:00
|
|
|
Mat src, edges;
|
|
|
|
Mat src_gray;
|
2011-06-29 03:32:48 +08:00
|
|
|
Mat standard_hough, probabilistic_hough;
|
|
|
|
int min_threshold = 50;
|
|
|
|
int max_trackbar = 150;
|
|
|
|
|
2012-11-07 22:21:20 +08:00
|
|
|
const char* standard_name = "Standard Hough Lines Demo";
|
|
|
|
const char* probabilistic_name = "Probabilistic Hough Lines Demo";
|
2011-06-29 03:32:48 +08:00
|
|
|
|
|
|
|
int s_trackbar = max_trackbar;
|
|
|
|
int p_trackbar = max_trackbar;
|
|
|
|
|
|
|
|
/// Function Headers
|
|
|
|
void help();
|
|
|
|
void Standard_Hough( int, void* );
|
|
|
|
void Probabilistic_Hough( int, void* );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function main
|
|
|
|
*/
|
2017-07-26 13:39:53 +08:00
|
|
|
int main( int argc, char** argv )
|
2011-06-29 03:32:48 +08:00
|
|
|
{
|
2017-07-26 13:39:53 +08:00
|
|
|
// Read the image
|
2019-07-28 17:09:17 +08:00
|
|
|
String imageName("building.jpg"); // by default
|
2017-07-26 13:39:53 +08:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
imageName = argv[1];
|
|
|
|
}
|
2019-07-28 17:09:17 +08:00
|
|
|
src = imread( samples::findFile( imageName ), IMREAD_COLOR );
|
2011-06-29 03:32:48 +08:00
|
|
|
|
|
|
|
if( src.empty() )
|
|
|
|
{ help();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Pass the image to gray
|
2013-04-06 22:16:51 +08:00
|
|
|
cvtColor( src, src_gray, COLOR_RGB2GRAY );
|
2012-10-17 07:18:30 +08:00
|
|
|
|
2011-06-29 03:32:48 +08:00
|
|
|
/// Apply Canny edge detector
|
|
|
|
Canny( src_gray, edges, 50, 200, 3 );
|
|
|
|
|
|
|
|
/// Create Trackbars for Thresholds
|
|
|
|
char thresh_label[50];
|
2022-06-25 11:48:22 +08:00
|
|
|
snprintf( thresh_label, sizeof(thresh_label), "Thres: %d + input", min_threshold );
|
2011-06-29 03:32:48 +08:00
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
namedWindow( standard_name, WINDOW_AUTOSIZE );
|
2011-06-29 03:32:48 +08:00
|
|
|
createTrackbar( thresh_label, standard_name, &s_trackbar, max_trackbar, Standard_Hough);
|
|
|
|
|
2013-04-08 02:45:38 +08:00
|
|
|
namedWindow( probabilistic_name, WINDOW_AUTOSIZE );
|
2011-06-29 03:32:48 +08:00
|
|
|
createTrackbar( thresh_label, probabilistic_name, &p_trackbar, max_trackbar, Probabilistic_Hough);
|
|
|
|
|
|
|
|
/// Initialize
|
|
|
|
Standard_Hough(0, 0);
|
|
|
|
Probabilistic_Hough(0, 0);
|
|
|
|
waitKey(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function help
|
|
|
|
* @brief Indications of how to run this program and why is it for
|
|
|
|
*/
|
|
|
|
void help()
|
|
|
|
{
|
|
|
|
printf("\t Hough Transform to detect lines \n ");
|
|
|
|
printf("\t---------------------------------\n ");
|
|
|
|
printf(" Usage: ./HoughLines_Demo <image_name> \n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function Standard_Hough
|
|
|
|
*/
|
|
|
|
void Standard_Hough( int, void* )
|
|
|
|
{
|
|
|
|
vector<Vec2f> s_lines;
|
2013-04-06 22:16:51 +08:00
|
|
|
cvtColor( edges, standard_hough, COLOR_GRAY2BGR );
|
2011-06-29 03:32:48 +08:00
|
|
|
|
|
|
|
/// 1. Use Standard Hough Transform
|
2012-10-17 07:18:30 +08:00
|
|
|
HoughLines( edges, s_lines, 1, CV_PI/180, min_threshold + s_trackbar, 0, 0 );
|
2011-06-29 03:32:48 +08:00
|
|
|
|
|
|
|
/// Show the result
|
2012-11-07 22:21:20 +08:00
|
|
|
for( size_t i = 0; i < s_lines.size(); i++ )
|
2011-06-29 03:32:48 +08:00
|
|
|
{
|
|
|
|
float r = s_lines[i][0], t = s_lines[i][1];
|
|
|
|
double cos_t = cos(t), sin_t = sin(t);
|
|
|
|
double x0 = r*cos_t, y0 = r*sin_t;
|
|
|
|
double alpha = 1000;
|
|
|
|
|
|
|
|
Point pt1( cvRound(x0 + alpha*(-sin_t)), cvRound(y0 + alpha*cos_t) );
|
|
|
|
Point pt2( cvRound(x0 - alpha*(-sin_t)), cvRound(y0 - alpha*cos_t) );
|
2013-04-08 02:45:38 +08:00
|
|
|
line( standard_hough, pt1, pt2, Scalar(255,0,0), 3, LINE_AA);
|
2012-10-17 07:18:30 +08:00
|
|
|
}
|
2011-06-29 03:32:48 +08:00
|
|
|
|
|
|
|
imshow( standard_name, standard_hough );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @function Probabilistic_Hough
|
|
|
|
*/
|
|
|
|
void Probabilistic_Hough( int, void* )
|
|
|
|
{
|
|
|
|
vector<Vec4i> p_lines;
|
2013-04-06 22:16:51 +08:00
|
|
|
cvtColor( edges, probabilistic_hough, COLOR_GRAY2BGR );
|
2011-06-29 03:32:48 +08:00
|
|
|
|
|
|
|
/// 2. Use Probabilistic Hough Transform
|
|
|
|
HoughLinesP( edges, p_lines, 1, CV_PI/180, min_threshold + p_trackbar, 30, 10 );
|
|
|
|
|
|
|
|
/// Show the result
|
|
|
|
for( size_t i = 0; i < p_lines.size(); i++ )
|
|
|
|
{
|
|
|
|
Vec4i l = p_lines[i];
|
2013-04-08 02:45:38 +08:00
|
|
|
line( probabilistic_hough, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(255,0,0), 3, LINE_AA);
|
2011-06-29 03:32:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
imshow( probabilistic_name, probabilistic_hough );
|
|
|
|
}
|