我正在用 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;
}
我从 system(./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;
}