# Problem with histogram equalization algorithm

Discussion in 'C' started by metamofia, Oct 20, 2009.

1. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
Likes Received:
0
Trophy Points:
0
Hi, im doing histogram equalization on 8-bit images of sizes 512*512 and 256*256..
Here is my code below:
Code:
```
void CLEO_MedivisionView::OnUpdateToolsHistogramequalization(CCmdUI *pCmdUI)
{
// TODO: Add your command update UI handler code here

if (fileopen)
{
pCmdUI->Enable(true);
}
else
{
pCmdUI->Enable(false);
}
}

void CLEO_MedivisionView::OnToolsHistogramequalization()
{
int i;
unsigned int array1[256];
unsigned int cdf[256];
unsigned int arrayhe[256];

//** Step 1 **\\
for (i=0;i<256;i++)
{
array1[i]=0;
}
if (no_of_rows == 512 && no_of_cols == 512)
{
for (i=0; i<262144; i++)
{
array1[image[i]]++;
}
}
else if (no_of_rows == 256 && no_of_cols == 256)
{
for (i=0; i<65536; i++)
{
array1[image256[i]]++;
}
}

//** Step 2 **\\
cdf[0] = array1[i]; // finding cumulative distribution

for (i=1; i<256; i++)
{
cdf[i] = array1[i] + cdf[i-1];
}
for (i=0;i<256;i++) // zeros out all the element in the array
{
arrayhe[i]=0;
}
for (i=0; i<256; i++) // applying the HE algorithm
{
arrayhe[i] = (cdf[i]*255/262144)+0.5;
}

}
```
Im sure that step 1 is correct..

however when i toggle a breakpoint on cdf in step 2 and when i wanted to check the values inside it and make sure its cumulative frequency compared to array1 which is just frequency.. When i got the values, i get random flow of number ie: '02ex47810'..

Any ideas why is this so? Thanks

2. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
Likes Received:
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
Code:
```for (i= !!! 1 !!!; i<256; i++)
for (i= !!! 0 !!!; i<256; i++)
```
You've reintroduced the "not starting at zero" bug.

3. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
Likes Received:
0
Trophy Points:
0
What do u mean?

4. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
Likes Received:
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
Sorry, my mistake, it's a different bug.
Code:
```cdf[0] = array1[i];
```
At this point, i is either 262144 or 65536 having just come out of the "if (no_of_rows == 512 && no_of_cols == 512)" block. So array1 is undefined, and all your values after that are critically dependent on this, so you get garbage.

5. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
Likes Received:
0
Trophy Points:
0
All right thanks. Thats settled.. IM getting the cumulative values for cdf[] now..

My second and probably the last issue is how do i display the NEW intensity values on the old image, after this code:

Code:
```unsigned int arrayhe[256];

for (i=0; i<256; i++) // applying the HE algorithm
{
arrayhe[i] = (cdf[i]*255/262144)+0.5;
}
```
Do help me out on this thanks.

6. ### xpi0t0sMentor

Joined:
Aug 6, 2004
Messages:
3,012
Likes Received:
203
Trophy Points:
0
Occupation:
Senior Support Engineer
Location:
England
How did you read the previous intensity values from the image?

7. ### metamofiaNew Member

Joined:
Sep 28, 2009
Messages:
47
Likes Received:
0
Trophy Points:
0
hi thanks for getting back to me.. im done with the equalization.. i got a new question on segmentation which i will post in another thread