Chinaunix首页 | 论坛 | 博客
  • 博客访问: 196459
  • 博文数量: 53
  • 博客积分: 2015
  • 博客等级: 上尉
  • 技术积分: 546
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-04 23:28
文章分类

全部博文(53)

文章存档

2012年(4)

2011年(8)

2010年(19)

2009年(22)

我的朋友

分类: C/C++

2010-01-22 10:51:18

 

#include <highgui.h>
#include <cv.h>

int main(int argc, char **argv)
{
    //新建窗口
    cvNamedWindow("origin", CV_WINDOW_AUTOSIZE);
    cvNamedWindow("processing", CV_WINDOW_AUTOSIZE);

    CvCapture *capture = NULL;

    //读取视频文件
    if (argc == 1)
    {
        //从摄像头读入
        capture = cvCreateCameraCapture(0);
    }
    else
    {
        //从视频文件读入
        capture = cvCreateFileCapture(argv[1]);
    }

    //原始图像和处理后图像
    IplImage *frame = NULL;
    IplImage *pro_frame = NULL;

    //原始像素值和处理的像素值
    CvScalar pixel = {0};
    CvScalar pro_pixel = {0};

    //pro_frame初始化
    pro_frame = cvCreateImage(
                    cvSize(
                        (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
                        (int) cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT)
                    ),
                    IPL_DEPTH_8U,
                    3
                );

    //当前视频帧数
    int sum = 1;

    while (cvWaitKey(33) != 27)        //按ESC键退出

    {
        frame = cvQueryFrame(capture);

        //视频结束退出
        if (!frame)
        {
            break;
        }

        //以第一帧初始化背景
        if (sum == 1)
        {
            for (int y = 0; y < frame->height; ++y)
            {
                for (int x = 0; x < frame->width; ++x)
                {
                    //获取像素值
                    pixel = cvGet2D(frame, y, x);

                    //像素值处理
                    pro_pixel.val[0] = pixel.val[0];
                    pro_pixel.val[1] = pixel.val[1];
                    pro_pixel.val[2] = pixel.val[2];

                    //写入像素值到pro_frame
                    cvSet2D(pro_frame, y, x, pro_pixel);
                }
            }
            sum++;
        }
        //背景更新
        else
        {
            for (int y = 0; y < frame->height; ++y)
            {
                for (int x = 0; x < frame->width; ++x)
                {
                    //获取像素值
                    pixel = cvGet2D(frame, y, x);
                    pro_pixel = cvGet2D(pro_frame, y, x);

                    //alpha取0.05, 更新各像素期望
                    pro_pixel.val[0] = 0.95 * pro_pixel.val[0] + 0.05 * pixel.val[0];
                    pro_pixel.val[1] = 0.95 * pro_pixel.val[1] + 0.05 * pixel.val[1];
                    pro_pixel.val[2] = 0.95 * pro_pixel.val[2] + 0.05 * pixel.val[2];

                    //写入pro_frame
                    cvSet2D(pro_frame, y, x, pro_pixel);
                }
            }

        }

        //显示原始帧和处理后的帧
        cvShowImage("origin", frame);
        cvShowImage("processing", pro_frame);
    }

    //释放内存
    cvReleaseCapture(&capture);
    cvReleaseImage(&pro_frame);
    cvDestroyWindow("origin");
    cvDestroyWindow("processing");

    return 0;
}


阅读(783) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~