How to Use Gamma Correction for Image Processing with Opencv
In reality, we can always see some photos that have low brightnesses and low contrast. To make objects recognizable in pictures, we need to process the photo with Illumination Compensation. There are many algorithms used for Illumination Compensation such as Histogram equalization, Color similarity measure, Gamma Correction and so on. In this tutorial, I will introduce Gamma Correction and show you how to use it with OpenCV.
What is Gamma Correction
Gamma correction was originally designed to compensate for CRT monitors’ non-linear response to the input signal. CRTs were not able to amplify the input signal themselves and thus the output signal from the PC needed to be adjusted, giving rise to (as of today) standard gamma 2.2 correction and sRGB color space. Gamma Correction is the name of a nonlinear operation used to code and decode luminance or tristimulus values in video or still image systems. Here is the definition of Gamma Correction in Wikipedia:
How to Use Gamma Correction with OpenCV
Gamma correction controls the overall brightness of an image. Images that are not corrected can look either bleached out or too dark. We can use this case:
1
2
3
4
5
| R = pow (R, 1/Gamma) G = pow (G, 1/Gamma) B = pow (B, 1/Gamma) |
The algorithm can be implemented with the following code, which can process images that have one or three channels.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
| void GammaCorrection(Mat& src, Mat& dst, float fGamma) { unsigned char lut[256]; for ( int i = 0; i < 256; 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 ; } } } |
How to Build and Run OpenCV Program on Ubuntu
Follow the official tutorial to install OpenCV 3.0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| #!/bin/bashd #https://help.ubuntu.com/community/OpenCV version= "$(wget -q -O - http://sourceforge.net/projects/opencvlibrary/files/opencv-unix | egrep -m1 -o '\"[0-9](\.[0-9]+)+' | cut -c2-)" echo "Installing OpenCV" $version mkdir OpenCV cd OpenCV echo "Removing any pre-installed ffmpeg and x264" sudo apt-get -qq remove ffmpeg x264 libx264-dev echo "Installing Dependenices" sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils ffmpeg cmake qt5- default checkinstall echo "Downloading OpenCV" $version wget -O OpenCV-$version.zip http: //sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download echo "Installing OpenCV" $version unzip OpenCV-$version.zip cd opencv-$version mkdir build cd build cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON .. make -j2 sudo checkinstall sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf' sudo ldconfig echo "OpenCV" $version "ready to be used" |
Build the C++ program with g++:
1
| g++ -ggdb `pkg-config --cflags opencv` -o `basename gamma.cpp .cpp` gamma.cpp `pkg-config --libs opencv` |