c - 在系统函数中调用 OpenCV 捕获代码时卡住

标签 c opencv pthreads system

我正在 OpenCV 中开发一个应用程序。我正在从相机拍摄快照,然后关闭捕获。

下面是我的捕获代码 (capturecam1lowres.c)

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


int main(int argc, char* argv[])
{    
  CvCapture* camera = cvCreateCameraCapture(0); // Use the default camera

  IplImage*     frame = 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
//  frame = cvQueryFrame(camera);

  if (frame != NULL) {
    printf("Frame extracted from CAM1\n\r");
        cvSaveImage("/home/root/Desktop/BBTCP/webcam1.jpg", frame,0);
  } else {
      printf("Null frame 1\n\r");
  }
  cvReleaseCapture(&camera);
  cvReleaseImage(&frame);
  return 0;
}

我正在调用此代码的可执行文件 系统(./capturecam1lowres) 但它卡住在

frame = cvQueryFrame(camera); 

有时(不是每次)排队。如何为该子程序 (capturecam1lowres) 设置超时。如果捕获花费太多时间,它应该放弃并退出。我怎样才能做到这一点?

我尝试使用 posix 线程,但无法获得结果。下面也是我的非工作线程代码。

#include <cv.h>
#include <highgui.h>
#include <cxcore.h>
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>

void *thread_function(void *arg) 
 {

    sleep(10);
    exit(0);

}

int main(int argc, char* argv[])
{    
  CvCapture* camera = cvCreateCameraCapture(0); // Use the default camera
  pthread_t mythread;
  IplImage*     frame = 0;

  if ( pthread_create( &mythread, NULL, thread_function, NULL) ) 
  {
    printf("error creating thread.");
    abort();
  }

  if ( pthread_join ( mythread, NULL ) ) 
  {
    printf("error joining thread.");
    abort();
  }


  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
//  frame = cvQueryFrame(camera);

  if (frame != NULL) {
    printf("Frame extracted from CAM1\n\r");
        cvSaveImage("/home/root/Desktop/BBTCP/webcam1.jpg", frame,0);
  } else {
      printf("Null frame 1\n\r");
  }
  cvReleaseCapture(&camera);
  cvReleaseImage(&frame);
  return 0;
}

最佳答案

代码或系统设计没有错误,但问题出在嵌入式系统本身的DMA模块上。 DMA 批量传输损坏导致此错误。所以不存在软件问题。

关于c - 在系统函数中调用 OpenCV 捕获代码时卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12968415/

相关文章:

c++ - 如何将 libusb 与 libevent 一起使用?

c - 多线程程序中线程的执行顺序

c - 如何将开关箱内的箱子软分隔

python - Heroku 和 OpenCV 与 Python

c++ - 图像搜索中的 OpenCV 图像 (C++)

clCreateImage2D - 加载 RGB 图像

c - 将 argv[1] 作为参数传递给 pthread_create。

C++11:带有 std::thread 和 lambda 函数的段错误

c - strncmp 无法正常工作

C - 控制台窗口中的彩色字符 (Linux)