ELRG
ELRG

Reputation: 630

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. I'm working with CV_32FC1 Mat types & my images are greyscale.

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.)

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. A fix would be great, but the why is what I'm most interested in.

Upvotes: 2

Views: 103

Answers (1)

Chris Maes
Chris Maes

Reputation: 37712

from the opencv documentation; the divide operator does not what you think: for each element it calculates:

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).

Upvotes: 2

Related Questions