1
votes

I've got some code that isn't working as intended. I have an application that uses imshow() to display an image, but I also want the program to still work if there is no display. For this reason I am trying to catch the exception from imshow (GTK-Warning: cannot open display) and continue the program without it (the bool 'display' is checked at every imshow() call).

//Display
bool display{false};
try{
    std::cout << "Attempting to open display..." << std::endl;
    cv::resize(image,modimage1,cv::Size(800,480));
    cv::namedWindow("Output", CV_WINDOW_NORMAL);
    cv::setWindowProperty("Output", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLS$
    cv::imshow("Output", modimage1);
    cv::waitKey(1);
    display = true;
} catch( cv::Exception& e ){
    const char* err_msg = e.what();
    std::cout << "exception caught:" << err_msg << std::endl;
    std::cout << "Failed to launch display, running without visual..." << s$
}

The catch block is copied right out of the opencv documentation, so I'm surprised that it doesn't catch the exception. The program behaves exactly as it did without the try block. The last output is "Attempting to open display" then I get the "GTK-Warning" and it exits the program.

So, what's missing? Am I catching the wrong error type? Is the exception not actually thrown by openCV but GTK/X/?

Thanks

2
I think I found the answer to this but I've got another thread going to test the solution. In this case X terminates the program before an exception is thrown, so the exception is never caught. The only solution I found was to use gtk_init_check() first. My problem is I can't get my application to link correctly with the gtk libraries. - DrTarr

2 Answers

0
votes

I don't think these are complete explanations of strange cv::Exception behavior.

try {
    cap = new cv::VideoCapture(<file_name>);
} catch(cv::Exception& ex) {
    cerr << "opencv exception." << endl;
}

If the file can't be found, my message is never displayed but the following is displayed:

[ERROR:0] global /data/data/com.termux/files/home/opencv4/opencv/modules/videoio/src/cap.cpp (142) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.3.0-pre) /data/data/com.termux/files/home/opencv4/opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): file_name in function 'icvExtractPattern'

This has nothing to do with an X server.

-1
votes

Fyi, the program is terminated before you can catch an exception in this case. My solution was the following code:

//Display
bool display{false};
display = gtk_init_check(NULL, NULL);
if (!display){
    std::cout << "Display unavailable, continuing without..." << std::endl;
}
if (display) {
    std::cout << "Attempting to open display..." << std::endl;
    cv::resize(image,modimage1,cv::Size(800,480));
    cv::namedWindow("Output", CV_WINDOW_NORMAL);
    cv::setWindowProperty("Output", CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);
    cv::imshow("Output", modimage1);
    cv::waitKey(1);
    display = true;
}

This works perfectly for me. The main obstacle was the "#include ", which took some effort to get to link with the appropriate libraries.