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 );
}
}