Ask Your Question
3

cvShowImage and pthreads

asked 2013-11-14 03:47:03 -0600

zestysauce gravatar image

updated 2013-11-14 03:57:24 -0600

berak gravatar image

I'm writing a multithreaded program using pthreads and opencv, and I'm having a problem where cvShowImage just appears as a blank gray rectangle (not even any window borders, completely blank).

To troubleshoot, I wrote two programs which should have identical results. one uses pthread, creates a single thread, and then displays an image, and closes. It produces the same problem, where just a gray rectangle appears.

The second program operates without pthread, with everything in main(), and works just fine. The two sample programs are below.

The operating system is OS X 10.8, and the opencv version is 2.4.6.1.

// first program: uses pthreads, cvShowImage produces only a blank rectangle
#include <stdio.h>
#include <pthread.h>
#include <highgui.h>
#include <cv.h>

int main()
{
    pthread_t display_thread;
    void *display_image();
    pthread_create(&display_thread, NULL, display_image, NULL);
    pthread_join(display_thread, NULL);

    return 0;
}

void *display_image()
{
    IplImage* image = cvLoadImage("map.png", CV_LOAD_IMAGE_COLOR);
    cvNamedWindow("window", 0);
    cvShowImage("window", image);

    sleep(1);

    cvDestroyAllWindows();
    cvReleaseImage(&image);

    pthread_exit(0);
}



// second program: works fine...
#include <stdio.h>
#include <highgui.h>
#include <cv.h>

int main()
{
    IplImage* image = cvLoadImage("map.png", CV_LOAD_IMAGE_COLOR);
    cvNamedWindow("window", 0);
    cvShowImage("window", image);

    sleep(1);

    cvDestroyAllWindows();
    cvReleaseImage(&image);
    return 0;
}
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2013-11-17 07:36:33 -0600

zestysauce gravatar image

just moving "cvNamedWindow" into main() solved the problem...

edit flag offensive delete link more

Comments

Thank you, it helped me a lot

Robst gravatar imageRobst ( 2014-08-02 17:54:17 -0600 )edit
0

answered 2013-11-14 04:34:36 -0600

Haris gravatar image

You should use cvWaitKey() after cvShowImage() to see image on window.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-11-14 03:47:03 -0600

Seen: 1,771 times

Last updated: Nov 17 '13