havakok
havakok

Reputation: 1247

Converting an image to type double

I am reading an image with the following command:

lefty = imread('sintra2.JPG');

and imshow(); gives me a good resault. but If I try to use:

lefty = double(imread('sintra2.JPG'));

imshow() gives me a white image. I am working with a relatively big image shared here. Is there a connection?

How do I convert to double if at all it is necessary? I was told it is better to work with double when working on image processing and computer vision in MATLAB.

Upvotes: 0

Views: 2028

Answers (2)

Sardar Usama
Sardar Usama

Reputation: 19689

When you read the image, its type was uint8 and thus lefty contained the values from 0 to 255 (28 = 256). When you used double, it converted the class from uint8 to double but the values remained same i.e 0-255.

You need im2double here which not only converts the values to double precision but also rescales the values in the range 0-1 by dividing all the entries of the input by the maximum possible value of the input data type. So in your case, as the input data type is uint8 whose maximum possible value is 255, therefore all the values will be divided by 255. Note that it is possible that the maximum value in your image data may not be 255 but since the maximum possible value of uint8 is 255, so all the values will be divided by 255.

So the following is what you're looking for:

lefty = imread('sintra2.JPG');
imshow(lefty)
figure
imshow(im2double(lefty))

Upvotes: 6

desa
desa

Reputation: 1390

The problem is with the data type that imshow requires. If the image is of type int, its range should be between 0 and 255. If it is double – between 0.0 and 1.0. Try this:

lefty = imread('sintra2.JPG');
imshow(lefty)

or:

lefty = imread('sintra2.JPG');
imshow(double(lefty)/double(max(lefty(:))))

Upvotes: 3

Related Questions