OpenCV实现Photoshop算法(五): 亮度对比度调整

亮度对比度调整
(一)算法
  亮度对比度调整的算法,我在网上找了很多篇,个人觉得以下这个算法效果较好,比较接近Photoshop的效果。
Algorithm of Brightness Contrast transformation
The formula is:
y = [x - 127.5 * (1 - B)] * k + 127.5 * (1 + B);

x is the input pixel value
y is the output pixel value
B is brightness, value range is [-1,1]
k is used to adjust contrast
k = tan( (45 + 44 * c) / 180 * PI );
c is contrast, value range is [-1,1]
 
于是,用OpenCV C++编写了一个 adjustBrightnessContrast()函数,可同时调整亮度、对比度。
 
(二)源码及例程
 
例程如下, 其中包含 adjustBrightnessContrast()函数。
 1 #include <iostream>
 2 #include "opencv2/core.hpp"
 3 #include "opencv2/imgproc.hpp"
 4 #include "opencv2/highgui.hpp"
 5  
 6 using namespace std;
 7 using namespace cv;
 8  
 9  
10 #define SWAP(a, b, t)  do { t = a; a = b; b = t; } while(0)
11 #define CLIP_RANGE(value, min, max)  ( (value) > (max) ? (max) : (((value) < (min)) ? (min) : (value)) )
12 #define COLOR_RANGE(value)  CLIP_RANGE(value, 0, 255)
13  
14 /**
15  * Adjust Brightness and Contrast
16  *
17  * @param src [in] InputArray
18  * @param dst [out] OutputArray
19  * @param brightness [in] integer, value range [-255, 255]
20  * @param contrast [in] integer, value range [-255, 255]
21  *
22  * @return 0 if success, else return error code
23  */
24 int adjustBrightnessContrast(InputArray src, OutputArray dst, int brightness, int contrast)
25 {
26     Mat input = src.getMat();
27     if( input.empty() ) {
28         return -1;
29     }
30  
31     dst.create(src.size(), src.type());
32     Mat output = dst.getMat();
33  
34     brightness = CLIP_RANGE(brightness, -255, 255);
35     contrast = CLIP_RANGE(contrast, -255, 255);
36  
37     /**
38     Algorithm of Brightness Contrast transformation
39     The formula is:
40         y = [x - 127.5 * (1 - B)] * k + 127.5 * (1 + B);
41         x is the input pixel value
42         y is the output pixel value
43         B is brightness, value range is [-1,1]
44         k is used to adjust contrast
45             k = tan( (45 + 44 * c) / 180 * PI );
46             c is contrast, value range is [-1,1]
47     */
48  
49     double B = brightness / 255.;
50     double c = contrast / 255. ;
51     double k = tan( (45 + 44 * c) / 180 * M_PI );
52  
53     Mat lookupTable(1, 256, CV_8U);
54     uchar *p = lookupTable.data;
55     for (int i = 0; i < 256; i++)
56         p[i] = COLOR_RANGE( (i - 127.5 * (1 - B)) * k + 127.5 * (1 + B) );
57  
58     LUT(input, lookupTable, output);
59  
60     return 0;
61 }
62  
63  
64 //=====主程序开始====
65  
66 static string window_name = "photo";
67 static Mat src;
68 static int brightness = 255;
69 static int contrast = 255;
70  
71 static void callbackAdjust(int , void *)
72 {
73     Mat dst;
74     adjustBrightnessContrast(src, dst, brightness - 255, contrast - 255);
75     imshow(window_name, dst);
76 }
77  
78  
79 int main()
80 {
81     src = imread("building.jpg");
82  
83     if ( !src.data ) {
84         cout << "error read image" << endl;
85         return -1;
86     }
87  
88     namedWindow(window_name);
89     createTrackbar("brightness", window_name, &brightness, 2*brightness, callbackAdjust);
90     createTrackbar("contrast", window_name, &contrast, 2*contrast, callbackAdjust);
91     callbackAdjust(0, 0);
92  
93     waitKey();
94  
95     return 0;
96  
运行效果:
原图:
调整参数, 实施亮度对比度调整后:
原文地址:https://www.cnblogs.com/ybqjymy/p/13801353.html