Solved

# Histogram equalization

Posted on 2006-05-17
389 Views
Hi there,
I have been having alot of trouble trying to make the histogramn equalization work in c++ and this laplacian smoothing is not working at all with me.
My program only enhances gray scale currently so im not involved with any rgb being stored and I am sending to my functions the array image2D[row][col] that contains the pixel values.
Thankyou
0
Question by:killaz4life

LVL 53

Expert Comment

Can you tell us the exact problem you have, as well as the relevant code you have ?
0

Author Comment

void CImgProcess::histogramEqualisation()
{

for(int r=0;r<ROW;r++)
for(int c=0;c<COL;c++)
{
double value=image2D[r][c]; value = value/255;
value=value*255/(ROW*COL);
image2D_Buffer[r][c]=(unsigned char)value;
}

}
0

LVL 53

Expert Comment

>>                value=value*255/(ROW*COL);
What do you intend to do here ?

In general value will end up to be 0 in most cases. I'm sure that's not what you want.
0

Author Comment

What should i do then?? Help me with the function, what I do wrong??
0

LVL 53

Expert Comment

As i asked : what do you want do do ?
0

Author Comment

I want to do histogram equalization thas what I want to do! Ok I get rid of value=value/256;
I really hope u know what histogram equalization is.
0

LVL 53

Accepted Solution

I had to look it up, and here's the step-by-step way of implementing it :

1) calculate the histogram for the image ... ie. count the distinct pixel values in the image (loop over the image, and increase the counter for the current pixel value by 1).

2) fill another array containing the sum of the histogram values upto and including that value (ie. cumulative). Normalise this array by multiplying it with 255/(COLS*ROWS)

3) transform the input image

Or in code :

int histogram[256] = { 0 };
int r, c, i;

// step 1 : calculation of histogram
for (r = 0; r < ROW; r++) {
for (c = 0; c < COL; c++) {
histogram[image2D[r][c]]++;
}
}

// step 2 : cumulative histogram and normalisation
for (i = 1; i < 256; i++) {
histogram[i] += histogram[i - 1];
}
for (i = 0; i < 256; i++) {
histogram[i] = (int) ((((double) histogram[i]) / ((double) (ROW * COL))) * 255);
}

// step 3 : transform image
for (r = 0; r < ROW; r++) {
for (c = 0; c < COL; c++) {
image2D_Buffer[r][c] = histogram[image2D[r][c]];
}
}
0

LVL 53

Expert Comment

And here's a nice link explaining the process :

http://www.generation5.org/content/2004/histogramEqualization.asp

btw, I didn't test the code, so you'll have to do that before assuming it's 100% correct !!
0

Author Comment

I just tested the code you gave me, there some reason I only see blocks rather than image enhancement. Could it be possible the int is doing any effect?
0

LVL 53

Expert Comment

What do you mean by "blocks" ?

The code I gave was not tested, and was written from my head ... so there could be errors present ... It was meant to give you an idea about the code rather than to use it as is.

In any wase, the big lines should be ok. You might want to check the intermediate outputs to see if the result is as expected.

Try it on an image you know should be enhanced by this process (eg. the ones on the site i referred to earlier).
0

## Featured Post

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.