【OpenCV学习】摄像头显示、录像、拍照程序

本文介绍了一个使用OpenCV库实现的简单视频捕获程序。该程序可以从摄像头或视频文件中捕获视频帧,并提供了录像功能及鼠标事件触发截图的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

/*
 * =====================================================================================
 *
 *       Filename:  videocapture.cpp
 *
 *    Description:  A Program of Capture the video Camrea
 *
 *        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 <stdio.h>
#include <ctype.h>
#include  <time.h>

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/

转载于:https://www.cnblogs.com/gnuhpc/archive/2012/12/07/2806794.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值