-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.cpp
69 lines (63 loc) · 1.61 KB
/
preprocessing.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
#include "preprocessing.h"
constexpr int MAXPIX = 255;
uchar add_overflow(uchar pix, int value) {
if (pix + value > MAXPIX)
pix = MAXPIX;
else if (pix + value < 0) {
pix = 0;
}
else pix = pix + value;
return pix;
}
uchar mul_overflow(uchar pix, float value) {
value = (float)pix * value;
if (value > MAXPIX)
value = MAXPIX;
else if (value < 0) {
value = 0;
}
return static_cast<uchar>(value);
}
cv::Mat& lighten_rgb(cv::Mat& I, int degree){
CV_Assert(I.depth() != sizeof(uchar));
switch(I.channels()) {
case 1:
for (int i = 0; i < I.rows; ++i)
for (int j = 0; j < I.cols; ++j)
I.at<uchar>(i, j) = add_overflow(I.at<uchar>(i,j), degree);
break;
case 3:
cv::Mat_<cv::Vec3b> _I = I;
for( int i = 0; i < I.rows; ++i)
for( int j = 0; j < I.cols; ++j ){
_I(i,j)[0] = add_overflow(_I(i, j)[0], degree);
_I(i,j)[1] = add_overflow(_I(i, j)[1], degree);
_I(i,j)[2] = add_overflow(_I(i, j)[2], degree);
}
I = _I;
break;
}
return I;
}
cv::Mat& contrast_rgb(cv::Mat& I, int degree) {
CV_Assert(I.depth() != sizeof(uchar));
float cval = 1 + (float)degree / 128;
switch (I.channels()) {
case 1:
for (int i = 0; i < I.rows; ++i)
for (int j = 0; j < I.cols; ++j)
I.at<uchar>(i, j) = mul_overflow(I.at<uchar>(i, j), cval);
break;
case 3:
cv::Mat_<cv::Vec3b> _I = I;
for (int i = 0; i < I.rows; ++i)
for (int j = 0; j < I.cols; ++j) {
_I(i, j)[0] = mul_overflow(_I(i, j)[0], cval);
_I(i, j)[1] = mul_overflow(_I(i, j)[1], cval);
_I(i, j)[2] = mul_overflow(_I(i, j)[2], cval);
}
I = _I;
break;
}
return I;
}