opencv框架差异未处理的异常

时间:2015-12-02 15:17:32

标签: c++ opencv

我尝试使用以下代码进行帧差异。当我运行它时它只显示第一帧和崩溃。你能帮忙看看为什么会这样吗

#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错误:输入参数的大小不匹配(操作既不是&#39;数组操作数组&#39;(其中数组具有相同的大小和相同的通道数),也不是&#39;数组操作cv :: arithm_op中的标量&#39;,&#39; scalar op array&#39;),文件C:\ builds \ 2_4_PackSlave-win64-vc12-shared \ opencv \ modules \ core \ src \ arithm.cpp,line 1287

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

在第二帧中会出现此问题: frameCurrent是RGB(3通道),而framePrev是灰色(1通道)。您可以调试并确保。 解决它: 改变:

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

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

答案 2 :(得分:0)

我已根据您的建议将代码更改为以下内容,当我运行时,它没有显示差异的结果

imshow(“frameAbsDiff”,frameAbsDiff);

它只是黑屏

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


}
}