2
我正在OpenCV中開發應用程序。我正在拍攝照相機的快照,然後我正在關閉拍攝。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;
}
我真的不知道,但你執行這個程序在另一個程序嵌入?如果stdout或errout緩衝區已滿並且未由*執行*應用程序執行,則此程序可能會阻止。 – rekire