Gamma correction is a non-linear operation of the gray value of the input image, which causes the gray value of the output image to be exponentially related to the gray value of the input image.
The exponential gamma is gamma .
The grayscale values of the input and output images after Gamma correction are shown in the following figure:
The horizontal axis is the input gray value, the ordinate is the output gray value, the blue curve is the gamma value is less than 1 o'clock input and output relationship, the red curve is the gamma value greater than 1 o'clock input and output relationship.
It can be observed that when the gamma value is less than 1 o'clock (blue curve), the overall luminance value of the image is improved, while the contrast at the lower gray level increases, the contrast at the high gray level decreases, which is more advantageous to the image detail when the low gray value is resolved;
When the gamma value is greater than 1 o'clock (red curve), the overall brightness of the image is worth decreasing, while the contrast at the lower gray level decreases, and the contrast at high gray level increases, which is more advantageous to the image detail when the high gray value is distinguished.
function Implementation (pro-Test pass):
/************************************************ * Gamma correction * template<...> _tpsaturate_cast (_TP2V): Template function for * Accurate conversion from one primitive type to another */void GammaCorrection (mat& src, mat& Amp
DST, float Fgamma) {//Build look up table unsigned char lut[256];
for (int i = 0; i < i++) {Lut[i] = saturate_cast<uchar> (Pow ((float) (i/255.0), Fgamma) * 255.0f);
} DST = Src.clone ();
const INT channels = Dst.channels ();
Switch (Channels) {Case 1: {matiterator_<uchar> it, end;
for (it = dst.begin<uchar> (), end = Dst.end<uchar> (); it! = end; it++) *it = lut[(*it)];
Break
} Case 3: {matiterator_<vec3b> it, end; for (it = dst.begin<vec3b> (), end = Dst.end<vec3b> (); it! = end; it++) {(*it) [0] = Lut[((*it) [0]); (*it)
[1] = lut[((*it) [1])]; (*it)
[2] = lut[((*it) [2])];
} break; }
}
}
Demo:
void Demo ()
{
Cv::mat src = Cv::imread ("./3.jpg");
Cv::mat DST;
GammaCorrection (SRC, DST, 0.45);
Cv::namedwindow ("Win1");
Cv::imshow ("Win1", SRC);
Cv::namedwindow ("win2");
Cv::imshow ("Win2", DST);
Cv::waitkey (0);
}