2

我已经实现了在 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);
    }
4

1 回答 1

0

首先,您可以从 while() 语句中开始声明 cameraCapture:

 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文档:这里写的是目前该函数仅支持视频文件:

  • 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);

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

于 2012-12-10T16:31:25.880 回答