cvReleaseCapture() error

cvReleaseCapture() error

本文关键字:error cvReleaseCapture      更新时间:2023-10-16

为了使我的问题更清楚,请查看以下代码:

对于捕捉图像:

void CameraTest ::on_snapButton_clicked()
{
    CvCapture* capture = cvCaptureFromCAM(0); // capture from video device #0

    cvSetCaptureProperty(capture ,CV_CAP_PROP_FRAME_WIDTH , 800); 
    cvSetCaptureProperty(capture ,CV_CAP_PROP_FRAME_HEIGHT , 600); 

    if(!cvGrabFrame(capture))  //if no webcam detected or failed to capture anything
    {              // capture a frame 
        cout << "Could not grab a framen7";
        exit(0);
    }
    IplImage* img=cvRetrieveFrame(capture);           // retrieve the captured frame
    cv::Mat imageContainer(img);
    image=imageContainer;
    cv::imshow("Mat",image);  
    //cvReleaseCapture(&capture);  When I enable this, and run the programming calling this, there will be an error. 
}

现在,程序显示图像:

      void CameraTest ::on_processButton_clicked() 
      {
          cv::imshow("image snapped", image);
          //my image processing steps...
      }

启用cvReleaseCapture(&capture)行时,收到以下错误:

Unhandled exception at 0x00fc3ff5 in CameraTest.exe: 0xC0000005: Access violation reading location 0x042e1030.

当我评论/删除该行时,我能够在单击另一个按钮时正确显示图像,但是当我想捕捉新图像时,我必须单击该按钮几次,这是程序的主要缺陷。有没有办法绕过它?

  • 避免过时的c-api(IplImages,Cv*functions)坚持使用C ++ API。
  • 您从捕获点到凸轮驱动程序内部的内存获得的图像。 如果你不克隆()图像,并释放捕获,你会得到一个悬空的指针。
  • 不要为每个镜头创建新捕获。(凸轮需要一些"预热"时间,所以它会很慢)。改为在类中保留一个实例

class CameraTest 
{
    VideoCapture capture;         // make it a class - member
    CameraTest () : capture(0)    // capture from video device #0
    {
         capture.set(CV_CAP_PROP_FRAME_WIDTH , 800); 
         capture.set(CV_CAP_PROP_FRAME_HEIGHT , 600); 
    }
    // ...
};
void CameraTest ::on_snapButton_clicked()
{
    Mat img;                // temp var pointing todriver mem    
    if(!capture.read(img))  //if no webcam detected or failed to capture anything
    {
        cout << "Could not grab a framen7";
        exit(0);
    }
    image = img.clone();      // keep our member var alive
    cv::imshow("Mat",image);  
}

替换 :

if(!cvGrabFrame(capture))  //if no webcam detected or failed to capture anything
    {              // capture a frame 
        cout << "Could not grab a framen7";
        exit(0);
    }

if ( !capture )
        {
            cout << "Could not grab a framen7";
            exit(0);
        }

并替换

  IplImage* img=cvRetrieveFrame(capture);

IplImage* img = cvQueryFrame( capture );

cvQueryFrame 抓取并从视频或相机返回帧。此函数是一次调用中 cvGrabFrame 和 cvRetrieveFrame 的combination。返回的镜像不应由用户发布或修改。

相关文章: