开发者

logic behind the code

开发者 https://www.devze.com 2023-01-06 00:16 出处:网络
this is from opencv hough lines, can any one explain me, after changing it tio cartesian WHY THEY ADDED a+1000, -b*1000

this is from opencv hough lines, can any one explain me, after changing it tio cartesian WHY THEY ADDED a+1000, -b*1000

#include <cv.h>
#include <highgui.h>
#include <math.h>

int main(int argc, char** argv)
{
    IplImage* src;
    if( argc == 2 && (src=cvLoadImage(argv[1], 0))!= 0)
    {
        IplImage* dst = cvCreateImage( cvGetSize(src), 8, 1 );
        IplImage* color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
        CvMemStorage* storage = cvCreateMemStorage(0);
        CvSeq* lines = 0;
        int i;
        cvCanny( src, dst, 50, 200, 3 );
        cvCvtColor( dst, color_dst, CV_GRAY2BGR );
#if 1
        lines = cvHoughLines2( dst,
                               storage,
                               CV_HOUGH_STANDARD,
                               1,
                               CV_PI/180,
 开发者_C百科                              100,
                               0,
                               0 );

        for( i = 0; i < MIN(lines->total,100); i++ )
        {
            float* line = (float*)cvGetSeqElem(lines,i);
            float rho = line[0];
            float theta = line[1];
            CvPoint 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));
            cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, 8 );
        }


Cos and Sin go from -1 to +1, so the origin of the Hough accumalator space is at 0,0.

Assuming your display has positive size it's convenient to have the centre of the plot in the middle of the screen.


Perhaps they wanted to get corners of the bounding rectangle around a given center?


It is a hack.

Try this. Run the example as is. Remove the 4 instances of 1000. You will get points instead of lines. Put in 750 instead of 1000. You get the same result as if you had put in 1000.

The 1000 is to make sure the lines get drawn across the image. You could also do the following, which is a little better:

Right after HoughLines(...) is called, add the following:

int h = src.rows;
int w = src.cols;
int factor = (int) (sqrt(h * h + w * w));  // diagonal length of the image, maximum line length

Then instead of 1000, multiply by factor. If your image is greater than 1000x1000, the original code won't work.

Roy

0

精彩评论

暂无评论...
验证码 换一张
取 消