Help with gray scale images

I have a gray scale image which I after thresholding and histogram equalization write using imwrite. But when I read back this stored image, the gray scale values change. 255 changes to 63 and so on. The data type I am using in imread is uint8 type and if I use double, the entire image is white except the pixels having 0 value. I am unable to store the intermediate images. Please can anyone tell me why this is so?
Thanks in advance

 Accepted Answer

If you could show us the code with a sample image (e.g. 'cameraman.tif') it would help us.
More than likely when you're viewing it as double, the view range is set from [0 1] so everything 1 or greater (all values in uint8) appear white. Use the second input argument to imshow to change this, e.g:
Igraydouble = double(imread('cameraman.tif'));
figure;
subplot(121)
imshow(Igraydouble)
subplot(122)
imshow(Igraydouble,[])

7 Comments

close all;
clear;
clc;
d=sprintf('cameraman.tif');
e=sprintf('1.tif');
a=imread(d);
% l= isodatae(a);
l=graythresh(a);
l=(l-0.1)*255;
for i=1:256
for j=1:256
if (a(i,j)==0 || a(i,j)>=l)
a(i,j)=255;
end;
end;
end;
% l1=isodatae(a);
% l1=l1-0.2;
a=imerode(a,strel('disk',8));
% imshow(v)
b=histeq(a,20);
[b,bmap] = gray2ind(b);
imwrite(b,bmap,e,'tif');
fclose all;
a=imread('1.tif');
imshow(a);
now compare the image stored and the image read
l is 62.5, that's why you're seeing so many 63s.
look at im2bw
B = im2bw(a,l); %etc.
agreed that the gray2ind is changing the level
but if you are using windows, can you tell me why the picture stored is so much different in color than the one it actually is in matlab?
is it because of the colormap we are using in imwrite?
if yes can we use the same colormap somehow to bring back the original image(having pixels in 255 rather than 63)
because you've modified it with the for-loops, erosion, gray2ind etc.
no it's not what i meant
when you read the image stored by imread and then imshow, you see a very different image than the one shown by any image viewer like picasa. I'm asking whether the image viewer is showing the different picture because of the graymap used in imwrite
yes, the result of gray2ind

Sign in to comment.

More Answers (1)

Amateuromania
Amateuromania on 9 Jun 2011

0 votes

the second argument [] only helps in display. The matrix value remains unchanged, i.e, 63 and not 255. How is 255 getting replaced by 63 in uint8?

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!