Simplicity
Simplicity

Reputation: 48966

Showing an image

I came to a matlab code where it first reads an image and converts it to double as follows:

I = double(imread(img));

After that, it shows the image as follows:

imshow(I/max(I(:)))

What does that mean? Especially when I removed the max part, I got only an image with a white background. What is the goal of such division? And, why does it show the image properly when done and doesn't show the image properly if you directly show the read image without the division by max?

Thanks.

Upvotes: 0

Views: 1447

Answers (3)

matheburg
matheburg

Reputation: 2180

I is a 2D or 3D matrix (depends on grayscale or color). I(:) is the vector where all values of the matrix are written in a column, just like they are arranged in the memory; you could do by reshape as well. Please read more about the colon-operator in Matlab documentation, it is an absolut basic concept of Matlab.

max gives you the maximum over a vector, i.e. max(I(:)) gives you the maximum value over the whole image.

It is an unwritten law that the range of an image starts at 0. Therefore you can map the values of the image to [0,1] by dividing it by max(I(:)). In Matlab it is done like that: myMatrix/myScalar.

So I/max(I(:)) gives you the image with values in [0,1], what is required for double-images that you want to show with imshow.

Please note:

(1) You can write imshow(I,[]) instead which will show you the image with values stretched to [0,1] (in difference to your version the minimum is mapped to 0 as well).

(2) In my view you should map the values of an image for visualization only in exceptional cases like you do. It can give you wrong impression of the image (e.g. a very dark image will be visualized as full contrast image). Try to devide by maximum of the original value range (often 255, 4095 or 65535). Example:

img = imread('some12bit.png');
img = double(img);
img = img / 4095; % [0,4095] -> [0,1]
imshow(img);

(3) Without mapping the image to [0,1] all values > 1 will be interpreted as 1. That's why your image is shown as a white image without mapping it to [0,1] before.

Upvotes: 0

Nick
Nick

Reputation: 3203

max(I(:)) seems to be a normalize step

From the documentation follows that imshow needs a input matrix having values between 0 and 1.0

imshow(I) displays the image I in a Handle Graphics® figure, where I is a grayscale, RGB (truecolor), or binary image. For binary images, imshow displays pixels with the value 0 (zero) as black and 1 as white.

Upvotes: 2

Paul R
Paul R

Reputation: 213170

The expression:

I/max(I(:))

just normalises the pixel values to the range 0..1.0. It does this by dividing all pixel values by the max value.

Upvotes: 3

Related Questions