Reputation: 437
I saw this code in the internet
imgN = double(img-min(img(:)))/(max(img(:)-min(img(:))));
to normalize the image in 0..1 . What's the meaning of it? Also its shows this error
Error using /
Integers can only be combined with integers of the same class, or scalar doubles.
Upvotes: 1
Views: 191
Reputation: 14316
In MATLAB, many functions make assumptions about the range of values in an image. Usually, images are stored either as uint8
or double
. In the case of uint8
, the range is 0...255
(which is all possible values in a uint8
.). For double
, MATLAB chose a value range from 0...1
. To use functions like imshow
, imwrite
and others correctly, your double
image has to be scaled correctly, otherwise some pixels are not displayed correctly.
There is a small bug in the code: It is basically a division a / b
, where a
is double(img - min(img(:)))
and b
is (max(img(:)) - min(img(:)))
. If your input image is of type double
, this will work fine. However if it is uint8
or any other type, the numerator a
will be converted to double
, while the denominator b
stays e.g. uint8
. You will have a division double / uint8
. MATLAB tells you in the error message, that it can not mix types in division. An easy solution is to also convert the denominator b
to double
:
imgN = double(img-min(img(:))) / double(max(img(:)-min(img(:))));
Upvotes: 4