rescale histogram

hello all !
i need to rescale the x-axis(gray levels) of a histogram from 0-1 to 0-255 ! can anyone help me ?! ,i need it coz i've to calculate pdf then.. thank you

2 Comments

Geoff
Geoff on 30 Apr 2012
Post the code you are having issues with. If you are using the 'hist' command, you can solve a lot of issues by using 'histc' to generate an array, and then draw it into a bar graph.
histc doesnt satisfy my needs, coz i've a histogram of an image where the grey levels are in this interval [0,1] and there are like 4 pulses in a different positions,and i want to normalize it between [0 255] so i would have these 4 pulses or whatever in an integer number and not fractional number ....

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 30 Apr 2012

0 votes

If your image has value in the range 0-255 then how did you get bins in the 0-1 range? You must be looking at an image that has been transformed, perhaps by im2double. Maybe you should just take the histogram of the original image, or else multiply the image you took the histogram of by 255 and cast to uint8 and take the histogram of that instead.

9 Comments

histc doesnt satisfy my needs, coz i've a histogram of an image where the grey levels are in this interval [0,1] and there are like 4 pulses in a different positions,and i want to normalize it between [0 255] so i would have these 4 pulses or whatever in an integer number and not fractional number ....
now i'm trying to do what u've told me... but the problem is that i have to make the mutual info between 2 images , and they don't have the same size so i resized them, and then i've to extend the interval of gray levels,coz it's not acceptable to have that interval,as i will have gray levels of fractional number, so i must make it between 0 255, or by approximating the fractional numbers in the interval [0 1]
I don't see why you need to scale the intensity ranges of the two images so that they match. And, even if you did, why stop at just matching the min and max? Why not match the whole histogram, like in my histogram matching app (http://www.mathworks.com/matlabcentral/fileexchange/28972-custom-shaped-histogram) or like http://www.eyemaginary.com/Portfolio/ColorHistogramWarp.html
first of all i'd thank you for replying... look if your aim is to calculate the entropy of an image, what may you do? as i know,to calculate the entropy i need the pdf(prob.density func.) that is the probability of the number of occurance of a gray level over the size of the image (p=p(i)/m*n) , but if the gray levels of my images are in tht interval [0 1] than (i) will be fractional numbers,and the calculation will not be so accurate, so i would project this interval to another one in a way to have (i) an integer numbers...
Yes, to calculate entropy (http://en.wikipedia.org/wiki/Entropy_%28information_theory%29) you find the histogram and then divide by the number of pixels in the image, then do sum(p.*log(p)). So the *counts* are scaled to within 0 and 1. Most likely no bins will hit 1 unless you have only one signal gray level in your image. But I don't see why the scaling of the gray levels is required. In your terminology, you scale p, but not i (where i is the gray level). I just don't see how the entropy formula has gray level in it - I see only the scaled counts (i.e., "p") in it. Am I missing something?
no , i want to scale (i) not p!!! look if we count the pixels in each gray level in our image, we will have ,suppose, for level 0.039 we have 10000 pixels , for i=0.068 we have 5000 pixels, can i transform the 0.039 and 0.068 to integer numbers! how? if not we will have the entropy tend to infinity because of these values that tend to zero and log(number tend to zero) goes to infinity !!
Geoff
Geoff on 2 May 2012
To transform the normalised grey level to an integer, you multiply by 256 (or 255, if you insist) and round the result. I thought the entire concept of entropy is a tendency to approach infinity... =P
You still haven't convinced me, and I doubt you will. The probability values in the probability density function do not depend on the bin value. The probability values will of course depend on the width of the bins, and the location of the bins with respect to the true distribution, but simply scaling the bins (what you call i or gray level) will not change the PDF, and thus not change p in sum(p*log p), and not change entropy. Try it and see. If you don't see, ask me and I'll make a demo for you to prove it.
ok thnaks tomorrow i'll let u see what i'm doing i'll send u my code !

Sign in to comment.

Geoff
Geoff on 2 May 2012
Still not sure I understand your motive completely...
But it seems like you have this situation:
data = rand(100,1);
bins = (1:4)/4 - 1/8;
hist(data, bins);
And you want this:
hist(data*256, bins*256);
set(gca, 'XLim', [0 256]);
set(gca, 'XTick', linspace(0,256,5));

10 Comments

great thank you, seems very very near to what i want...i will tell u tomorrow ..godnight. thanks image analyst for your help
it doesnt work !!! i tried to modify this code but i still can't do it!!
Geoff
Geoff on 2 May 2012
What do you mean "it doesn't work"? If you think this answer is close but can't make it work with your modifications, then post your modifications so we can have a better guess at what you want.
It could be because he took your code and calculated the entropy, and he got the same entropy no matter which histogram he used. That's what I've been trying to tell him, but he hasn't realized it yet - he's still expects that the entropy of the image when the intensity values are in the range 0-1 will somehow be different and less accurate than when they are scaled from 0-255. You and I know that's not true but he's not convinced. The PDF = pixelCount / numel(pixelCount) and this doesn't depend on the bin values (the "x", or the value at the center of the bin, or gray level values, or his "i", or however you want to describe it). Maybe you can take a shot at explaining it to him.
i didn't his code to calculaate the entropy or whatever! i simply tried it to rescale the histogram, i've calculated the entropy and the sum of frequencies and it's ok in the [0 1] interval.... but my proffessor want us to rescale the histogram, that's all ! actually for me is easier to work in the[0 1].....
So either divide the bin values by 255 so the bins now go from 0-1 instead of 0-255 (but don't divide the pixel counts), or else divide the image by 255 and then use hist() or imhist(). Either way should get you the same thing.
ok i'll try it now... if not i'll calculate entropy and all the rest without modifying it.... but i would send u all the project ,if u dont have problems !!
i've given up !! what i think to do is;
[counts x]=imhist(img)
nz=find(counts)
p(nz)=counts(nz)./(tot.no.counts)
then sum(sum(p(nz))) . for this poit it works ! but the if i try to calculate the entropy ( -sum(p(nz)*log2(p(nz)) ) it doesnt work !!!! what the hell!!!!!!!!
ok i've missed the (.) in the formula . i did it!
but could u tell me ,which of these 2 codes is much better!
img1=img./255 % or img1=img.*255 ???
[counts x]=imhist(img1)
nz=find(counts)
p=count(nz)./(m*n) %m*n= number of pixels in img
sum(sum(p))
entropy=-sum(p.*log10p) % or log2 ???

Sign in to comment.

Junaid
Junaid on 2 May 2012

0 votes

I guess if you normalize your histogram, you will your desired output .. May be ?
As after normalizing your histogram, the sum of all values will be 1. That is actually you want (all values in Histogram will be in between 0-1).
Easiest way to normalize is to divide histogram by sum of histogram.

3 Comments

No, he wanted the intensity values to go from 0-1, not the count values.
Junaid
Junaid on 3 May 2012
Oh... thanks for letting me know.
sorry junaid if i ddnt reply yet !! ... i'll write my code here after i've finished it.

Sign in to comment.

Tags

Asked:

on 30 Apr 2012

Community Treasure Hunt

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

Start Hunting!