Skip to content

Commit 06e35d5

Browse files
committedApr 9, 2019
Add PerfectReflectionAlgorithm.cpp
1 parent fb1bcb1 commit 06e35d5

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed
 
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
Mat PerfectReflectionAlgorithm(Mat src) {
2+
int row = src.rows;
3+
int col = src.cols;
4+
Mat dst(row, col, CV_8UC3);
5+
int HistRGB[767] = { 0 };
6+
int MaxVal = 0;
7+
for (int i = 0; i < row; i++) {
8+
for (int j = 0; j < col; j++) {
9+
MaxVal = max(MaxVal, (int)src.at<Vec3b>(i, j)[0]);
10+
MaxVal = max(MaxVal, (int)src.at<Vec3b>(i, j)[1]);
11+
MaxVal = max(MaxVal, (int)src.at<Vec3b>(i, j)[2]);
12+
int sum = src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2];
13+
HistRGB[sum]++;
14+
}
15+
}
16+
int Threshold = 0;
17+
int sum = 0;
18+
for (int i = 766; i >= 0; i--) {
19+
sum += HistRGB[i];
20+
if (sum > row * col * 0.1) {
21+
Threshold = i;
22+
break;
23+
}
24+
}
25+
int AvgB = 0;
26+
int AvgG = 0;
27+
int AvgR = 0;
28+
int cnt = 0;
29+
for (int i = 0; i < row; i++) {
30+
for (int j = 0; j < col; j++) {
31+
int sumP = src.at<Vec3b>(i, j)[0] + src.at<Vec3b>(i, j)[1] + src.at<Vec3b>(i, j)[2];
32+
if (sumP > Threshold) {
33+
AvgB += src.at<Vec3b>(i, j)[0];
34+
AvgG += src.at<Vec3b>(i, j)[1];
35+
AvgR += src.at<Vec3b>(i, j)[2];
36+
cnt++;
37+
}
38+
}
39+
}
40+
AvgB /= cnt;
41+
AvgG /= cnt;
42+
AvgR /= cnt;
43+
for (int i = 0; i < row; i++) {
44+
for (int j = 0; j < col; j++) {
45+
int Blue = src.at<Vec3b>(i, j)[0] * MaxVal / AvgB;
46+
int Green = src.at<Vec3b>(i, j)[1] * MaxVal / AvgG;
47+
int Red = src.at<Vec3b>(i, j)[2] * MaxVal / AvgR;
48+
if (Red > 255) {
49+
Red = 255;
50+
}
51+
else if (Red < 0) {
52+
Red = 0;
53+
}
54+
if (Green > 255) {
55+
Green = 255;
56+
}
57+
else if (Green < 0) {
58+
Green = 0;
59+
}
60+
if (Blue > 255) {
61+
Blue = 255;
62+
}
63+
else if (Blue < 0) {
64+
Blue = 0;
65+
}
66+
dst.at<Vec3b>(i, j)[0] = Blue;
67+
dst.at<Vec3b>(i, j)[1] = Green;
68+
dst.at<Vec3b>(i, j)[2] = Red;
69+
}
70+
}
71+
return dst;
72+
}

‎Correction algorithm/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
- GammaCorrection.cpp Gamma 矫正C++实现。原理附在了代码中
44
- HistograEqualization.cpp 直方图均衡化C++实现。原理请看:https://blog.csdn.net/just_sort/article/details/85013803
5-
- Gray World.cpp 对灰度世界算法的实现。原理请看:https://blog.csdn.net/just_sort/article/details/85638420
5+
- Gray World.cpp 对灰度世界算法的C++实现。原理请看:https://blog.csdn.net/just_sort/article/details/85638420
6+
- PerfectReflectionAlgorithm.cpp 对完美反射算法的C++实现。原理请看:https://blog.csdn.net/just_sort/article/details/85982871

0 commit comments

Comments
 (0)
Please sign in to comment.