c++ - 不必在 OpenCV 中复制 Mat 的最佳方法

标签 c++ opencv

我有这段代码,它基本上对两帧进行“愚蠢”的背景减法。

void FrameDifferenceBGS::operator()(cv::InputArray _image, cv::OutputArray _fgmask, double learningRate)
{
  cv::Mat img_input = _image.getMat();

  if(img_input.empty())
    return;

  _fgmask.create(img_input.size(), CV_8U);
  cv::Mat img_foreground = _fgmask.getMat();

  if(img_input_prev.empty())
  {
    img_input.copyTo(img_input_prev);
    return;
  }

  cv::absdiff(img_input_prev, img_input, img_foreground);

  if(img_foreground.channels() == 3)
    cv::cvtColor(img_foreground, img_foreground, CV_BGR2GRAY);

  if(enableThreshold)
    cv::threshold(img_foreground, img_foreground, threshold, 255, cv::THRESH_BINARY);

  if(showOutput)
    cv::imshow("Frame Difference", img_foreground);

  img_input.copyTo(img_input_prev);
  img_foreground.copyTo(_fgmask);
  firstTime = false;
}

如果我不在最后添加 img_foreground.copyTo(_fgmask),则输出数组不会使用 img_foreground 的结果进行更新,从而导致黑色调用时的图像。

我做错了什么/应该在这里做什么?

最佳答案

我再次检查了您的代码。看起来您正在为 _fgmask 创建新对象。

 _fgmask.create(img_input.size(), CV_8U);

我认为这就是您遇到问题的原因。因为论证中的这个引用与这个声明之后的引用不同。为什么不在调用函数之前先调用行。

关于c++ - 不必在 OpenCV 中复制 Mat 的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19140052/

相关文章:

python - 如何可靠地检测条码的 4 个角?

当几乎相同的函数工作时,C++ OIS 段错误

python - 使用Opencv和pytesseract进行验证码预处理和求解

c++ - Typedef 函数指针错误

c++ - C++ 中右值引用的分配和不变性

opencv - 如何在 OpenCV2 中使用 cv::Point 找到中点

opencv - 在 nanoserver Docker 容器中使用 OpenCV 运行 .NET Core 2.0 C# 模块

c++ - OpenCV 2 是否为编码图像的快速片段解码提供任何 api?

c++ - C++ 怎么知道 "quotation marks"之间的任何东西是一个 std::string?

c++ - 如何查看编译器添加的代码?