|
#include <QtCore/QCoreApplication>
#include "highgui.h"
#include <qfuture.h>
#include <qtconcurrentrun.h>
#include <qdatetime.h>
CvCapture *capture_left,*capture_right;
IplImage *frame_left,*frame_right;
CvVideoWriter *writer_left;
CvVideoWriter *writer_right;
bool grabImage(int cameraidx)
{
//#ifdef OPENCV_CAPTURE
if (cameraidx == 0)
{
// while(cvGrabFrame(capture_left) == 0) {}
double t=(double)clock()/1000;
bool ret;
ret=cvGrabFrame(capture_left);
t=(double)clock()/1000 - t;
if (t>50)
printf("Left waited %f ms\n",t);
return ret;
}
if (cameraidx == 1)
{
//while(cvGrabFrame(capture_right)== 0) {}
double t=(double)clock()/1000;
bool ret;
ret=cvGrabFrame(capture_right);
t=(double)clock()/1000 - t;
if (t>50)
printf("Right waited %f ms\n",t);
return ret;
}
//#endif
return true;
}//grabImage
void clean()
{
cvReleaseCapture(&capture_left);
cvReleaseCapture(&capture_right);
//cvRelease();
cvDestroyAllWindows();
}
int main(int argc, char *argv[])
{
//provides event loop
// QCoreApplication a(argc, argv);
// a.exec();
CvSize imageSize;
imageSize.width=640;
imageSize.height=480;
// capture_left = cvCaptureFromCAM(CV_CAP_IEEE1394 + 0);
// capture_right = cvCaptureFromCAM(CV_CAP_IEEE1394 + 1);
// cvReleaseCapture(&capture_left);
// cvReleaseCapture(&capture_right);
capture_left = cvCaptureFromCAM(CV_CAP_IEEE1394 + 0);
capture_right = cvCaptureFromCAM(CV_CAP_IEEE1394 + 1);
cvSetCaptureProperty(capture_left ,CV_CAP_PROP_FRAME_WIDTH,imageSize.width);
cvSetCaptureProperty(capture_left ,CV_CAP_PROP_FRAME_HEIGHT,imageSize.height);
//cvSetCaptureProperty(capture_left ,CV_CAP_PROP_FPS,3.75); //-- fps does not work for V4L2, works for FireWire
cvSetCaptureProperty(capture_right ,CV_CAP_PROP_FRAME_WIDTH,imageSize.width);
cvSetCaptureProperty(capture_right ,CV_CAP_PROP_FRAME_HEIGHT,imageSize.height);
cvNamedWindow( "left");
cvNamedWindow( "right");
QFuture<bool> bleft=QtConcurrent::run(grabImage,0);
QFuture<bool> bright=QtConcurrent::run(grabImage,1);
// bool bleft=cvGrabFrame(capture_left);
// bool bright=cvGrabFrame(capture_right);
if (bleft)
frame_left = cvRetrieveFrame(capture_left);
if (bright)
frame_right = cvRetrieveFrame(capture_right);
QTime time = QTime::currentTime();
QString timeString = time.toString();
QString left=timeString+"left.yuv", right=timeString+"right.yuv";
// if (writer_left != NULL)
// cvReleaseVideoWriter(&writer_left);
// if (writer_right != NULL)
// cvReleaseVideoWriter(&writer_right);
writer_left = cvCreateVideoWriter(left.toAscii(),CV_FOURCC('I','4','2','0'),15,imageSize);
writer_right = cvCreateVideoWriter(right.toAscii(),CV_FOURCC('I','4','2','0'),15,imageSize);
char key;
long count_l=0,count_r=0;
for (;;)
{
if ((key=(char)cvWaitKey(5))!=-1)
{ clean(); exit(0); }
// printf("Grabbing... ");
bool bgrab_left = cvGrabFrame(capture_left);
bool bgrab_right = cvGrabFrame(capture_right);
//if (bgrab_left || bgrab_right)
if (bgrab_left || bgrab_right)
{
double t=(double)clock();//CLOCKS_PER_SEC
if (bgrab_left)
{
// printf("Grabbed LEFT");
count_l=0;
frame_left = cvRetrieveFrame(capture_left);
cvShowImage("left",frame_left);
cvWriteFrame(writer_left,frame_left);
}
if (bgrab_right)
{
// printf("Grabbed RIGHT");
count_r=0;
frame_right = cvRetrieveFrame(capture_right);
cvShowImage("right",frame_right);
cvWriteFrame(writer_right,frame_right);
}
printf("%s%d\n","CPU time used for storage: ",((double)clock()-t)/1000);
// printf(", missed %d %d",count_l,count_r);
// printf(" DONE\n");
}
//else
{
// printf("%s","Two frames are missing\n\n\n");
// exit(1);
// if (!bgrab_left) count_l++;//printf("%s","no LEFT");
// if (!bgrab_right) count_r++;//printf("%s","no RIGHT");
// //printf("\n");
// continue;
// //exit(1);
}
}//for
clean();
return 0;
}
|