我的软件环境是Ubuntu 10.04,opencv2.0.0
源代码:
#include<stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include<time.h>
#include<stdlib.h>
#include<cv.h>
#include<cxcore.h>
#include<highgui.h>
int main( int argc,char ** argv )
{
//声明IplImage指针
IplImage* pFrame=NULL;
IplImage* pSaveFrame=NULL;
CvCapture* pCapture=NULL;
static char filename[40];
struct tm * tm_ptr;
time_t the_time;
//创建窗口
cvNamedWindow("video",CV_WINDOW_AUTOSIZE);
pCapture=cvCaptureFromCAM(-1);
//打开摄像头
if( !pCapture )
{
fprintf(stderr,"Can not open camera.\n");
return -1;
}
if(mkdir("./Robert",0755)==-1)
{
//创建时存在该目录会返回错误码,如不存在则创建它,但由于我们需要该目录,则出错也不处理
}
//逐帧读取视频
while(pFrame=cvQueryFrame(pCapture))
{
cvShowImage("video",pFrame);
if(cvWaitKey(10)>=0) break; //显示视频,若参数为0,只显示第一帧 如果你敲了键盘,就退出程序,否则继续捕获下一帧
//cvWaitKey(0); 显示图像
pSaveFrame=cvCreateImage(cvSize(pFrame->width,pFrame->height),pFrame->depth,pFrame->nChannels);
//get file name
(void) time(&the_time);
tm_ptr=localtime(&the_time);
snprintf(filename, 40, "./Robert/%04d%02d%02d%02d%02d%02d.bmp",tm_ptr->tm_year-100+2000, tm_ptr->tm_mon + 1, tm_ptr->tm_mday, tm_ptr->tm_hour,tm_ptr->tm_min, tm_ptr->tm_sec);
cvResize(pFrame,pSaveFrame,CV_INTER_LINEAR); //为了让所有输入图像同样大小,需要对图像进行缩放.CV_INTER_LINEAR双线性插值
cvSaveImage(filename,pSaveFrame);
//cvReleaseImage( &pFrame );
cvReleaseImage(&pSaveFrame);
}
cvDestroyWindow("video");
cvReleaseCapture(&pCapture);
printf("process ok!\n");
return 0;
}
编译没问题。
但运行时,老出现Corrupt JPEG data: premature end of data segment。
望牛人解答。。。。。
翘首等待。。。。。。。。。。。。。