使用OpenCV实现WebCam摄像头保存JPEG图片(改良1版)

来源:互联网 发布:php论坛手机源码 编辑:程序博客网 时间:2024/06/05 18:31

http://blog.sina.com.cn/s/blog_3e4774e30100frwi.html

 

     本次的代码(OpenCV是1.0的)从上次实现从电脑上的摄像头捕捉视频并一秒保存一张图片的效果出发,增加了可以修改保存的图片的像素功能,代码如下:

//使用OpenCV实现获取摄像头数据并1秒保存成一张jpg图片(保存的图片的大小被缩小了),jpg图片的大小可调,文件名为test.c//编译方法为:g++ `pkg-config opencv --libs --cflags opencv` test.c -o test#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",1);    //打开摄像头   if( !(pCapture=cvCaptureFromCAM(-1)) )   {       fprintf(stderr,"Can not open camera.\n");       return -1;   }    if(mkdir("./Alert",0755)==-1)    {         //创建时存在该目录会返回错误码,如不存在则创建它,但由于我们需要该目录,则出错也不处理    }   //逐帧读取视频   while(pFrame=cvQueryFrame(pCapture))   {          cvShowImage("video",pFrame);          if(cvWaitKey(2)>=0) break;          pSaveFrame=cvCreateImage(cvSize(320,240),pFrame->depth,pFrame->nChannels);          //get file name          (void) time(&the_time);          tm_ptr=localtime(&the_time);          snprintf(filename, 40, "./Alert/%04d%02d%02d%02d%02d%02d.jpg",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);           cvSaveImage(filename,pSaveFrame);           cvReleaseImage(&pSaveFrame);   }   cvDestroyWindow("video");   cvReleaseCapture(&pCapture);   return 0;}   



 

原创粉丝点击