-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgproc.cpp
392 lines (289 loc) · 10.8 KB
/
imgproc.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
* Author: Zhentao Huang
* Email: [email protected]
*/
#include "imgproc.h"
namespace speedbot2d{
/**
* @brief 调整图片的亮度和对比度
*
* @param in_image 输入的需要调整的图片
* @param alpha 对比度参数,越大对比度越大
* @param beta 亮度参数,越大亮度越大
* @return cv::Mat 输出调整后的图片
*/
cv::Mat AdjustBrightness(const cv::Mat in_image, float alpha=15, float beta=0)
{
cv::Mat out_image = cv::Mat::zeros(in_image.size(), in_image.type()); //创建一个和原图像大小相同,类型相同,像素值为0的图像
//对每个像素点的操作
for (int i = 0; i < in_image.rows; i++) {
for (int j = 0; j < in_image.cols; j++) {
if (in_image.channels() == 3) { //如果是RGB图像
out_image.at<cv::Vec3b>(i, j)[0] = cv::saturate_cast<uchar>(alpha*(in_image.at<cv::Vec3b>(i, j)[0]) + beta);
out_image.at<cv::Vec3b>(i, j)[1] = cv::saturate_cast<uchar>(alpha*(in_image.at<cv::Vec3b>(i, j)[1]) + beta);
out_image.at<cv::Vec3b>(i, j)[2] = cv::saturate_cast<uchar>(alpha*(in_image.at<cv::Vec3b>(i, j)[2]) + beta);
} else if (in_image.channels() == 1) { //如果是灰度图像
out_image.at<uchar>(i,j) = cv::saturate_cast<uchar>(alpha*(in_image.at<uchar>(i, j)) + beta);
}
}
}
return out_image;
}
/**
* @brief 图片Gamma矫正
*
* @param in_image 需要矫正的图片
* @param gamma 矫正的gamma参数
* @return cv::Mat 矫正后的图片
*/
cv::Mat GammaCorrection(const cv::Mat in_image, float gamma=2.5)
{
cv::Mat out_image = cv::Mat::zeros(in_image.size(), in_image.type());
float inverse_gamma = 1 / gamma;
for(int i = 0; i < in_image.rows; i++) {
for(int j = 0; j < in_image.cols; j++) {
if(in_image.channels() == 3) { //如果是RGB图像
out_image.at<cv::Vec3b>(i,j)[0] = cv::saturate_cast<uchar>(pow(in_image.at<cv::Vec3b>(i,j)[0] / 255.0, inverse_gamma) * 255.0);
out_image.at<cv::Vec3b>(i,j)[1] = cv::saturate_cast<uchar>(pow(in_image.at<cv::Vec3b>(i,j)[1] / 255.0, inverse_gamma) * 255.0);
out_image.at<cv::Vec3b>(i,j)[2] = cv::saturate_cast<uchar>(pow(in_image.at<cv::Vec3b>(i,j)[2] / 255.0, inverse_gamma) * 255.0);
} else if(in_image.channels() == 1) { //如果是灰度图像
out_image.at<uchar>(i,j) = cv::saturate_cast<uchar>(pow(in_image.at<uchar>(i,j) / 255.0, inverse_gamma) * 255.0);
}
}
}
return out_image;
}
/**
* @brief 供本地测试用
*
* @return cv::Mat 图片
*/
cv::Mat ReadImg()
{
cv::Mat out_image = cv::imread("../data/autothres.png", cv::IMREAD_UNCHANGED);
return out_image;
}
/**
* @brief 供本地测试用
*
* @param in_image
* @return int
*/
int WriteImg(cv::Mat in_image)
{
cv::imwrite("../data/output.png", in_image);
return 0;
}
/**
* @brief 根据图片的色彩进行对比度拉伸,不需要输入参数
*
* @param in_image 输入需要调整的图片
* @return cv::Mat 输出调整后的图片
*/
cv::Mat StretchContrast(const cv::Mat in_image)
{
cv::Mat out_image = cv::Mat::zeros(in_image.size(), in_image.type()); //创建一个和原图像大小相同,类型相同,像素值为0的图像
if(in_image.channels() == 3){ //如果是RGB图像
cv::Mat image_rgb[3];
cv::split(in_image, image_rgb); //将三通道图像分为单通道分别处理
double min_value_r, max_value_r, min_value_g, max_value_g, min_value_b, max_value_b; // 最大值,最小值
cv::minMaxLoc(image_rgb[0], &min_value_b, &max_value_b); //求各通道的最大值和最小值
cv::minMaxLoc(image_rgb[1], &min_value_g, &max_value_g);
cv::minMaxLoc(image_rgb[2], &min_value_r, &max_value_r);
if(max_value_b == min_value_b || max_value_g == min_value_g || max_value_r == min_value_r) {
return in_image;
} else {
for (int i = 0; i < in_image.rows; i++) { //对每个像素点的操作
for (int j = 0; j < in_image.cols; j++) { // x = 255 * (x - min) / range;
out_image.at<cv::Vec3b>(i, j)[0] = cv::saturate_cast<uchar>(255 * (in_image.at<cv::Vec3b>(i, j)[0] - min_value_b) / (max_value_b - min_value_b));
out_image.at<cv::Vec3b>(i, j)[1] = cv::saturate_cast<uchar>(255 * (in_image.at<cv::Vec3b>(i, j)[1] - min_value_g) / (max_value_g - min_value_g));
out_image.at<cv::Vec3b>(i, j)[2] = cv::saturate_cast<uchar>(255 * (in_image.at<cv::Vec3b>(i, j)[2] - min_value_r) / (max_value_r - min_value_r));
}
}
}
} else if (in_image.channels() == 1) { //如果是灰度图像
double min_value, max_value; //最大值,最小值
cv::minMaxLoc(in_image, &min_value, &max_value); //求各通道的最大值和最小值
std::cout<<min_value<<std::endl<<max_value<<std::endl;
if(max_value == min_value) { //如果range 为 0
return in_image;
} else {
float divisor = 0.04;
std::cout<<in_image.cols<<std::endl;
for (int i = 0; i < in_image.rows; i++) { //对每个像素点的操作
for (int j = 0; j < 2560; j++) { // x = 255 * (x - min) / range;
//std::cout<<(int)in_image.at<uchar>(i,j)<<" ";
//out_image.at<uchar>(i,j) = cv::saturate_cast<uchar>(255 * (in_image.at<uchar>(i,j) - min_value) / (max_value - min_value));
out_image.at<uchar>(i,j) = cv::saturate_cast<uchar>(in_image.at<uchar>(i,j)/divisor );
// std::cout<<(int)out_image.at<uchar>(i,j)<<std::endl;
}
}
}
}
return out_image;
}
/**
* @brief 获取输入图片的颜色直方图作为特征之一方便后续处理
*
* @param in_image 输入图片
* @return cv::Mat 输出颜色直方图
*/
cv::Mat GetHistogram(const cv::Mat& in_image)
{
int histSize[1] = {256};
float hranges[2] = {0.0, 256.0};
const float* ranges[1] = {hranges};
int channels[1] = {0};
cv::Mat hist;
cv::calcHist(&in_image,
1, //仅为一个图像的直方图
channels, //使用的通道
cv::Mat(),//不使用掩码
hist, //作为结果的直方图
1, //这时一维的直方图
histSize, //箱子数量
ranges //像素值的范围
);
return hist;
}
/**
* @brief 计算用于图像二值化的阈值 -- Huang
*
* @param hist 输入的颜色直方图
* @return int 阈值
*/
int ComputeThresholdHuang(cv::Mat hist)
{
// Code ported from the AutoThreshold ImageJ plugin:
// Implements Huang's fuzzy thresholding method
// Uses Shannon's entropy function (one can also use Yager's entropy
// function) Huang L.-K. and Wang M.-J.J. (1995) "Image Thresholding by
// Minimizing the Measures of Fuzziness" Pattern Recognition, 28(1): 41-51
// Reimplemented (to handle 16-bit efficiently) by Johannes Schindelin Jan
// 31, 2011
// 找出直方图中最大最小值
size_t first, last;
for (first = 0; first < 256 && hist.at<float>(first) == 0; first++) {
// do nothing
}
for (last = 256 - 1; last > first && hist.at<float>(last) == 0; last--) {
// do nothing
}
if (first == last) {
return 0;
}
// Calculate the cumulative density and the weighted cumulative density
std::vector<double> S(last + 1); //实践发现5000x3000以上的图片float类型很有可能溢出,故改成double类型
std::vector<double> W(last + 1);
S[0] = hist.at<float>(0);
W[0] = 0.0f;
for (size_t i = std::max((size_t)1, first); i <= last; i++) {
S[i] = S[i - 1] + hist.at<float>((unsigned char)i);
W[i] = W[i - 1] + i * hist.at<float>((unsigned char)i);
}
// Precalculate the summands of the entropy given the absolute difference x
// - mu (integral)
float C = (float)(last - first);
std::vector<float> Smu(last + 1 - first);
for (size_t i = 1; i < Smu.size(); i++) {
float mu = 1 / (1 + i / C);
Smu[i] = -mu * std::log(mu) - (1 - mu) * std::log(1 - mu);
}
// Calculate the threshold
int bestThreshold = 0;
float bestEntropy = std::numeric_limits<float>::max();
for (size_t threshold = first; threshold <= last; threshold++) {
float entropy = 0;
int mu = round(W[threshold] / S[threshold]);
for (size_t i = first; i <= threshold; i++) {
entropy += Smu[(size_t)std::abs((int)i - mu)] * hist.at<float>((unsigned char)i);
}
mu = round((W[last] - W[threshold]) / (S[last] - S[threshold]));
for (size_t i = threshold + 1; i <= last; i++) {
entropy += Smu[(size_t)std::abs((int)i - mu)] * hist.at<float>((unsigned char)i);
}
if (bestEntropy > entropy) {
bestEntropy = entropy;
bestThreshold = (int)threshold;
}
}
return bestThreshold;
}
/**
* @brief 将灰度图片根据阈值进行二值/三值化
* 如果小于threshold1则设为value1,大于threshold2则设为value3,中间设为value2
*
* @param in_image 输入图片
* @param threshold1 阈值1
* @param threshold2 阈值2
* @param value1 设定值1
* @param value2 设定值2
* @param value3 设定值3
* @return cv::Mat
*/
cv::Mat Binarise(const cv::Mat in_image, unsigned char threshold1, unsigned char threshold2, unsigned char value1, unsigned char value2, unsigned char value3)
{
cv::Mat out_image = cv::Mat::zeros(in_image.size(), in_image.type()); //创建一个和原图像大小相同,类型相同,像素值为0的图像
//对每个像素点的操作
for (int i = 0; i < in_image.rows; i++) {
for (int j = 0; j < in_image.cols; j++) {
if (in_image.channels() == 1) { //如果是灰度图像
if( in_image.at<uchar>(i,j) < threshold1 ){
out_image.at<uchar>(i,j) = cv::saturate_cast<uchar>(value1);
} else if ( in_image.at<uchar>(i,j) > threshold2) {
out_image.at<uchar>(i,j) = cv::saturate_cast<uchar>(value3);
} else {
std::cout<<1<<std::endl;
out_image.at<uchar>(i,j) = cv::saturate_cast<uchar>(value2);
}
}
}
}
return out_image;
}
cv::Mat AutoThresholding(const cv::Mat in_image, AutoThresholdMethod method)
{
int threshold = -1;
cv::Mat hist = GetHistogram(in_image);
switch (method) {
case AUTO_THRESHOLD_HUANG:
std::cout<<7666<<std::endl;
threshold = ComputeThresholdHuang(hist);
std::cout<<threshold<<std::endl;
break;
// case AUTO_THRESHOLD_INTERMODES:
// threshold = computeThresholdIntermodes(histogram);
// break;
// case AUTO_THRESHOLD_ISODATA:
// threshold = computeThresholdIsoData(histogram, I.getSize());
// break;
// case AUTO_THRESHOLD_MEAN:
// threshold = computeThresholdMean(histogram, I.getSize());
// break;
// case AUTO_THRESHOLD_OTSU:
// threshold = computeThresholdOtsu(histogram, I.getSize());
// break;
// case AUTO_THRESHOLD_TRIANGLE:
// threshold = computeThresholdTriangle(histogram);
// break;
default:
break;
}
cv::Mat out_image = in_image;
if (threshold != -1) {
// Threshold
out_image = Binarise(in_image, (unsigned char)threshold, (unsigned char)255, 0, 255, 255);
}
return out_image;
}
int EdgeDetectionSobel(const cv::Mat in_image, cv::Mat &out_image)
{
return 0;
}
int EdgeDetectionLaplacian(const cv::Mat in_image, cv::Mat &out_image)
{
cv::Laplacian(in_image,out_image,-1,3);
//cv::imshow("asd", out_image);
return 0;
}
}