#include<iostream>
#include<cv.h>
#include<highgui.h>
using namespace cv;
using namespace std;
Mat img;
int thresh=100;
void on_trackbar(int, void *){
Mat edges;
Canny(img,edges,50,100);
vector<Vec2f> lines;
HoughLines(edges,lines,1,CV_PI/180.F,thresh);
Mat img_show=img.clone();
for(int i=0;i<lines.size();i++){
float rho=lines[i][0];
float theta=lines[i][1];
double a=cos(theta), b=sin(theta);
double x0=a*rho, y0=b*rho;
Point pt1(cvRound(x0+1000*(-b)),cvRound(y0+1000*(a)));
Point pt2(cvRound(x0-1000*(-b)),cvRound(y0-1000*(a)));
line(img_show,pt1,pt2,Scalar(0,0,255));
}
imshow("shapes",img_show);
}
int main(){
img=imread("images/linescircles.jpg");
namedWindow("shapes");
createTrackbar("Acc. thresh","shapes",&thresh,300,on_trackbar);
on_trackbar(0,0);
while(char(waitKey(0))!='q'){}
return 0;
}
If you've gone through the previous posts(#11x.), this would'nt need much explanation.
HoughLines(edges,lines,1,CV_PI/180.F,thresh);
Format & Explanation:
HoughLines(InputArray image, OutputArray lines, double rho, double theta, int threshold, double srn=0, double stn=0 )
| Parameters: |
- image – 8-bit, single-channel binary source image. The image may be modified by the function.
--> In our case: 'edges'
- lines – Output vector of lines. Each line is represented by a two-element vector
. is the distance from the coordinate origin (top-left corner of the image). is the line rotation angle in radians ( ).
-->The above point is the gist of the program
- rho – Distance resolution of the accumulator in pixels.
- theta – Angle resolution of the accumulator in radians.
- threshold – Accumulator threshold parameter. Only those lines are returned that get enough votes (
).
The following may not be relevant to this tutorial.
- srn – For the multi-scale Hough transform, it is a divisor for the distance resolution rho . The coarse accumulator distance resolution isrho and the accurate accumulator resolution is rho/srn . If both srn=0 and stn=0 , the classical Hough transform is used. Otherwise, both these parameters should be positive.
- stn – For the multi-scale Hough transform, it is a divisor for the distance resolution theta.
- method –
One of the following Hough transform variants:
- CV_HOUGH_STANDARD classical or standard Hough transform. Every line is represented by two floating-point numbers
, where is a distance between (0,0) point and the line, and is the angle between x-axis and the normal to the line. Thus, the matrix must be (the created sequence will be) of CV_32FC2 type
- CV_HOUGH_PROBABILISTIC probabilistic Hough transform (more efficient in case if the picture contains a few long linear segments). It returns line segments rather than the whole line. Each segment is represented by starting and ending points, and the matrix must be (the created sequence will be) of the CV_32SC4 type.
- CV_HOUGH_MULTI_SCALE multi-scale variant of the classical Hough transform. The lines are encoded the same way asCV_HOUGH_STANDARD.
- param1 –
First method-dependent parameter:
- For the classical Hough transform, it is not used (0).
- For the probabilistic Hough transform, it is the minimum line length.
- For the multi-scale Hough transform, it is srn.
- param2 –
Second method-dependent parameter:
- For the classical Hough transform, it is not used (0).
- For the probabilistic Hough transform, it is the maximum gap between line segments lying on the same line to treat them as a single line segment (that is, to join them).
- For the multi-scale Hough transform, it is stn.
|
You may go through the Documentation HERE.
No comments:
Post a Comment