文章目录 HoughLines 算子 HoughLinesP 算子 示例
HoughLines 算子
cv:: HoughLines (
InputArray src,
OutputArray lines,
double rho,
double theta,
int threshold,
double srn= 0 ;
double stn= 0 ;
double min_theta= 0 ;
double max_theta= CV_PI
)
HoughLinesP 算子
cv:: HoughLinesP (
InputArray src,
OutputArray lines,
double rho,
double theta,
int threshold,
double minLineLength= 0 ;
double maxLineGap= 0 ;
)
示例
# include <iostream>
# include <opencv2/opencv.hpp>
using namespace std;
using namespace cv; Mat src, src_gray, dst; const char * output_title = "final image" ;
int main ( )
{ src = imread ( "test.png" ) ; if ( src. empty ( ) ) { cout << "could not load img..." ; return - 1 ; } namedWindow ( output_title) ; imshow ( "test" , src) ; Canny ( src, src_gray, 150 , 200 ) ; cvtColor ( src_gray, dst, COLOR_GRAY2BGR) ; vector< Vec4f> plines; HoughLinesP ( src_gray, plines, 1 , CV_PI / 180.0 , 10 , 0 , 10 ) ; Scalar color = Scalar ( 0 , 0 , 255 ) ; for ( size_t i = 0 ; i < plines. size ( ) ; i++ ) { Vec4f hline = plines[ i] ; line ( dst, Point ( hline[ 0 ] , hline[ 1 ] ) , Point ( hline[ 2 ] , hline[ 3 ] ) , color, 3 , LINE_AA) ; } imshow ( output_title, dst) ; waitKey ( 0 ) ; return 0 ;
}
# include <iostream>
# include <opencv2/opencv.hpp>
using namespace std;
using namespace cv; Mat src, src_gray, dst; const char * output_title = "final image" ;
int main ( )
{ src = imread ( "test.png" ) ; if ( src. empty ( ) ) { cout << "could not load img..." ; return - 1 ; } namedWindow ( output_title) ; imshow ( "test" , src) ; Canny ( src, src_gray, 150 , 200 ) ; cvtColor ( src_gray, dst, COLOR_GRAY2BGR) ; vector< Vec2f> lines; HoughLines ( src_gray, lines, 1 , CV_PI / 180 , 150 , 0 , 0 ) ; for ( size_t i = 0 ; i < lines. size ( ) ; i++ ) { 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 ( dst, pt1, pt2, Scalar ( 0 , 0 , 255 ) , 1 , LINE_AA) ; } imshow ( output_title, dst) ; waitKey ( 0 ) ; return 0 ;
}