-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathGray World.cpp
63 lines (59 loc) · 1.67 KB
/
Gray World.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
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
#include "algorithm"
#include "vector"
#include "stdio.h"
#include "map"
#include "unordered_map"
using namespace std;
using namespace cv;
Mat GrayWorld(const Mat &src){
vector <Mat> bgr;
cv::split(src, bgr);
double B = 0;
double G = 0;
double R = 0;
int row = src.rows;
int col = src.cols;
Mat dst(row, col, CV_8UC3);
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
B += 1.0 * src.at<Vec3b>(i, j)[0];
G += 1.0 * src.at<Vec3b>(i, j)[1];
R += 1.0 * src.at<Vec3b>(i, j)[2];
}
}
B /= (row * col);
G /= (row * col);
R /= (row * col);
printf("%.5f %.5f %.5f\n", B, G, R);
double GrayValue = (B + G + R) / 3;
printf("%.5f\n", GrayValue);
double kr = GrayValue / R;
double kg = GrayValue / G;
double kb = GrayValue / B;
printf("%.5f %.5f %.5f\n", kb, kg, kr);
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
dst.at<Vec3b>(i, j)[0] = (int)(kb * src.at<Vec3b>(i, j)[0]);
dst.at<Vec3b>(i, j)[1] = (int)(kg * src.at<Vec3b>(i, j)[1]);
dst.at<Vec3b>(i, j)[2] = (int)(kr * src.at<Vec3b>(i, j)[2]);
for(int k = 0; k < 3; k++){
if(dst.at<Vec3b>(i, j)[k] > 255){
dst.at<Vec3b>(i, j)[k] = 255;
}
}
}
}
return dst;
}
int main(){
Mat src = cv::imread("../1.jpg");
Mat dst = GrayWorld(src);
cv::imshow("origin", src);
cv::imshow("result", dst);
cv::imwrite("../result.jpg", dst);
cv::waitKey(0);
return 0;
}