forked from BBuf/Image-processing-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoLevelAndAutoContrast.cpp
241 lines (236 loc) · 6.71 KB
/
AutoLevelAndAutoContrast.cpp
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "opencv2/opencv.hpp"
#include "iostream"
#include "algorithm"
#include "vector"
using namespace std;
using namespace cv;
Mat AutoLevel(Mat src, double LowCut, double HighCut){
int rows = src.rows;
int cols = src.cols;
int totalPixel = rows * cols;
//统计每个通道的直方图
uchar Pixel[256*3] = {0};
vector <Mat> rgb;
split(src, rgb);
Mat HistBlue, HistGreen, HistRed;
int histSize = 256;
float range[] = {0, 255};
const float* histRange = {range};
bool uniform = true;
bool accumulate = false;
calcHist(&rgb[0], 1, 0, Mat(), HistRed, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&rgb[1], 1, 0, Mat(), HistGreen, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&rgb[2], 1, 0, Mat(), HistBlue, 1, &histSize, &histRange, uniform, accumulate);
//分别计算各通道按照给定的参数所确定的上下限值
int MinBlue = 0, MaxBlue = 0;
int MinRed = 0, MaxRed = 0;
int MinGreen = 0, MaxGreen = 0;
//Blue Channel
float sum = 0;
sum = 0;
for(int i = 0; i < 256; i++){
sum += HistBlue.at<float>(i);
if(sum >= totalPixel * LowCut * 0.01){
MinBlue = i;
break;
}
}
sum = 0;
for(int i = 255; i >= 0; i--){
sum = sum + HistBlue.at<float>(i);
if(sum >= totalPixel * HighCut * 0.01){
MaxBlue = i;
break;
}
}
//Red channel
for(int i = 0; i < 256; i++){
sum += HistRed.at<float>(i);
if(sum >= totalPixel * LowCut * 0.01){
MinRed = i;
break;
}
}
sum = 0;
for(int i = 255; i >= 0; i--){
sum = sum + HistRed.at<float>(i);
if(sum >= totalPixel * HighCut * 0.01){
MaxRed = i;
break;
}
}
//Green channel
sum = 0;
for(int i = 0; i < 256; i++){
sum += HistGreen.at<float>(i);
if(sum >= totalPixel * LowCut * 0.01){
MinGreen = i;
break;
}
}
sum = 0;
for(int i = 255; i >= 0; i--){
sum = sum + HistGreen.at<float>(i);
if(sum >= totalPixel * HighCut * 0.01){
MaxGreen = i;
break;
}
}
printf("%d %d %d %d %d %d\n", MinGreen, MaxGreen, MinBlue, MaxBlue, MinRed, MaxRed);
//自动色阶:按照我们刚刚计算出的MinBlue/MaxBlue构建一个隐射表
for(int i = 0; i < 256; i++){
if(i <= MinBlue){
Pixel[i*3+2] = 0;
}else{
if(i > MaxBlue){
Pixel[i*3+2] = 255;
}else{
float temp = (float)(i - MinBlue) / (MaxBlue - MinBlue);
Pixel[i*3+2] = (uchar)(temp*255);
}
}
if(i <= MinGreen){
Pixel[i*3+1] = 0;
}else{
if(i > MaxGreen){
Pixel[i*3+1] = 255;
}else{
float temp = (float)(i - MinGreen) / (MaxGreen - MinGreen);
Pixel[i*3+1] = (uchar)(temp*255);
}
}
if(i <= MinRed){
Pixel[i*3] = 0;
}else{
if(i > MaxRed){
Pixel[i*3] = 255;
}else{
float temp = (float)(i - MinRed) / (MaxRed - MinRed);
Pixel[i*3] = (uchar)(temp*255);
}
}
}
Mat dst;
Mat TMP(1, 256, CV_8UC3, Pixel);
LUT(src, TMP, dst);
return dst;
}
Mat AutoContrast(Mat src, double LowCut, double HighCut){
int rows = src.rows;
int cols = src.cols;
int totalPixel = rows * cols;
//统计每个通道的直方图
uchar Pixel[256*3] = {0};
vector <Mat> rgb;
split(src, rgb);
Mat HistBlue, HistGreen, HistRed;
int histSize = 256;
float range[] = {0, 255};
const float* histRange = {range};
bool uniform = true;
bool accumulate = false;
calcHist(&rgb[0], 1, 0, Mat(), HistRed, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&rgb[1], 1, 0, Mat(), HistGreen, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&rgb[2], 1, 0, Mat(), HistBlue, 1, &histSize, &histRange, uniform, accumulate);
//分别计算各通道按照给定的参数所确定的上下限值
int MinBlue = 0, MaxBlue = 0;
int MinRed = 0, MaxRed = 0;
int MinGreen = 0, MaxGreen = 0;
//Blue Channel
float sum = 0;
sum = 0;
for(int i = 0; i < 256; i++){
sum += HistBlue.at<float>(i);
if(sum >= totalPixel * LowCut * 0.01){
MinBlue = i;
break;
}
}
sum = 0;
for(int i = 255; i >= 0; i--){
sum = sum + HistBlue.at<float>(i);
if(sum >= totalPixel * HighCut * 0.01){
MaxBlue = i;
break;
}
}
//Red channel
for(int i = 0; i < 256; i++){
sum += HistRed.at<float>(i);
if(sum >= totalPixel * LowCut * 0.01){
MinRed = i;
break;
}
}
sum = 0;
for(int i = 255; i >= 0; i--){
sum = sum + HistRed.at<float>(i);
if(sum >= totalPixel * HighCut * 0.01){
MaxRed = i;
break;
}
}
//Green channel
sum = 0;
for(int i = 0; i < 256; i++){
sum += HistGreen.at<float>(i);
if(sum >= totalPixel * LowCut * 0.01){
MinGreen = i;
break;
}
}
sum = 0;
for(int i = 255; i >= 0; i--){
sum = sum + HistGreen.at<float>(i);
if(sum >= totalPixel * HighCut * 0.01){
MaxGreen = i;
break;
}
}
int minn = min(MinBlue, min(MinGreen, MinRed));
int maxx = max(MaxBlue, max(MaxGreen, MaxRed));
//自动对比度
for(int i = 0; i < 256; i++){
if(i <= minn){
Pixel[i*3+2] = 0;
}else{
if(i > maxx){
Pixel[i*3+2] = 255;
}else{
float temp = (float)(i - minn) / (maxx - minn);
Pixel[i*3+2] = (uchar)(temp*255);
}
}
if(i <= minn){
Pixel[i*3+1] = 0;
}else{
if(i > maxx){
Pixel[i*3+1] = 255;
}else{
float temp = (float)(i - minn) / (maxx - minn);
Pixel[i*3+1] = (uchar)(temp*255);
}
}
if(i <= minn){
Pixel[i*3] = 0;
}else{
if(i > maxx){
Pixel[i*3] = 255;
}else{
float temp = (float)(i - minn) / (maxx - minn);
Pixel[i*3] = (uchar)(temp*255);
}
}
}
Mat dst;
Mat TMP(1, 256, CV_8UC3, Pixel);
LUT(src, TMP, dst);
return dst;
}
int main(){
Mat src = imread("../3.png");
Mat dst = AutoLevel(src, 0.005, 0.005);
imshow("origin", src);
imshow("result", dst);
waitKey(0);
}