OpenCV cpp 运动检测

OpenCV cpp motion detection

我想弄清楚 opencv 中的运动检测是如何工作的。

我可以在那里看到视频分析参考,但我没有找到足够的信息来说明它是如何使用的。

我也看到一些人使用 absdiff 我这样试过,但它在 memore 错误时给了我一个例外

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'a rray op array' (where arrays have the same size and the same number of channels) , nor 'array op scalar', nor 'scalar op array') in cv::arithm_op, file C:\builds _4_PackSlave-win32-vc12-shared\opencv\modules\core\src\arithm.cpp, line 1287

密码是

#include <iostream>
#include <sstream>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main()
{
    //create matrix for storage
    Mat image;
    Mat image2;
    Mat image3;
    Mat image4;

    //initialize capture
    VideoCapture cap;
    cap.open(0);

    //create window to show image
    namedWindow("window", 1);

    while (1){

        //copy webcam stream to image
        cap >> image;
        cap >> image2;

        absdiff(image, image2, image3);
        threshold(image3, image4, 128, 255, THRESH_BINARY);


        //print image to screen
        if (!image.empty()) {

            imshow("window", image3);

        }

        //delay33ms

        waitKey(10);

        //
    }

}

我显然没有正确使用它

使用图片前需要先确认VideoCapture成功。此外,您还想在使用之前测试图像是否已成功捕获。试试这个:

VideoCapture cap(0);

if(!cap.isOpened()) {
  std::cerr << "Failed to open video capture" << std::endl;
  return -1;
}

namedWindow("window");

while(true) {
    cap >> image;
    cap >> image2;

    if(image.empty() || image2.empty()) {
        std::cerr << "failed to capture images\n";
        return -1;
    }

    absdiff(image, image2, image3);
    threshold(image3, image4, 128, 255, THRESH_BINARY);

    imshow("window", image);
    int k = waitKey(30) & 0xff;
    if('q' == k || 27 == k)
       break;

}