Ask Your Question
0

Why the image just disappears in the following code

asked 2013-04-22 11:44:11 -0600

hayden gravatar image

updated 2013-04-22 12:47:57 -0600

berak gravatar image

I have copied the following code and compiled and tried to run with MSVC 2010. The image appears and the next minute disappers. i am newbie in OpenCV. Could anybody tell me what is the problem with the following code:

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

using namespace cv;
int main(int argc, char** argv)
{
    Mat img, gray;
    if( argc != 2 && !(img=imread(argv[1], 1)).data)
       return -1;

    cvtColor(img, gray, CV_BGR2GRAY);

    // smooth it, otherwise a lot of false circles may be detected

    GaussianBlur( gray, gray, Size(9, 9), 2, 2 );
    vector<Vec3f> circles;

    HoughCircles(gray, circles, CV_HOUGH_GRADIENT, 2, gray.rows/4, 200, 100 );
    for( size_t i = 0; i < circles.size(); i++ )
    {
         Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
         int radius = cvRound(circles[i][2]);
         // draw the circle center
         circle( img, center, 3, Scalar(0,255,0), -1, 8, 0 );
         // draw the circle outline
         circle( img, center, radius, Scalar(0,0,255), 3, 8, 0 );
    }
    namedWindow( "circles", 1 );
    imshow( "circles", img );
    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
4

answered 2013-04-22 12:50:06 -0600

berak gravatar image

you're missing:

waitKey(0); // wait forever

between imshow() and return

edit flag offensive delete link more

Comments

Thanks for fixing the problem.

hayden gravatar imagehayden ( 2013-04-22 15:31:38 -0600 )edit

Question Tools

Stats

Asked: 2013-04-22 11:44:11 -0600

Seen: 3,754 times

Last updated: Apr 22 '13