gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:08:14 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com