2016-08-06 56 views
1

我有一些代碼不能按預期工作。我有一個應用程序使用imshow()來顯示圖像,但我也希望程序在沒有顯示的情況下仍然可以工作。出於這個原因,我試圖從imshow(GTK-Warning:無法打開顯示)中捕獲異常,並在沒有它的情況下繼續執行程序(在每次imshow()調用時檢查bool'display')。從imshow()和namedWindow()捕獲opencv異常

//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$ 
} 

catch塊被複制權了OpenCV的文檔,所以我很驚訝,它不捕獲異常。該程序的行爲與沒有try塊時的行爲完全相同。最後一個輸出是「嘗試打開顯示」,然後我得到「GTK-Warning」並退出程序。

那麼,缺少什麼?我是否抓錯了錯誤類型? openCV引發的異常不是真的,而是GTK/X /?

謝謝

+0

我想我找到了答案,但我有另一個線程去測試解決方案。在這種情況下,X在引發異常之前終止程序,所以異常不會被捕獲。我發現的唯一解決方案是首先使用gtk_init_check()。我的問題是我無法讓我的應用程序與gtk庫正確鏈接。 – DrTarr

回答

0

Fyi,程序被終止,然後才能在這種情況下捕獲異常。我的解決方案是以下代碼:

//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; 
} 

這對我來說非常合適。主要障礙是「#include」,它花費了一些努力來與適當的庫鏈接。