zoukankan      html  css  js  c++  java
  • 【Opencv学习】摄像头采集、录像、截图小工具

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/

    /*
    * =====================================================================================
    *
    * Filename: videocapture.cpp
    *
    * Description: A Program of Capture the video Camrea 本程序可以进行摄像头采集、录像(按键 r)和截图(鼠标左键)
    *
    * Version: 1.0
    * Created: 02/02/2009 09:48:27 AM
    * Revision: none
    * Compiler: gcc
    *
    * Author: Futuredaemon (BUPT), gnuhpc@gmail.com
    * Company: BUPT_UNITED
    *
    * =====================================================================================
    */
    
    #include "cv.h"
    #include "highgui.h"
    #include 
    #include 
    #include 
    
    IplImage *frame=0;
    
    void mouseHandler(int event,int x,int y,int flags,void *param);
    
    int main(int argc , char **argv)
    {
    CvCapture *capture = 0;
    
    int flag= 1;
    
    int iscolor=1;
    int fps=15;
    int frameW=320;
    int frameH=240;
    int press=0;
    CvVideoWriter *writer=cvCreateVideoWriter("out.avi",CV_FOURCC('D','I','V','X'),
    fps,cvSize(frameW,frameH),iscolor);
    
    if (argc==1|| (argc==2 && strlen(argv[1])==1 && isdigit(argv[1][0])))
    {
    capture = cvCaptureFromCAM(-1);
    }
    else if(argc==2)
    {
    capture = cvCaptureFromAVI(argv[1]);
    }
    
    if (!capture)
    {
    fprintf(stderr,"Could not initialize capturing.../n");
    return -1;
    }
    
    cvNamedWindow("main",0);
    
    cvSetMouseCallback("main",mouseHandler,NULL);
    for (;;)
    {
    
    frame = cvQueryFrame(capture);
    if (!frame)
    {
    break;
    }
    
    if( flag==0 )
    {
    cvWriteFrame(writer,frame);
    }
    
    cvShowImage("main",frame);
    
    if ((press=cvWaitKey(20))=='r')
    {
    if( flag==1 )
    {
    printf("Starting Record the Video!/n");
    flag = 0;
    } else
    {
    printf("Stop the recording!/n");
    flag = 1;
    }
    }
    else if(press==27)
    {
    break;
    }
    
    }
    
    cvReleaseCapture(&capture);
    cvDestroyWindow("main");
    return 0;
    }
    
    void mouseHandler(int event,int x,int y,int flags,void *param)
    {
    time_t curtime=time(NULL);
    char *date=ctime(&curtime);
    
    switch ( event )
    {
    case CV_EVENT_LBUTTONDOWN :
    printf("Get a picrure named %s/n",date+11);
    sprintf(date,"%s.jpg",date+11);
    cvSaveImage(date,frame);
    break;
    default :
    break;
    }
    }
    
    

    作者:gnuhpc
    出处:http://www.cnblogs.com/gnuhpc/


                   作者:gnuhpc
                   出处:http://www.cnblogs.com/gnuhpc/
                   除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


    分享到:

  • 相关阅读:
    JZOJ 3034. 【NOIP2012模拟10.17】独立集
    JZOJ 3035. 【NOIP2012模拟10.17】铁轨
    JZOJ 1259. 牛棚安排
    数位DP JZOJ 3316. 非回文数字
    JZOJ 3046. 游戏
    JZOJ 3013. 填充棋盘
    debian 安装oracle提供的java8
    java 汉字转拼音 PinYin4j
    debian ssh设置root权限登陆 Permission denied, please try again
    java并发下订单生成策略
  • 原文地址:https://www.cnblogs.com/gnuhpc/p/2810093.html
Copyright © 2011-2022 走看看