Opencv Capture导致内存分配错误

时间:2012-12-10 13:32:18

标签: c memory-management opencv capture

我已经实现了在OpenCV库上运行的捕获代码。 2个摄像头的代码捕获按订单排序。但是代码会导致内存分配错误。

我必须释放camera1的捕获流来打开camera2的捕获流。我无法同时获得两次捕获所以我必须按顺序捕获它。

为什么在这种情况下会出现内存分配错误?

我的代码位于以下位置:

#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <stdio.h>

  CvCapture* camera; // Use the default camera
  IplImage*     frame;

int main(int argc, char* argv[])
{    

     while(1)
     {
      camera = cvCreateCameraCapture(0); // Use the default camera
      //camera2 = cvCreateCameraCapture(1); // Use the default camera
      frame = 0;
      //frame2 = 0;

      cvSetCaptureProperty(camera,CV_CAP_PROP_FRAME_WIDTH,1024) ;
      cvSetCaptureProperty(camera,CV_CAP_PROP_FRAME_HEIGHT,768); 

      frame = cvQueryFrame(camera); //need to capture at least one extra frame

      if (frame != NULL) {
        printf("Frame extracted from CAM1\n\r");
            cvSaveImage("/dev/shm/webcam1.jpg", frame,0);
        printf("Frame from CAM1 saved\n\r");
      } else {
          printf("Null frame 1\n\r");
      }

      cvReleaseImage(&frame);
      cvReleaseCapture(&camera);

      camera = cvCreateCameraCapture(1); // Use the default camera

      cvSetCaptureProperty(camera,CV_CAP_PROP_FRAME_WIDTH,1024) ;
      cvSetCaptureProperty(camera,CV_CAP_PROP_FRAME_HEIGHT,768); 

      frame = cvQueryFrame(camera); //need to capture at least one extra frame

      if (frame != NULL) {
        printf("Frame extracted from CAM2\n\r");
            cvSaveImage("/dev/shm/webcam2.jpg", frame,0);
        printf("Frame from CAM2 saved\n\r");
      } else {
          printf("Null frame 2\n\r");
      }
      cvReleaseImage(&frame);
      cvReleaseCapture(&camera);
    }

1 个答案:

答案 0 :(得分:0)

首先,你可以从cameraCapture:

开始声明while()语句
 camera0 = cvCreateCameraCapture(0);
 camera1 = cvCreateCameraCapture(1);
 frame0 = 0;
 frame1 = 0;
 cvSetCaptureProperty(camera0,CV_CAP_PROP_FRAME_WIDTH,1024) ;
 cvSetCaptureProperty(camera0,CV_CAP_PROP_FRAME_HEIGHT,768);
 cvSetCaptureProperty(camera1,CV_CAP_PROP_FRAME_WIDTH,1024) ;
 cvSetCaptureProperty(camera1,CV_CAP_PROP_FRAME_HEIGHT,768); 

 while(1) {
     /* your operation */
     frame0 = cvQueryFrame(camera0);
     frame1 = cvQueryFrame(camera1);
     /* your operation */
 }

 cvReleaseImage(&frame0);
 cvReleaseImage(&frame1);
 cvReleaseCapture(&camera0);
 cvReleaseCapture(&camera1);

修改

如果你想从A-cam然后从B-cam获取第一个流,你将拥有像这样的代码。我不明白你想要做什么,所以这段代码只是在窗口上显示你的流。 请记住使用 cvWaitKey(...),以便让highgui有时间处理来自 cvShowImage()的绘制请求。 另外,请看一下 cvSetCaptureProperty documentation:这里写的是当前该函数仅支持视频文件:

  • CV_CAP_PROP_POS_MSEC;
  • CV_CAP_PROP_POS_FRAMES;
  • CV_CAP_PROP_POS_AVI_RATIO。

顺便说一句,这是我的建议:

const char* wndName = "window";
cvNamedWindow(wndName, CV_WINDOW_NORMAL);

IplImage* frame;
CvCapture* capture;

while(true) {
  capture = cvCaptureFromCAM(0);

  /* Showing for a while capture in window */
  while(/* your condition */) {
    frame = cvQueryFrame(capture);
    /* operation with A cam */
    cvShowImage(wndName, frame);
    cvWaitKey(30);
  }

  cvReleaseCapture(&capture);
  cvWaitKey(100);
  /* switching source cam */
  capture = cvCaptureFromCAM(1);

  /* Showing for a while capture in the same window */
  while(/* your condition */) {
    frame = cvQueryFrame(capture);
    /* operation with B cam */
    cvShowImage(wndName, frame);
    cvWaitKey(30);
  }
} 
cvReleaseImage(&frame); 
cvReleaseCapture(&capture);

这对我有用,给我任何反馈。