abbas mahmudi
abbas mahmudi

Reputation: 175

stretching histogram of image in matlab

I'm trying to implement a function imshow(img,[]), using the following formula for each pixel:

(img(x,y)-min(img))/(max(img)-min(img))*255  

But I got a different result How can I stretch the histogram without using imshow(img,[]) tnx

enter image description here

code:

IAS=input('please enter image address','s');
Iimg=imread(IAS);
stimg=(Iimg-min(Iimg(:)))/(max(Iimg(:))-min(Iimg(:)))*255;
subplot(1,3,1)
imshow(stimg);
title('strechself');
subplot(1,3,2)
imshow(Iimg);
title('original image');
subplot(1,3,3)
imshow(Iimg,[])
title('imshow(img,[])');

Upvotes: 2

Views: 1766

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

Its probably due to the wrong use of max and min.

You are doing min(img) and that will give you an array of the minimum of each row. if you want the absolute minimum of the whole image, you should call min(img(:))

Therefore, change your line to:

img=(img-min(img(:)))/(max(img(:))-min(img(:)))*255  

Note that its just 1 line. In Matlab you don't need to access each pixel (img(x,y)) and change independently as in other languages, you can do it directly.

Additionally, if img is not a uint8 , I suggest you make it (because you are using 0-255 scale)

img=uint8(img);

EDIT : Having a look to your results, very probably your original image is an uint8, therefore, before the line to strech the image, you should add the following line:

img=double(img);

So you can make divisions and keep the numbers. Else, you are performing integer division, so 34/255=0

Upvotes: 3

Related Questions