opencv帧差异未处理的异常

opencv frame difference Unhandled exception

本文关键字:异常 未处理 opencv      更新时间:2023-10-16

我正在尝试用下面的代码进行帧差异处理。当我运行它时,它只显示第一帧并崩溃。你能帮我看看为什么会这样吗

#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错误:输入参数的大小不匹配(操作既不是"array op array"(其中数组具有相同的大小和相同的通道数),也不是"array op scalar"或"scalar op array’"),位于cv::numeric_op,文件C:\builds\2_4_PackSlave-win64-vc12-shared\OpenCV\modules\core\src\anumeric.cpp,第1287行

看起来应该在最后一个cap >> frameCurrent;之后添加cvtColor(frameCurrent, frameCurrent, CV_BGR2GRAY);。由于使用CV_BGR2GRAY使用的是单通道图像,因此需要连贯并在所有帧中保持使用,否则将尝试在3通道图像和单通道图像之间应用减法。

在第二帧中,将出现此问题:CCD_ 4是RGB(3通道),而CCD_。您可以调试并确保。要解决此问题:更改:

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

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

根据您的建议,我将代码更改为以下代码,当我运行时,它不会显示差异的结果

imshow("frameAbsDiff",frameAbsDdiff);

只是黑屏

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

}
}