Gamma correction (based on OPENCV)

Source: Internet
Author: User

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


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.