# Thread: calculate percentiles from a histogram

1. ## calculate percentiles from a histogram

Hi,
I need to compute the percentile of a histogram , but I'm not sure how to accomplish that. After computing the color histogram of an image I have an 256 size arrays that contains the distribution of pixel colors (Red ,
Green or Blue). Given a percentile , the number of pixels in the
image :n ,my idea was first to sort the vector increasingly and than
start adding the elements of the array until I got a sum closest to
p*n . The index of the array where I stopped adding was considered to
be the intensity color that matched the searched percentile. The
thing is that I'm really not sure whether this is correct , so any hints on this would be greatly appreciated.

2. Originally Posted by rantravee
Hi,
I need to compute the percentile of a histogram , but I'm not sure how to accomplish that. After computing the color histogram of an image I have an 256 size arrays that contains the distribution of pixel colors (Red ,
Green or Blue). Given a percentile , the number of pixels in the
image :n ,my idea was first to sort the vector increasingly and than
start adding the elements of the array until I got a sum closest to
p*n . The index of the array where I stopped adding was considered to
be the intensity color that matched the searched percentile. The
thing is that I'm really not sure whether this is correct , so any hints on this would be greatly appreciated.
something like:

Code:
ll=length(data);  %length of data set assumed a linear array

data1=sort(data);  %sort the data set

nn=round(ll*percentile/100); %caluclate the
%index for the approximate
%percentile position
if nn<=0
nn=1
elseif nn>ll
nn=ll
end

xx=data1(nn);  %approximate percentile
CB

3. something more like this :

[PHP]
int calculatePercentile(int v[], int nrTotalPixeli,int procent)
{
int limita=(procent*nrTotalPixeli)/100;
boolean contCond=true;
int sum=0;
int index=0;

while(contCond)
{
sum=sum+v[index];
if(sum>limita)
{
sum=sum-v[index];
contCond=false;
index--;
}
else
{
index++;
}
}

return index
[/PHP]

4. Originally Posted by rantravee
something more like this :

[php]
int calculatePercentile(int v[], int nrTotalPixeli,int procent)
{
int limita=(procent*nrTotalPixeli)/100;
boolean contCond=true;
int sum=0;
int index=0;

while(contCond)
{
sum=sum+v[index];
if(sum>limita)
{
sum=sum-v[index];
contCond=false;
index--;
}
else
{
index++;
}
}

return index
[/php]
So you have a 256 element array v with the pixel count of those pixels with value n in v(n)? Then what you have looks about right-ish. Why not do a hand calculation to compare with the code result?

CB