Search code examples
image-processingc++-clihistogram

My Code Doesn't Work to Equalize Histogram


I tried to code histogram equalization operation in order to enhance contrast of the images, but my code didn't work. When I displayed image's original histogram and histogram after processed by my code I saw that output histogram only have a value at 0 and there aren't values for other pixel intensities. I don't know why. Here is my code:

private: System::Void histogramEqualizationToolStripMenuItem_Click(System::Object^  sender, System::EventArgs^  e) {

    Raw_Intensity = ConvertBMPToIntensity(Buffer, Width, Height);
    int histogram[256] = { 0 };
    int equalizedHistogram[256] = { 0 };
    int runningSum = 0;
    int numberOfPixels = Width * Height;


    for (int row = 0; row < Height; row++)
    {
        for (int column = 0; column < Width; column++)
        {
            histogram[Raw_Intensity[row * Width + column]]++;
        }
    }

    for (int i = 0; i < 256; i++)
    {
        runningSum += histogram[i];
        int index = round(((runningSum / numberOfPixels) * 255));
        equalizedHistogram[index] += histogram[i];
    }


}

Solution

  • I think the casting issue is occuring. change this line :

     int index = round(((runningSum / numberOfPixels) * 255));
    

    to

    int index = round(((runningSum*1.0 / numberOfPixels) * 255));