Gamma correction for balancing uneven illumination

 

Gamma correction ( the Gamma Correction ) also known as gamma non-linearity ( Gamma-iinearity ), gamma encoding ( Gamma encoding ) or can just simply called gamma (gamma). It is used for video image or light system for luminance non-linear or anti-arithmetic operation performed (Luminance) or tristimulus values (tristimulus values).

Wikipedia

1, official

 

 

Wherein A is a constant, the value of the input and output are non-negative real values. Generally speaking in general of A = 1, the range of input and output values are between 0 and 1. The gamma value gamma] <1 where sometimes referred encoded value gamma (encoding Gamma) , the above-mentioned encoding operation is performed by using a power law of the process is also called gamma compression (compression Gamma) ; contrast, gamma value gamma] > 1 is sometimes also referred to as decoded gamma value (decoding gamma) , the above-described decoding operation is performed using the power law of the process is also called "expanded gamma (gamma expansion)".

2, interpretation

Gamma coding object image is used to compensate for the characteristics of human vision, thereby indicating black and white data bits or bandwidth based on human perception of light, or black and white, maximum use. Under normal illumination (neither dark nor dazzling bright), the properties of human vision have generally or gamma power function. If the image is not gamma encoding, the use of bandwidth or data bits will be unevenly distributed - or have too much bandwidth of data bits used to represent the human can not perceive the difference, and for indicating very sensitive human visual perception range of data bits or bandwidth, will be insufficient. Gamma encoded images is not necessary (and even sometimes counterproductive), the color value of floating-point format has provided an estimate of the number of linear part of the curve. γ correction (Gamma Correction, gamma correction): The so-called gamma correction for the gamma curve is image editing to the method of non-linear tone image editing, the image signal detecting section dark and light portions, and both the proportion increased, thereby improving image contrast effect.

3. Detailed

1. What is Gamma curve correction?

Gamma correction curve What does it mean? Gamma Curve is a special tone curve, when the Gamma value equal to 1, the curve is a straight line and the coordinate axis of 45 °, this time to refer to the same input and output density. Gamma value greater than 1 will cause the output of the lighting, Gamma value less than 1 will cause the output of the dark. In conclusion, our requirements are the input and output ratio of close to 1 as possible. In the monitor, scanner, printer, input and output devices this is a fairly common and important concept. In a computer system, the graphics or the image due to the display of the actual output will deviate in brightness, Gamma curve correction method is to correct the deviation of this image by a certain method. Generally, when the value for the Gamma correction is greater than 1, the high light portion of the image is compressed and darkened portion is extended, when the value of Gamma correction is smaller than 1, the high light portion of the image is expanded and darkened portion is compressed, Gamma correction for general details smooth extended dark tone.

2, Gamma Correction Code
 1 #include <opencv2/core/core.hpp>
 2 #include <opencv2/imgproc/imgproc.hpp>    
 3 #include <opencv2/imgproc/types_c.h>
 4 #include <opencv2/highgui/highgui.hpp>
 5 #include <opencv2/highgui/highgui_c.h>
 6 #include <opencv2/highgui.hpp>
 7 #include <opencv2/calib3d.hpp>
 8 #include <iostream>
 9 10 using namespace cv;
11 using namespace std;
12 13 void GammaCorrection(Mat& src, Mat& dst, float fGamma)
14 {
15     unsigned char lut[256];
16     for (int i = 0; i < 256; i++)
17     {
18         lut[i] = saturate_cast<uchar>(pow((float)(i / 255.0), fGamma) * 255.0f);
19     }
20 21     dst = src.clone();
22     const int channels = dst.channels();
23     switch (channels)
24     {
25         case 1: 
26         {
27 28             MatIterator_<uchar> it, end;
29             for (it = dst.begin<uchar>(), end = dst.end<uchar>(); it != end; it++)
30                 * it = lut[(*it)];
31 32             break;
33         }
34         case 3:
35         {
36 37             MatIterator_<Vec3b> it, end;
38             for (it = dst.begin<Vec3b>(), end = dst.end<Vec3b>(); it != end; it++)
39             {
40                 (*it)[0] = lut[((*it)[0])];
41                 (*it)[1] = lut[((*it)[1])];
42                 (*it)[2] = lut[((*it)[2])];
43             }
44 45             break;
46 47         }
48         default:
49             break;
50     }
51 }
52 53 int main()
54 {
55     Mat image = imread("D://Images/wrong/2.jpg");
56     if (image.empty())
57     {
58         cout << "Error: Could not load image" << endl;
59         return 0;
60     }
61     Mat dst;
62     float fGamma = 1 / 1.8;
63     GammaCorrection(image, dst, fGamma);
64     imwrite("D://Images/wrong/24.jpg", dst);
65     waitKey();
66 67     return 0;
68 }

 

 

 1 package org.jaiken.tools;
 2  3 import org.opencv.core.Core;
 4 import org.opencv.core.CvType;
 5 import org.opencv.core.Mat;
 6 import org.opencv.imgcodecs.Imgcodecs;
 7  8 public class GammaCorrection {
 9     // 伽马校正
10     public Mat gamma(Mat src) {
11         Mat x = new Mat();
12         src.convertTo(x, CvType.CV_32FC3);
13         Mat i = new Mat();
14         
15         Core.pow(x, 1/1.8, i);
16         Mat dst = new Mat();
17         // 归一化
18         Core.normalize(i, dst, 0, 255, Core.NORM_MINMAX, 0);
19         Imgcodecs.imwrite("D://Images/wrong/22.jpg", dst);
20         return dst;
21     }
22 23     public static void main(String[] args) {
24         System.load("D://Export/opencv_java320.dll");
25         Mat src = Imgcodecs.imread("D://Images/wrong/2.jpg",0);
26         if (src.empty()) {
27             System.err.println("The picture doesn't exist");
28             return;
29         }
30         GammaCorrection gammaCorrection = new GammaCorrection();
31         gammaCorrection.gamma(src);
32     }
33 34 }

 


 

Renderings

 


This also can only see a change in image brightness, not experience changes in detail, such a method with a threshold value and detects two image Blob analysis, the following results of FIG.

 

Clearly the results of the fourth column, fifth row of image details more clear, more the result of a good deal, but there is no interference "line" on the image.

Guess you like

Origin www.cnblogs.com/Jaiken/p/10943312.html