0

Can OpenCV be made to work with the following code, modified from here:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <thread>

using namespace cv;
using namespace std;

void showWindow(std::string const &name, cv::Mat const &img)
{
    cv::namedWindow(name, cv::WINDOW_AUTOSIZE);
    cv::imshow(name, img);
}

int main( int argc, char** argv )
{
    if( argc != 2)
    {
     cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
     return -1;
    }

    Mat image;
    image = imread(argv[1], CV_LOAD_IMAGE_COLOR);

    if(! image.data )
    {
        cout <<  "Could not open or find the image" << std::endl ;
        return -1;
    }

    std::thread t1(showWindow, "Display window", image);
    t1.join();

    waitKey(0);
    return 0;
}

Compiled with g++ -std=c++11 -g opencv.cpp -o app `pkg-config --cflags --libs opencv`. My actual example is much more complex, but this is a MCVE.

The program hangs, and pressing CTRL+C in gdb gives me:

Program received signal SIGINT, Interrupt.
0x00007ffff6a7dd4d in nanosleep () at ../sysdeps/unix/syscall-template.S:81
81  ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) bt
#0  0x00007ffff6a7dd4d in nanosleep () at ../sysdeps/unix/syscall-template.S:81
#1  0x00007ffff6aaefd4 in usleep (useconds=<optimised out>) at ../sysdeps/unix/sysv/linux/usleep.c:32
#2  0x00007ffff7bafe1e in cvWaitKey () from /usr/local/lib/libopencv_highgui.so.3.2
#3  0x00007ffff7baa2c6 in cv::waitKey(int) () from /usr/local/lib/libopencv_highgui.so.3.2
#4  0x0000000000401970 in main (argc=3, argv=0x7fffffffd418) at opencv.cpp:31

I tried rebuilding with WITH_TBB=ON but it made no difference.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • There seems to be some inconsistency, as I see the `waitKey` function on line 35 but the error message contains `opencv.cpp:31`. / Unfortunately SO doesn't have line numbers... – user202729 Jan 08 '18 at 06:29
  • Is any windows displayed? – user202729 Jan 08 '18 at 06:30
  • I slightly edited the code, but it's the only `waitKey()` call that hangs. I get a window frame displayed but no contents. – Ken Y-N Jan 08 '18 at 06:45
  • ... can you press a key in the window? ... Because (of course) `waitKey` wait for a key. Also, unfortunately, cv doesn't seem to work with threads, creating windows in the main thread is better. – user202729 Jan 08 '18 at 06:48
  • No, that doesn't work either. Creating windows in one specific thread looks like it will be a pain, though. I'll have to create a class that manages a single window thread that multiple callers from their own individual threads can access. Ending a thread (something I have no control over in my actual application) with windows still open seems to be A Bad Thing – Ken Y-N Jan 08 '18 at 06:54
  • It works fine, if I compile with -pthread – gj13 Jan 08 '18 at 07:34
  • @gj13 Nope, no joy for me. My OpenCV uses QT with the extra toolbar on the window - do you? – Ken Y-N Jan 09 '18 at 01:39
  • Mine is without Qt. – gj13 Jan 15 '18 at 07:04

0 Answers0