OpenCV C++ absdiff() 未处理的异常

OpenCV C++ absdiff() unhandled exception

我尝试使用 absdiff() 查找静止图像和从我的网络摄像头拍摄的帧之间的帧差异。我的代码如下:

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

using namespace std;
using namespace cv;

int main() {
    VideoCapture webcam(0);
    Mat img, web, diff;
    
    webcam >> web;
    img = imread("/Users/dnxv2/Desktop/test.jpg");

    cvtColor(web, web, COLOR_BGR2GRAY);
    cvtColor(img, img, COLOR_BGR2GRAY);

    absdiff(img, web, diff);
    
    imshow("test", diff);
    waitKey(0);
}

在 运行 之后,我 运行 遇到错误,我无法解决它。

Unhandled exception at 0x00007FF937E24F99 in test.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000FA29CFE1B0.

有什么我错过的吗?非常感谢您的帮助!

我找到了解决这个问题的方法。简而言之,这只是一个小分辨率问题,可以通过将 img 的分辨率与 web.

的分辨率进行匹配来解决。

如果有人感兴趣,这里有两行代码似乎可以解决问题:D

int resx = 500, resy = 500; // 500x400 resolution
resize(img, img, { resx, resy }, 0, 0, cv::INTER_NEAREST);
resize(web, web, { resx, resy }, 0, 0, cv::INTER_NEAREST);