简体   繁体   中英

opencv frame difference Unhandled exception

I'm trying to do frame difference with below code. when I run it it show only first frame and crashes . can you help to see why this is happening

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>


using namespace std;
using namespace cv;

int main()

{
cv::Mat frameCurrent, framePrev;
cv::Mat  frameAbsDiff=;
//prepare Mats
VideoCapture cap("e.mp4");

cap >> frameCurrent;

framePrev = cv::Mat::zeros(frameCurrent.size(), frameCurrent.type());

cvtColor(frameCurrent, frameCurrent, CV_BGR2GRAY);


frameCurrent.copyTo(framePrev);

while (1)
{
    if (frameCurrent.empty()) {
        std::cout << "Frame1Message->End of sequence" << std::endl;
        break;
    }

    cv::absdiff(frameCurrent, framePrev, frameAbsDiff);

    imshow("frameCurrent", frameCurrent);
    imshow("frameAbsDiff", frameAbsDiff);


    if (waitKey(90) == 27)
        break;

    frameCurrent.copyTo(framePrev);
    cap >> frameCurrent;
}
}

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array 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\\2_4_PackSlave-win64-vc12-shared\\opencv\\modules\\core\\src\\arithm.cpp, line 1287

It looks like you should add cvtColor(frameCurrent, frameCurrent, CV_BGR2GRAY); after the last cap >> frameCurrent; . Since you are using a single channel image by using CV_BGR2GRAY you need to be coherent and keep using it in all the frames, otherwise you will be trying to apply the subtraction between a 3 channel image and a single channel one.

In the second frame this problem will happen : frameCurrent is RGB(3-Channel) while framePrev is Gray(1 Channel). You may debug and make sure. To solve it: change :

  frameCurrent.copyTo(framePrev);
  cap >> frameCurrent;

to

  frameCurrent.copyTo(framePrev);
  cap >> frameCurrent;
  cvtColor(frameCurrent, frameCurrent, CV_BGR2GRAY);

I have changed the code to the following according to your suggestions , when I run , it doesn't show the result of difference in

imshow("frameAbsDiff", frameAbsDiff);

it is just black screen

int main()

{
   cv::Mat frameCurrent, framePrev;
   cv::Mat  frameAbsDiff;
//prepare Mats

VideoCapture cap("m.mp4");
cap >> frameCurrent;
cvtColor(frameCurrent, frameCurrent, CV_BGR2GRAY);

frameCurrent.copyTo(framePrev);

while (1)
{
    if (frameCurrent.empty()) {
        std::cout << "Frame1Message->End of sequence" << std::endl;
        break;
    }

    cv::absdiff(frameCurrent, framePrev, frameAbsDiff);

    imshow("frameCurrent", frameCurrent);
    imshow("frameAbsDiff", frameAbsDiff);


    if (waitKey(90) == 27)
        break;

    cap >> frameCurrent;

    cvtColor(frameCurrent, frameCurrent, CV_BGR2GRAY);
    frameCurrent.copyTo(framePrev);


}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM