简体   繁体   English

划分 OpenCV Mat 给出了意想不到的结果

[英]Dividing OpenCV Mat gives unexpected result

I'm trying to do some convolution between an image and a kernel, and so my result isn't really huge, I've been attempting to normalise the kernel to a range between 0 and 1 beforehand.我正在尝试在图像和内核之间进行一些卷积,所以我的结果并不是很大,我一直在尝试预先将内核标准化为 0 到 1 之间的范围。 I'm working with CV_32FC1 Mat types & my images are greyscale.我正在使用 CV_32FC1 垫类型,我的图像是灰度的。

My kernel starts off with a range of -1 to 1. I subtract the lowest value, giving a range of 0 to 2, then divide by the largest value, to what should theoretically be a range of 0 to 1. However, after this division, the range of values becomes 0 to 358. (I'm rounding these numbers - let me know if the actual precise values are helpful.)我的内核从 -1 到 1 的范围开始。我减去最低值,给出 0 到 2 的范围,然后除以最大值,理论上应该是 0 到 1 的范围。但是,在此之后除法后,值的范围变为 0 到 358。(我将这些数字四舍五入 - 如果实际的精确值有帮助,请告诉我。)

This is my code:这是我的代码:

double minVal; double maxVal;
minMaxLoc(kernel, &minVal, &maxVal);
kernel = kernel - minVal;             
minMaxLoc(kernel, &minVal, &maxVal);
divide(maxVal, kernel, kernel);

So, why is this happening?那么,为什么会发生这种情况? I feel like maybe I'm somehow running into a memory or overflow issue, but I think all my values are within range of what 32F can store.我觉得我可能以某种方式遇到了内存或溢出问题,但我认为我的所有值都在 32F 可以存储的范围内。 A fix would be great, but the why is what I'm most interested in.修复会很棒,但为什么是我最感兴趣的。

from the opencv documentation ;来自opencv 文档 the divide operator does not what you think: for each element it calculates: divide运算符与您的想法不同:对于它计算的每个元素:

dst(I) = saturate(scale/src2(I))

While you expect:虽然您期望:

dst(I) = saturate(src2(I)/scale)

I think you should just do:我认为你应该这样做:

kernel = kernel / maxVal;

(not sure this will work; but just make sure you have a real elementwise division). (不确定这会起作用;但只要确保你有一个真正的元素划分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM