Histogram Equalization Algorithm & code

Source: Internet
Author: User

After one day, it was a black screen, and finally found that it was caused by a local variable.

It is basically divided into three steps, which is to achieve a balanced histogram under the grayscale image. The color is directly balanced and researched and then supplemented.

Step 1:

Calculate the initial histogram, that is, the number of occurrences of each pixel.

 

Int hist [256] = {0 };

For (INT y = 0; y <m_image-> getheight (); y ++)
{
For (INT x = 0; x <m_image-> getwidth (); X ++)
{
Colorref RGB = m_image-> getpixel (x, y );

Int rvalue = getrvalue (RGB); // records the number of occurrences of each pixel.

Hist [rvalue] ++;
}
}

 

 

 

Step 2:

Calculate the normalized histogram and the cumulative histogram from the initial histogram obtained above.

 

 

Double phist [256];

For (INT I = 0; I <= 255; I ++)
{
Phist [I] = (double) hist [I]/(m_image-> getheight () * m_image-> getwidth (); // normalized histogram indicates the probability of each pixel
 
}

Double dsum [256] = {0.0 };

For (INT I = 0; I <= 255; I ++)
{
If (I! = 0)
{
Dsum [I] = dsum [I-1] + phist [I];
}
Else // cumulative Histogram
{
Dsum [I] = phist [I];
}
}

 

Step 3:

 

Calculate the balanced ing (old graph-> New Graph) relationship and write the new image

 

Int mapping [256];

For (INT I = 0; I <= 255; I ++)
{

Mapping [I] = 255 * dsum [I] + 0.5; // ing relationship


}

 

For (INT y = 0; y <m_image-> getheight (); y ++)
{
For (INT x = 0; x <m_image-> getwidth (); X ++)
{

Colorref RGB = m_image-> getpixel (x, y );

Int rvalue = getrvalue (RGB );

Rvalue = mapping [rvalue]; // balance based on the ing relationship

RGB = RGB (Rvalue, Rvalue, Rvalue );

M_image-> setpixel (X, Y, RGB );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.