-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhite_balance2.cpp
185 lines (143 loc) · 5.32 KB
/
white_balance2.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
#include <stdio.h>
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
Mat clahe_conversion(Mat input)
{
vector<Mat> RGB; // Use the STL’s vector structure to store multiple Mat objects
split(input, RGB); // split the image into separate color planes (R G B)
//// Enhance Local Contrast (CLAHE)
Ptr<CLAHE> clahe = createCLAHE();
clahe->setClipLimit(4);
Mat RGB_eq;
//// Equalizes the histogram of a one channel image (8UC1) using Contrast Limited Adaptive Histogram Equalization.
clahe->apply(RGB[0],RGB[0]);
clahe->apply(RGB[1],RGB[1]);
clahe->apply(RGB[2],RGB[2]);
merge(RGB,RGB_eq); // now merge the results back
return RGB_eq;
}
void gray_world(Mat src1,float *ml,float *ma, float *mb, int p)
{
*ma=0;
*mb=0;
*ml=0;
for(int i=0;i<src1.rows;i++)
{
for(int j=0;j<src1.cols;j++)
{
Vec3b v1=src1.at<Vec3b>(i,j);
float lc=pow(v1.val[0],p);
float ac=pow(v1.val[1],p);
float bc=pow(v1.val[2],p);
*ma=*ma+ac;
*mb=*mb+bc;
*ml=*ml+lc;
}
}
*ma=pow((float)*ma/(src1.cols*src1.rows),(float)1/p);
*mb=pow((float)*mb/(src1.cols*src1.rows),(float)1/p);
*ml=pow((float)*ml/(src1.cols*src1.rows),(float)1/p);
}
int main(){
Mat image = imread("/home/shaggy/line_auv/rippleJPG.jpg");
Mat image_hsv;
namedWindow("ColorBalance",CV_WINDOW_NORMAL);
//image = clahe_conversion(image);
vector<Mat> bgr_planes;
Mat src = image;
Mat dst;
src.copyTo(dst);
Mat src1 = src;
split(src,bgr_planes);
float ma=0,mb=0,ml=0;
int p=1;
int m=1;
gray_world(src,&ml,&ma,&mb,p);
float r=(ma+mb+ml)/3;
if(m==1)
{
r=(ma+mb+ml)/3;
r=max(ma,mb);
r=max(r,ml);
}
MatIterator_<Vec3b> it=src1.begin<Vec3b>();
MatIterator_<Vec3b> itend=src1.end<Vec3b>();
MatIterator_<Vec3b> itout=dst.begin<Vec3b>();
for (;it!=itend;++it,++itout)
{
Vec3b v1=*it;
float l=v1.val[0];
float a=v1.val[1];
float b=v1.val[2];
a=a*(r/ma);
b=b*(r/mb);
l=l*(r/ml);
if(a>255)
a=255;
if(b>255)
b=255;
if(l>255)
l=255;
v1.val[0]=l;
v1.val[1]=a;
v1.val[2]=b;
*itout=v1;
}
// cvtColor(dst,dst,CV_Lab2BGR);
imshow("ColorBalance",dst);
/*cvtColor(image, image_hsv, COLOR_BGR2HSV);
/*for(int y=0; y<image_hsv.cols; y++){
for(int x=0; x<image_hsv.rows; x++){
if(image_hsv.at<uchar>(x,y)[1] >=0 && image_hsv.at<uchar>(x,y)[1] <20 ){
image_hsv.at<uchar>(x,y)[1] = 255;
}
else
image_hsv.at<uchar>(x,y)[1] = 0;
}
}
vector<Mat>hsv;
split(image_hsv, hsv);
imwrite("/home/harsha/Pictures/clahee.JPG",image);
imshow("hsv",image_hsv);
imshow("normal", image);
imshow("h",hsv[0]);
imshow("saturation", hsv[1]);
Mat hsv_image = hsv[0];
Mat hsv_image1 = hsv[1];
for(int y=0; y<hsv_image.cols; y++){
for(int x=0; x<hsv_image.rows; x++){
if(hsv_image.at<uchar>(x,y) >=0 && hsv_image.at<uchar>(x,y) <25 || hsv_image.at<uchar>(x,y) >160 && hsv_image.at<uchar>(x,y)<180){
hsv_image.at<uchar>(x,y) = 255;
}
else
hsv_image.at<uchar>(x,y) = 0;
}
}
for(int y=0; y<hsv_image1.cols; y++){
for(int x=0; x<hsv_image1.rows; x++){
if(hsv_image1.at<uchar>(x,y) >=0 && hsv_image1.at<uchar>(x,y) <20 ){
hsv_image1.at<uchar>(x,y) = 0;
}
else
hsv_image1.at<uchar>(x,y);
}
}
for(int y=0; y<hsv_image.cols; y++){
for(int x=0; x<hsv_image.rows; x++){
if(hsv_image1.at<uchar>(x,y) == 0){
hsv_image.at<uchar>(x,y) = 0;
}
else
hsv_image.at<uchar>(x,y);
}
}
imshow("saturation1",hsv_image1);
imshow("path",hsv_image);
*/
waitKey(0);
return 0;
}