opencv之视频中行人监控(5)

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>

using namespace cv;
void onTrackerSlid(Mat &inputimage1, Mat &inputimage2, Mat &outputimage, int pos);

int main()
{
    VideoCapture videoCap("1.avi");
    if(!videoCap.isOpened())
    {
        return -1;
    }
    double videoFPS = videoCap.get(CV_CAP_PROP_FPS);  //获取帧率
    double videoPause = 1000/videoFPS;
    Mat framePrePre; //上上一帧
    Mat framePre; //上一帧
    Mat frameNow; //当前帧
    Mat frameDet; //运动物体
    videoCap>>framePrePre;
    videoCap>>framePre;
    cvtColor(framePrePre, framePrePre, CV_RGB2GRAY);
    cvtColor(framePre, framePre, CV_RGB2GRAY);
    while(true)
    {
        videoCap>>frameNow;
        if(frameNow.empty()||waitKey(videoPause) == 27)
        {
            break;
        }
        cvtColor(frameNow, frameNow, CV_RGB2GRAY);
        Mat Det1;
        Mat Det2;
          /*
        absdiff(framePrePre, framePre, Det1);  //帧差1
        absdiff(framePre, frameNow, Det2);     //帧差2
        threshold(Det1, Det1, 0, 255, CV_THRESH_OTSU);  //自适应阈值化
        threshold(Det2, Det2, 0, 255, CV_THRESH_OTSU);
       */
         // /*
        onTrackerSlid(framePrePre, framePre, Det1, 50);          //两个效果差不多
        onTrackerSlid(framePre, frameNow, Det2, 50);
         // */
        //Mat element = getStructuringElement(0, Size(3,3));  //膨胀核
        //dilate(Det1, Det1, element);    //膨胀
        //dilate(Det2, Det2, element);
        bitwise_and(Det1, Det2, frameDet);
        //addWeighted(Det1, 1,  Det2, 1, 0, frameDet);
        framePrePre = framePre;
        framePre = frameNow;
        imshow("Video", frameNow);
        imshow("Detection", frameDet);
    }
    return 0;
}

void onTrackerSlid(Mat &inputimage1, Mat &inputimage2, Mat &outputimage, int pos)
{
    uchar *data1 = NULL;
    uchar *data2 = NULL;
    uchar *data3 = NULL;
    int i, j;

    outputimage = inputimage1.clone();
    int rowNumber = outputimage.rows;
    int colNumber = outputimage.cols*outputimage.channels();
    int step = outputimage.step/sizeof(uchar);
    data1 = (uchar*)inputimage1.data;
    data2 = (uchar*)inputimage2.data;
    data3 = (uchar*)outputimage.data;

    for(i = 0; i < rowNumber; i++)
    {
        for(j = 0; j < colNumber; j++)
        {
            if(fabs(data2[i*step + j] - data1[i*step + j]) > pos)
                data3[i*step + j] = 255;
            else
                data3[i*step + j] = 0;
        }
    }
}

1.avi:链接:https://pan.baidu.com/s/1zBSP1WDZuQqjRRAkL3yBkQ 密码:y0bc

结果

猜你喜欢

转载自blog.csdn.net/juliarjuliar/article/details/79812950