使用 OpenCV 进行运动检测

概览

最近在做一个项目的时候需要检测摄像头拍摄范围内的图像是否有变化,即需要进行运动检测,在检测到运动之后录制 5 秒的视频存档。
在查阅了一些资料之后,发现了一种效果不错的方法(原始文章见参考内容 1),该方法只进行运动检测,不进行追踪。该方法的主要思路就是计算连续帧之间的差距,如果该差距较大,我们就认为画面的内容发生了变化。其中为了避免假阳性,该方法计算了帧差的标准差。
算法的主要流程如下:

  1. 计算帧间的距离(Pythagorean distance)
  2. 在计算出来的距离矩阵上应用高斯模糊
  3. 使用一定的阈值进行过滤
  4. 计算标准差
  5. 标准差大于一定的阈值,认为检测到了运动,输出信息

本文将给出 PythonC++ 两种语言的实现。

运行环境

操作系统:ARMBIAN 5.38 stable Ubuntu 16.04.3 LTS 3.4.113-sun8i
编程环境:Python 2.7.12, OpenCV 3.3.0

Python 实现

import numpy as np
import cv2

sdThresh = 10
font = cv2.FONT_HERSHEY_SIMPLEX

def distMap(frame1, frame2):
    """outputs pythagorean distance between two frames"""
    frame1_32 = np.float32(frame1)
    frame2_32 = np.float32(frame2)
    diff32 = frame1_32 - frame2_32
    norm32 = np.sqrt(diff32[:,:,0]**2 + diff32[:,:,1]**2 + \
             diff32[:,:,2]**2)/np.sqrt(255**2 + 255**2 + 255**2)
    dist = np.uint8(norm32*255)
    return dist

cv2.namedWindow('frame')
cv2.namedWindow('dist')

#capture video stream from camera source. 0 refers to first camera, 1 referes to 2nd and so on.
cap = cv2.VideoCapture(0)

_, frame1 = cap.read()
_, frame2 = cap.read()

facecount = 0
while(True):
    _, frame3 = cap.read()
    rows, cols, _ = np.shape(frame3)
    cv2.imshow('dist', frame3)
    dist = distMap(frame1, frame3)

    frame1 = frame2
    frame2 = frame3

    # apply Gaussian smoothing
    mod = cv2.GaussianBlur(dist, (9,9), 0)

    # apply thresholding
    _, thresh = cv2.threshold(mod, 100, 255, 0)

    # calculate st dev test
    _, stDev = cv2.meanStdDev(mod)

    cv2.imshow('dist', mod)
    cv2.putText(frame2, "Standard Deviation - {}".format(round(stDev[0][0],0)), \
                (70, 70), font, 1, (255, 0, 255), 1, cv2.LINE_AA)
    if stDev > sdThresh:
            print("Motion detected.. Do something!!!");

    cv2.imshow('frame', frame2)
    if cv2.waitKey(1) & 0xFF == 27:
        break

cap.release()
cv2.destroyAllWindows()

C++ 实现

复制下面的代码,保存为 motion.cpp,运行下面的编译命令即可

g++ motion.cpp -o motion `pkg-config --cflags --libs opencv`
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include"omp.h"

using namespace cv;
#define MOTION_THRESH 10

void distMap(const Mat &a, const Mat &b, Mat &out){
    #pragma omp parallel
    for(int y = 0; y < a.rows; ++y){
        for(int x = 0; x < a.cols; ++x){
            int summary = 0;
            for(int z = 0; z < a.dims; ++z){
                summary += pow(a.at<Vec3b>(y, x)[z] - b.at<Vec3b>(y, x)[z], 2);
            }
            out.at<uchar>(y, x) = (int) (255.0 * sqrt(summary) / sqrt(3 * pow(255, 2)));
        }
    }
}


int main(int argc, char **argv){
    printf("=========== initializing ==========\n");

    int device = 0;
    int motion_count = 0;

    if(argc == 2) {
        device = atoi(argv[1]);
    }

    printf("device id %d\n", device);

    VideoCapture myCamera(device);

    myCamera.set(CV_CAP_PROP_FRAME_WIDTH, 640);
    myCamera.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

    if (!myCamera.isOpened()){
        printf("cannot open camera\n");
        return 0;
    }else{
        printf("%s\n", "===========start camera==========");
    }

    Mat preVideoFrame, curVideoFrame, nextVideoFrame;

    for(int i = 0; i < 5; i++) {
        myCamera >> preVideoFrame;
    }

    myCamera >> preVideoFrame;
    myCamera >> curVideoFrame;


    Mat dist(preVideoFrame.rows, preVideoFrame.cols, CV_8UC1);
    Mat blurDist(preVideoFrame.rows, preVideoFrame.cols, CV_8UC1);
    Mat threDist(preVideoFrame.rows, preVideoFrame.cols, CV_8UC1);
    Mat meanDist(preVideoFrame.rows, preVideoFrame.cols, CV_8UC1);
    Mat stdDist(1, 1, CV_8UC1);

    char text[100] = {0};

    int key = 0;
    while (key != 27){
        myCamera >> nextVideoFrame;

        // key = waitKey(1);
        // continue;

        if (!preVideoFrame.empty() && !curVideoFrame.empty() && !nextVideoFrame.empty()){
            distMap(preVideoFrame, nextVideoFrame, dist);

            curVideoFrame.copyTo(preVideoFrame);
            nextVideoFrame.copyTo(curVideoFrame);

            blur(dist, blurDist, Size(3, 3));

            threshold(blurDist, threDist, 100, 255, THRESH_BINARY);

            meanStdDev(threDist, meanDist, stdDist);

            sprintf(text, "Standard Deviation - %d", stdDist.at<uchar>(0, 0));

            cv::putText(curVideoFrame, text, Point(70, 70), FONT_HERSHEY_SIMPLEX, 
                        1, (255, 0, 255), 1, LINE_AA);

            if(stdDist.at<uchar>(0, 0) > MOTION_THRESH) {
                printf("%d motion detected\r", stdDist.at<uchar>(0, 0));
                fflush(stdout);
            }

            imshow("distMap", blurDist);
            imshow("preview", curVideoFrame);

        }

        key = waitKey(1);
    }

    myCamera.release();
    printf("%s\n", "===========release camera==========");

    return 0;
}

参考内容

  1. Motion detection using OpenCV
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值