运动目标检测(帧间差分法)——opencv

#include
#include

using namespace std;
using namespace cv;

int main()
{
    VideoCapture capture("C:\\Users\\aoe\\Desktop\\avi\\walk.avi");//获取视频
    if (!capture.isOpened())
        return -1;
    double rate = capture.get(CV_CAP_PROP_FPS);//获取视频帧率
    int delay = 1000 / rate;
    Mat framepro, frame, dframe;
    bool flag = false;
    namedWindow("image");
    namedWindow("test");
    while (capture.read(frame))
    {
        if (false == flag)
        {
            framepro = frame.clone();//将第一帧图像拷贝给framePro
            flag = true;
        }
        else
        {
            absdiff(frame, framepro, dframe);//帧间差分计算两幅图像各个通道的相对应元素的差的绝对值。
            framepro = frame.clone();//将当前帧拷贝给framepro
            threshold(dframe, dframe, 80, 255, CV_THRESH_BINARY);//阈值分割
            imshow("image", frame);
            imshow("test", dframe);
            waitKey(delay);
        }
    }
    waitKey();
    return 0;
}

opencv对视频的处理参考:
http://www.cnblogs.com/ronny/p/opencv_road_10.html

你可能感兴趣的:(opencv)