Skip to content

Commit 29ba092

Browse files
committed
Update Gray World.cpp
1 parent 5ce718b commit 29ba092

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

Correction algorithm/Gray World.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "opencv2/opencv.hpp"
2+
#include "opencv2/imgproc/imgproc.hpp"
3+
#include "iostream"
4+
#include "algorithm"
5+
#include "vector"
6+
#include "stdio.h"
7+
#include "map"
8+
#include "unordered_map"
9+
using namespace std;
10+
using namespace cv;
11+
12+
Mat GrayWorld(const Mat &src){
13+
vector <Mat> bgr;
14+
cv::split(src, bgr);
15+
double B = 0;
16+
double G = 0;
17+
double R = 0;
18+
int row = src.rows;
19+
int col = src.cols;
20+
Mat dst(row, col, CV_8UC3);
21+
for(int i = 0; i < row; i++){
22+
for(int j = 0; j < col; j++){
23+
B += 1.0 * src.at<Vec3b>(i, j)[0];
24+
G += 1.0 * src.at<Vec3b>(i, j)[1];
25+
R += 1.0 * src.at<Vec3b>(i, j)[2];
26+
}
27+
}
28+
B /= (row * col);
29+
G /= (row * col);
30+
R /= (row * col);
31+
printf("%.5f %.5f %.5f\n", B, G, R);
32+
double GrayValue = (B + G + R) / 3;
33+
printf("%.5f\n", GrayValue);
34+
double kr = GrayValue / R;
35+
double kg = GrayValue / G;
36+
double kb = GrayValue / B;
37+
printf("%.5f %.5f %.5f\n", kb, kg, kr);
38+
for(int i = 0; i < row; i++){
39+
for(int j = 0; j < col; j++){
40+
dst.at<Vec3b>(i, j)[0] = (int)(kb * src.at<Vec3b>(i, j)[0]);
41+
dst.at<Vec3b>(i, j)[1] = (int)(kg * src.at<Vec3b>(i, j)[1]);
42+
dst.at<Vec3b>(i, j)[2] = (int)(kr * src.at<Vec3b>(i, j)[2]);
43+
for(int k = 0; k < 3; k++){
44+
if(dst.at<Vec3b>(i, j)[k] > 255){
45+
dst.at<Vec3b>(i, j)[k] = 255;
46+
}
47+
}
48+
}
49+
}
50+
return dst;
51+
}
52+
53+
54+
55+
int main(){
56+
Mat src = cv::imread("../1.jpg");
57+
Mat dst = GrayWorld(src);
58+
cv::imshow("origin", src);
59+
cv::imshow("result", dst);
60+
cv::imwrite("../result.jpg", dst);
61+
cv::waitKey(0);
62+
return 0;
63+
}

Correction algorithm/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# OpenCV 和 C++ 实现一些矫正算法
22

33
- GammaCorrection.cpp Gamma 矫正C++实现。原理附在了代码中
4-
54
- HistograEqualization.cpp 直方图均衡化C++实现。原理请看:https://blog.csdn.net/just_sort/article/details/85013803
6-
7-
- ACE.cpp 对《Real-time adptive contrast enhancement for imaging sensors》论文的C++复现,也就是自适应局部对比度增强。原理请看:https://blog.csdn.net/just_sort/article/details/85208124
5+
- ACE.cpp 对《Real-time adptive contrast enhancement for imaging sensors》论文的C++复现,也就是自适应局部对比度增强。原理请看:https://blog.csdn.net/just_sort/article/details/85208124
6+
- Gray World.cpp 对灰度世界算法的实现。原理请看:https://blog.csdn.net/just_sort/article/details/85638420

0 commit comments

Comments
 (0)