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

int main(int argc, char** argv)
{
    IplImage* src;
    if( argc == 2 && (src=cvLoadImage("qqqq.jpg", 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,
                               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 );
        }
#else
        lines = cvHoughLines2( dst,
                               storage,
                               CV_HOUGH_PROBABILISTIC,
                               1,
                               CV_PI/180,
                               80,
                               30,
                               10 );
        for( i = 0; i < lines->total; i++ )
        {
            CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
            cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, 8 );
        }
#endif
        cvNamedWindow( "Source", 1 );
        cvShowImage( "Source", src );

        cvNamedWindow( "Hough", 1 );
        cvShowImage( "Hough", color_dst );

        cvWaitKey(0);
    }

}

i used this code for "hough transform" in opencv to detect object in a image. and program run without any error. but the result is only a console window appear quickly and disappear quickly. what should i do for this.

Thar1988
  • 511
  • 3
  • 9
  • 21
  • The program should ideally wait indefinitely on `cvWaitKey(0);` but since it is not, I suspect, may be the image is not getting loaded at all (in the first if condition). Can you recheck about this? – UltraInstinct Jun 02 '12 at 17:01
  • image path is correct.are there any method to check whether image is loaded or not? – Thar1988 Jun 02 '12 at 17:13
  • The way you are doing it is fine enough - The check for the return value to be non-zero. Does the control go inside the if-block? – UltraInstinct Jun 02 '12 at 17:20
  • 2
    And since this question is tagged `C++` why not use C++ `cv::Mat` class instead of C-ish `IPLImage` ? – UltraInstinct Jun 02 '12 at 17:21
  • yeah that was a mistake .IPLImage is much easier to me than cv::Mat – Thar1988 Jun 02 '12 at 17:33
  • I would suggest trying your hand at cv::* functions. Its easier to handle, and does much of your work. – UltraInstinct Jun 02 '12 at 17:35

1 Answers1

1

That's some bad logic you got over there:

  • 1st: if argc is bigger or smaller than 2, your main code won't run, nor will you be notified about it.
  • 2nd: if for any reason cvLoadImage() fails, you will also not be notified about it.

I suspect one of these 2 things are happening: either you are not calling your program with the right number of parameters, or cvLoadImage() is failing (unable to find the file or the file type is not supported).

I suggest you add appropriate debugs (printf calls) to see what is really going on.

EDIT:

A couple of notes:

  • If your image is being loaded as "qqqq.jpg" and if you are running your program from within Visual Studio, you need to put the image inside the same folder as your source code files (and not in the folder where your .exe is);
  • If you are using Windows and trying to load the image using the full path, don't forget to escape the slashes: C:\\folder\\qqqq.jpg
  • FYI, argc == 2 implies that you are running your application from the command-line using the format: app.exe param1
karlphillip
  • 92,053
  • 36
  • 243
  • 426