簡體   English   中英

OpenCV - Videocapture中未處理的異常

[英]OpenCV - unhandled exception in Videocapture

我最近安裝了OpenCV 2.4.7並將其配置為我的Visual Studio 2010 Ultimate ide ...我甚至測試了一個代碼來顯示圖像......

#include "opencv2/highgui/highgui.hpp"
#include "iostream"

using namespace cv;
using namespace std;

int main()
{
    Mat im = imread("d:/lena.jpg");
    if (im.empty()) 
    {
        cout << "Cannot load image!" << endl;
        return -1;
    }
    imshow("Image", im);
    waitKey(0);
}

它工作,但當我嘗試使用這里給出的視頻捕捉代碼時,它會給出一個錯誤..

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    Mat edges;
    namedWindow("edges",1);
    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        cvtColor(frame, edges, CV_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

myNewOpenCv1.exe中0x75dc812f處的未處理異常:Microsoft C ++異常:cv ::內存位置0x0019f6d8的異常

我不知道它是否與安裝有關。 我對OpenCV很新,並且不太了解,如果任何習慣這種情況的人都可以為我解決這個錯誤,並且還會給我一個解釋,為什么它會發生,並且這方面的指導會很棒。

希望等待你的答案 - 喬納森 -

嘗試更換

cap >> frame;

有:

while (frame.empty()) {
    cap >> frame;
}

有時opencv相機API會為前幾幀提供垃圾,但過了一段時間后一切正常。

您可能希望將該循環限制為固定的迭代次數,以避免無限運行。

以下代碼行僅用於邊緣檢測。

cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);

所以,如果您只對視頻捕獲感興趣,請使用以下代碼:

#include "opencv2/opencv.hpp"

using namespace cv;

int main(int, char**)
{
    VideoCapture cap(0); // open the default camera
    if(!cap.isOpened())  // check if we succeeded
        return -1;

    for(;;)
    {
        Mat frame;
        cap >> frame; // get a new frame from camera
        imshow("display", frame);
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

要運行此代碼,您應該在VS中設置庫路徑,並且您應該在VS中設置鏈接器選項中的dll。它將起作用!

暫無
暫無

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

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