## How to Implement an “imshow” function?

### Anna Nazarova (view profile)

on 2 Dec 2018 at 0:33
Latest activity Commented on by Image Analyst

### Image Analyst (view profile)

on 9 Dec 2018 at 15:52

### Walter Roberson (view profile)

How to Implement an “imshow” function by transforming the range [min, max] on the gray scale into the interval determined by 0 ≤ a, b ≤ 1? I need use variables and it should be suitable for any image not particular. Thank you!

### Walter Roberson (view profile)

on 2 Dec 2018 at 0:41

See mat2gray()

on 2 Dec 2018 at 0:46

If you mean the scaling of any range of values to [0 1], than the code below does what you need.
IM=uint8(randi(255,50,50));%generate 'image'
a=20;b=80;%define upper and lower bound
IMsc=double(IM);%make sure it is a double
IMsc=(IMsc-a)/(b-a);
IMsc(IMsc<0)=0;IMsc(IMsc>1)=1;
figure(1),clf(1)
subplot(1,3,1)
imshow(IM,[])
title('original')
subplot(1,3,2)
imshow(IM,[a b])
title('original (scaled)')
subplot(1,3,3)
imshow(IMsc,[])
title('scaled result')

### Anna Nazarova (view profile)

on 2 Dec 2018 at 1:02

The result should be like this. And if I use your code than the image not changing

Show 1 older comment
Anna Nazarova

### Anna Nazarova (view profile)

on 2 Dec 2018 at 14:48
Rik, this code:
a=0;b=255;%define upper and lower bound
IMsc=double(IM);%make sure it is a double
IMsc=(IMsc-a)/(b-a);
IMsc(IMsc<0)=0;IMsc(IMsc>1)=1;
figure(1),clf(1)
subplot(1,3,1)
imshow(IM,[])
title('original')
subplot(1,3,2)
imshow(IM,[a b])
title('original (scaled)')
subplot(1,3,3)
imshow(IMsc,[])
title('scaled result')

on 2 Dec 2018 at 16:18
Of course the image is not changing: you have defined a and b is such a way that it will not change. The data type that most images will have when loaded is uint8. This has a minimum of 0 and a maximum of 255. If you want to change how the image looks, there are multiple methods:
1. the method I showed you, where you will have to define a and b yourself, or use a=min(IM(:));b=max(IM(:));
2. the method Walter suggested: use mat2gray(IM) (note that mat2gray(IM,[a b]) is equivalent to my method. mat2gray is in the image processing toolbox, but you appear to have that anyway.)
3. the method Image Analist just suggested: don't change the data, but use the empty square brackets to display you image with a scaled color scheme by using imshow(IM,[])
Which of these you think is best for your situation is for you to decide.
Image Analyst

### Image Analyst (view profile)

on 2 Dec 2018 at 17:35
I also suggested #2. And there is yet another way that might be useful. It's imadjust(). With imadjust(), you can specify a percentage of the way to come in on the tails of the histogram to determine the range. that way, outliers (like salt and pepper noise) will not prevent a nice picture from appearing.

### Image Analyst (view profile)

on 2 Dec 2018 at 15:14

To display a gray scale image without changing the values of the array, but only changing the displayed values, do this:
imshow(grayImage, []);
To change the actual matrix, use mat2gray():
% Scale grayImage to between 0 and 1: min goes to 0, max goes to 1
grayImage = mat2gray(grayImage);

Image Analyst

### Image Analyst (view profile)

on 9 Dec 2018 at 0:03
imshow(grayImage, []);
works, as does
The [] are not needed if you use imadjust since the output of imadjust is already scaled from 0 to 255.
Anna Nazarova

### Anna Nazarova (view profile)

on 9 Dec 2018 at 15:23
Can you please send full code?
Image Analyst

### Image Analyst (view profile)

on 9 Dec 2018 at 15:52
Yes, you can find it here