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;
}