簡體   English   中英

在c ++中獲取opencv錯誤

[英]Getting opencv error in c++

我試着得到opencv的錯誤! 說我有這個程序:

#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>

int main (){
    cv::Mat frame;
    cv::VideoCapture cap(1); // I don't have a second videoinput device! 
    int key = 0; 

    while(key !=27){
        cap >> frame;
        cv::imshow("frame",frame);
        key = cv::waitKey(10);
    }

    cap.release();
    return 0;
}

當我運行這個程序時,我在控制台中收到這條消息:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in unknown functi
on, file ..\..\..\opencv\modules\highgui\src\window.cpp, line 261

我的問題是如何獲取此消息並將其保存在字符串中以獲取每個錯誤! 如果有可能逃脫程序崩潰!

提前致謝!

它使用C ++異常。 這里的文檔為多。

try
{
    ... // call OpenCV
}
catch( cv::Exception& e )
{
    const char* err_msg = e.what();
    std::cout << "exception caught: " << err_msg << std::endl;
}

OpenCV代碼中的CV_Assert是一個調用OpenCV函數error的宏。 這個功能可以在這里看到。 除非您沒有設置customErrorCallback否則它將始終在stderr上打印錯誤文本。 您可以通過cvRedirectError執行此cvRedirectError ,請參閱此處

您必須檢查代碼中的OpenCV函數調用是否成功執行。 然后你就可以理解確切的問題。 這是修改后的代碼。

int main (){
    cv::Mat frame;
    cv::VideoCapture cap(1); // I don't have a second videoinput device! 

    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the video cam" << endl;
         return -1;
    }

    int key = 0; 

    while(key !=27){
        bool bSuccess = cap.read(frame); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
            cout << "Cannot read the frame from video cam" << endl;
            break;
        }
        cv::imshow("frame",frame);
        key = cv::waitKey(10);
    }

    cap.release();
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM