【问题标题】:Error while finding differences in frames from camera从相机中查找帧差异时出错
【发布时间】:2013-05-21 16:05:48
【问题描述】:
int main(int argc, char* argv[])
{
    VideoCapture cap(0);
    Mat current_frame;
    Mat previous_frame;
    Mat result; 
    Mat frame;

    //cap.open(-1);
    if (!cap.isOpened()) {
        //cerr << "can not open camera or video file" << endl;
        return -1;
    }

    while(1)
    {
        cap >> current_frame;
        if (current_frame.empty())
            break;

        if (! previous_frame.empty())  {
            // subtract frames
            subtract(current_frame, previous_frame, result);
        }


        imshow("Window", result);
        waitKey(10);

        frame.copyTo(previous_frame); 
    }
}

当我运行这个程序从前一帧中减去当前帧然后显示结果帧时,它会在开始执行时显示这个错误

WK01.exe 中 0x755d812f 处未处理的异常:Microsoft C++ 异常:cv::Exception at memory location 0x001fe848..

我想在录制的视频上应用同样的东西

【问题讨论】:

  • 你用框架做什么?这可能不是您的错误,但看起来框架将始终为空,因此您将在每次迭代时将一个空框架复制到上一个框架中。
  • @JoeRunde 我想在视频上运行它,就像在任何 5 分钟录制的电影上一样,所以它怎么可能是空的?从第一帧到第二帧必须有变化?
  • 就像 berak 在他的回答中所说的那样,有一个名为“frame”的垫子,你从来没有把任何东西放进去。你所做的就是将下一帧放入“当前帧”,然后存储“结果" = "当前帧" - "上一帧" 然后存储 "上一帧" = "帧"。看到问题了吗?你从来没有把任何东西放入“框架”
  • @JoeRunde 是的我明白了,你想说我不应该分配 Frame,因为它是 emtpy

标签: c++ visual-c++ opencv image-processing computer-vision


【解决方案1】:

在第一帧,结果为空!

imshow("Window", result); // this will crash

另外,您将空的 frame Mat 复制到 previous_frame,应该是 current_frame,不是吗?

试试看:

   if (! previous_frame.empty())  {
       // subtract frames
       subtract(current_frame, previous_frame, result);
       imshow("Window", result); 
   }
   waitKey(10);
   current_frame.copyTo(previous_frame); 
}

【讨论】:

    【解决方案2】:

    我认为问题出在previos_frame。您仅在循环的 and 处为 previous_frame 赋值。 我认为在while循环开始时它可能是空的,所以

    if (! previous_frame.empty())  {
            // subtract frames
            subtract(current_frame, previous_frame, result);
        }
    

    块不会被执行。

    previous_frame 在减法时也必须与current_frame 大小相同。

    此代码(减法)应确定result 的大小,即您希望在下一行显示的内容。

    【讨论】:

      猜你喜欢
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      相关资源
      最近更新 更多