Home > front end >  OpenCV C absdiff() unhandled exception
OpenCV C absdiff() unhandled exception

Time:10-19

I tried finding the frame difference between a still image and a frame taken from my webcam, using absdiff(). My code is as follows:

#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);
}

Upon running it, I ran into an error, and I could not resolve it.

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

Was there something I missed? Your help is much appreciated!

CodePudding user response:

I figured out the solution to this problem. In short, it was only a small resolution issue, which could be fixed by matching the resolution of img with web.

Here are the two lines of code that seemed to resolve the issue if anyone is interested :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); 
  • Related