帧间差分法python代码_帧间差分法运动检测代码

#include "StdAfx.h"

#include &ltcv.h&gt

#include &ltopencv2/opencv.hpp&gt

#include &ltcstdio&gt

#include &ltcstdlib&gt

#include &ltWindows.h&gt

#include "highgui.h"

#include "math.h"

using namespace std;

using namespace cv;

int W=600;

int H=400;

int main (int argc, char** argv)

{

VideoCapture capture("tree.avi");

Mat frame;

Mat frame_resize;

//Mat frame_r_Gray;

Mat average;

Mat background,foreground,foreground_BW;

//---------------------------------------------------------------------

//获取视频的宽度、高度、帧率、总的帧数

CvCapture* pCapture = NULL;

pCapture = cvCaptureFromAVI("tree.avi");

int frameH = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_HEIGHT);

int frameW = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_WIDTH);

int fps = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FPS);

int numFrames = (int) cvGetCaptureProperty(pCapture, CV_CAP_PROP_FRAME_COUNT);

int num=numFrames;

printf("vedio's \nwidth = %d\t height = %d\n video's fps = %d\t nums = %d", frameW, frameH, fps, numFrames);

//---------------------------------------------------------------------

Mat frame_r_Gray;

Mat frame_0,frame_1;//Mat m(3, 5, CV_32FC1, 1);

//---------------------------------------------------------------------

while(num)

{

capture&gt&gtframe;

if(!capture.read(frame)) break;

resize(frame, frame_resize, Size(W,H));

imshow("frame_resize",frame_resize);

cvtColor( frame_resize,frame_r_Gray, CV_RGB2GRAY );

//imshow("frame_resize_Gray",frame_r_Gray);

//-----------------------------------------------------------------------------------

//选择前一帧作为背景(读入第一帧时,第一帧作为背景)

if(num==numFrames)

{

background=frame_r_Gray.clone();

frame_0=background;

}

else

{

background=frame_0;

}

//------------------------------------------------------------------------------------

absdiff(frame_r_Gray,background,foreground);//用帧差法求前景

imshow("foreground",foreground);

threshold( foreground, foreground_BW, 50, 255 , 0 );//二值化

imshow("foreground_BW",foreground_BW);

frame_0=frame_r_Gray.clone();

num--;

char c = waitKey(33);

if( c ==27 ) break;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值