Images don't show with imshow after converting them to double.

54 ビュー (過去 30 日間)
Adrian
Adrian 2013 年 10 月 2 日
コメント済み: Image Analyst 2017 年 12 月 4 日
Hello all,
I have a very simple code, pic.png is a greyscale image:
I = imread('pic.png');
imshow(I); %displays the picture without problems
I = double(I);
imshow(I) %only displays white, nothing more.
I find this weird, why does it do this?
  3 件のコメント
Image Analyst
Image Analyst 2015 年 10 月 11 日
Not necessarily. What if your double image is an array of temperatures? Using im2double() would cause all of the temperatures to change, which would be a major inconvenience to you.
Walter Roberson
Walter Roberson 2015 年 10 月 11 日
im2double() is discussed in the existing Answers, with reasoning and possible disadvantages discussed as well.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2013 年 10 月 2 日
MATLAB looks at the datatype to decide which range of values to expect. uint8 are expected to be 0 to 255. double are expected to be 0 to 1. When you double() a uint8 you end up with 0. to 255. and everyt value from 1 upwards will be considered to saturate the maximum 9 to 1 range.
You im2double() instead of double()
  1 件のコメント
Adrian
Adrian 2013 年 10 月 2 日
Thanks, that made it clear for me.

サインインしてコメントする。

その他の回答 (4 件)

Image Analyst
Image Analyst 2013 年 10 月 2 日
I wouldn't use im2double. It's usually inconvenient to scale your image when you want to do some operations, like convolution or whatever. All you need to do is to simply use [] in imshow():
imshow(yourDoubleImage, []);
and it will work beautifully for gray scale images. If it's a color image though, that won't work, you'd have to case to uint8 to display or use im2double and, unfortunately, have to deal with altered intensity values. I prefer the casting to uint8 which usually works if your values remain in the range of about 0-255. Step through this demo and it will illustrate the different things that happen depending on what you do.
% Read in a gray scale image and make it double
% in the range 0-255.
doubleGrayImage = double(imread('moon.tif'));
subplot(2,2,1);
imshow(doubleGrayImage, []) % Doesn't look right.
% Read in a gray scale image and make it double
% in the range 0-1.
doubleGrayImage = im2double(imread('moon.tif'));
subplot(2,2,2);
imshow(doubleGrayImage, []) % Doesn't look right.
% Read in an image and make it double
% in the range 0-255.
doubleRGBImage = double(imread('peppers.png'));
subplot(2,2,3);
imshow(doubleRGBImage, []) % Doesn't look right.
% Read in an image and make it double
% in the range 0-1.
doubleRGBImage = im2double(imread('peppers.png'));
subplot(2,2,4);
imshow(doubleRGBImage) % Now looks right, but values are changed.
  1 件のコメント
Adrian
Adrian 2013 年 10 月 2 日
Cool, thanks for the reply. I'll be checking that demo out.

サインインしてコメントする。


Sean de Wolski
Sean de Wolski 2013 年 10 月 2 日
You could also just show it over the full range by using the [] input to imshow:
imshow(I,[])

Amit Nambiar
Amit Nambiar 2013 年 10 月 2 日
I=uint8(round(I-1))
imshow(I)
do this to convert and show a double image...
  1 件のコメント
Adrian
Adrian 2013 年 10 月 2 日
I just used mat2gray and it worked. But I'm curious as to why imread will not work once I converted the values to double.

サインインしてコメントする。


Visweshwar Srinivasan
Visweshwar Srinivasan 2017 年 12 月 4 日
Simple! See, double values are just integer values, the reason why imshow doesn't work with double values is because it is made to work only with 8bit values. Hence, either convert double values to 8 bit integer values using uint8([double value]) command or use the im2double() statement which does this internally!
  2 件のコメント
Stephen23
Stephen23 2017 年 12 月 4 日
編集済み: Stephen23 2017 年 12 月 4 日
"reason why imshow doesn't work with double values is because it is made to work only with 8bit values"
Actually imshow works perfectly with double values.
Note that the solution Image Analyst gave is simpler, and does not require changing the data itself:
imshow(yourDoubleImage, [])
Image Analyst
Image Analyst 2017 年 12 月 4 日
"double values are just integer values" is another incorrect statement.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by