【问题标题】:opencv frame difference Unhandled exceptionopencv框架差异未处理的异常
【发布时间】:2015-12-02 15:17:32
【问题描述】:

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

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

【问题讨论】:

    标签: c++ opencv


    【解决方案1】:

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

    【讨论】:

    • 或者去掉开头的灰度转换。
    • 有时候在减帧的时候最好用一个通道,这个技术用在运动检测上,一个通道就够了。
    • 确实是这样(虽然运动检测可能会更好地使用颜色信息 - 取决于场景),但用户并没有告诉他最终喜欢使用灰度还是颜色,所以有两种不同的解决方法。
    【解决方案2】:

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

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

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

    【讨论】:

    • 感谢 cmets,我需要对帧进行灰度化。原始视频为RGB视频。
    • 如你所见,上面的代码只是黑色输出没有差异结果
    • 是的,您正在从其副本中减去图像,因此差异很大
    • 在开始时获取新帧并在最后保留副本
    • 谢谢,现在可以工作了,是 frameCurrent.copyTo(framePrev);可以吗?还是我应该使用其他方法
    【解决方案3】:

    我根据你的建议把代码改成下面的了,运行的时候并没有显示出差异的结果

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

    【讨论】:

      猜你喜欢
      • 2021-03-08
      • 2021-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2011-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多