gpt4 book ai didi

c++ - 无法理解 cv2::absdiff 的行为:c++

转载 作者:行者123 更新时间:2023-11-28 01:55:26 24 4
gpt4 key购买 nike

我正在使用 cv2::absdiff() 从图像矩阵中减去标量。我使用的代码是:

  double min;
double max;
Scalar mean;
Scalar std_dev;

minMaxLoc(img_a_color_planes[1], &min, &max);
meanStdDev(img_a_color_planes[1], mean, std_dev);

Mat img_a_color_planes[3];
split(img_a, img_a_color_planes);
Mat oper = img_a_color_planes[1];

absdiff(oper, mean, oper);
divide(oper, std_dev, oper);
multiply(oper, 10, oper);
add(oper, mean, oper);

在这里,我特意将 img_a 的绿色 channel 复制到 Mat oper 中,尽管在 中指定了输出矩阵 oper >绝对差异。即便如此,绿色 channel img_a_color_planes[1] 也会受到影响。我不明白这是为什么。我怎样才能避免这种情况?以下是我的 img_aabsdiff 操作后受到影响的方式:
初始img_a:

enter image description here

absdiff操作后:

enter image description here

最佳答案

问题是 Mat oper = img_a_color_planes[1]; 不复制底层数组,只复制 header 数据。这同样适用于 copy constructor .

您需要制作一个独立的拷贝,以避免对原始图像进行更改。一种方法是通过 clone() 方法:Mat oper = img_a_color_planes[1].clone();。克隆在后台使用 copyTo():https://stackoverflow.com/a/15688165/2988730 .

引用 Mat docs :

Use a copy constructor or assignment operator where there can be an array or expression on the right side (see below). As noted in the introduction, the array assignment is an O(1) operation because it only copies the header and increases the reference counter. The Mat::clone() method can be used to get a full (deep) copy of the array when you need it.

关于c++ - 无法理解 cv2::absdiff 的行为:c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41397945/

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