-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram 3.cpp
More file actions
406 lines (358 loc) · 8.2 KB
/
Program 3.cpp
File metadata and controls
406 lines (358 loc) · 8.2 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "opencv2/highgui.hpp"
#include <math.h>
#include <iostream>
using namespace cv;
using namespace std;
void runOnWindow(int W1, int H1, int W2, int H2, Mat inputImage, char *outName) {
int rows = inputImage.rows;
int cols = inputImage.cols;
vector<Mat> i_planes;
split(inputImage, i_planes);
Mat iB = i_planes[0];
Mat iG = i_planes[1];
Mat iR = i_planes[2];
// double u1, v1, Uw, Vw,Xw,Yw,Zw,X,Y,Z,Ir,Ig,Ib;
// dynamically allocate RGB arrays of size rows x cols
int** R = new int*[rows];
int** G = new int*[rows];
int** B = new int*[rows];
for (int i = 0; i < rows; i++) {
R[i] = new int[cols];
G[i] = new int[cols];
B[i] = new int[cols];
}
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
R[i][j] = iR.at<uchar>(i, j);
G[i][j] = iG.at<uchar>(i, j);
B[i][j] = iB.at<uchar>(i, j);
// cout << R[i][j] << "/" << G[i][j] << "/" << B[i][j] << endl;
}
double** L = new double*[rows];
double** u = new double*[rows];
double** v = new double*[rows];
for (int i = 0; i < rows; i++) {
L[i] = new double[cols];
u[i] = new double[cols];
v[i] = new double[cols];
}
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
//conversion to non-linear RGB
double R1 = R[i][j] / 255.0;
double G1 = G[i][j] / 255.0;
double B1 = B[i][j] / 255.0;
// cout << R1 << "/" << G1 << "/" << B1 << endl;
//inverse gamma correction
if (R1 < 0.03928)
{
R1 = R1 / 12.92;
}
else
{
R1 = pow(((R1 + 0.055) / 1.055), 2.4);
}
if (G1 < 0.03928)
{
G1 = G1 / 12.92;
}
else
{
G1 = pow(((G1 + 0.055) / 1.055), 2.4);
}
if (B1 < 0.03928)
{
B1 = B1 / 12.92;
}
else
{
B1 = pow(((B1 + 0.055) / 1.055), 2.4);
}
//cout << R1 << "/" << G1 << "/" << B1 << endl;
double X, Y, Z;
//XYZ conversion
X = (0.412453*R1) + (0.35758*G1) + (0.180423*B1);
Y = (0.212671*R1) + (0.71516*G1) + (0.072169*B1);
Z = (0.019334*R1) + (0.119193*G1) + (0.950227*B1);
// cout << X <<"/" << Y << "/" << Z << endl;
double Xw = 0.95;
double Yw = 1.0;
double Zw = 1.09;
double Uw = (4 * Xw) / (Xw + (15 * Yw) + (3 * Zw));
double Vw = (9 * Yw) / (Xw + (15 * Yw) + (3 * Zw));
//cout << Uw << "/" << Vw <<endl;
//Luv conversion
if (Y > 0.008856)
{
L[i][j] = (116 * pow(Y, (1 / 3.0))) - 16;
}
else
{
L[i][j] = 903.3*Y;
}
//clipping L between 0 to 100
if (L[i][j] > 100)
{
L[i][j] = 100;
}
else if (L[i][j] < 0)
{
L[i][j] = 0;
}
double d = X + (15 * Y) + (3 * Z);
double u1, v1;
if (d == 0)
{
u1 = 0;
v1 = 0;
}
else
{
u1 = (4 * X) / d;
v1 = (9 * Y) / d;
}
u[i][j] = 13 * L[i][j] * (u1 - Uw);
v[i][j] = 13 * L[i][j] * (v1 - Vw);
L[i][j] = round(L[i][j]); //rounding the L value, used for histogram equalization
}
//min and max of L
double min = L[H1][W1];
double max = L[H1][W1];
for (int i = H1; i <= H2; i++)
for (int j = W1; j <= W2; j++) {
if (L[i][j]>max)
{
max = L[i][j];
}
if (L[i][j] < min)
{
min = L[i][j];
}
}
//cout << max << "/" << min << endl;
int h[101];
for (int a = 0; a < 101; a++) //initialising the array to 0, so that there are no garbage values
{
h[a] = 0;
}
//h[i] values
for (int i = H1; i <= H2; i++) {
for (int j = W1; j <= W2; j++) {
for (int i1 = 0; i1 < 101; i1++) {
if (L[i][j] == i1)
{
h[i1]++;
}
}
}
}
int f[101];
for (int a = 0; a < 101; a++) //initialising the array to 0, so that there are no garbage values
{
f[a] = 0;
}
f[0] = h[0];
for (int i1 = 1; i1 < 101; i1++)
{
f[i1] = h[i1] + f[i1 - 1];
}
double hist[101];
for (int a = 0; a < 101; a++) //initialising the array to 0, so that there are no garbage values
{
hist[a] = 0;
}
//histogram equalization
hist[0] = ((f[0]) * 101) / (2 * f[100]);
hist[0] = floor(hist[0]);
for (int i1 = 1; i1 < 101; i1++) {
hist[i1] = ((f[i1 - 1] + f[i1]) * 101.0) / (2.0 * f[100]);
hist[i1] = floor(hist[i1]);
if (hist[i1] < 0) {
hist[i1] = 0;
}
else if (hist[i1] > 100) {
hist[i1] = 100;
}
}
for (int i = H1; i <= H2; i++) {
for (int j = W1; j <= W2; j++) {
for (int i1 = 0; i1 < 101; i1++) {
if (L[i][j] == i1)
{
L[i][j] = hist[i1];
break;
}
//cout << i << "/" << j << endl;
}
}
}
// cout << max << "/" << min << endl;
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
if (L[i][j] >= max)
{
L[i][j] = 100;
}
if (L[i][j] <= min)
{
L[i][j] = 0;
}
double x, y, z;
// cout << L[i][j]<< "/" << u[i][j] << "/" << v[i][j]<< endl;
double Xw = 0.95;
double Yw = 1.0;
double Zw = 1.09;
double u1, v1;
double Uw = (4 * Xw) / (Xw + (15 * Yw) + (3 * Zw));
double Vw = (9 * Yw) / (Xw + (15 * Yw) + (3 * Zw));
if (L[i][j] == 0) {
u1 = 0;
v1 = 0;
}
else {
u1 = (u[i][j] + (13 * Uw * L[i][j])) / (13 * L[i][j]);
v1 = (v[i][j] + (13 * Vw * L[i][j])) / (13 * L[i][j]);
}
// cout << L[1][2]<< "/" << u[1][2] << "/" << v[1][2]<< endl;
//Luv to XYZ conversion
if (L[i][j] > 7.9996)
{
y = (pow(((L[i][j] + 16) / 116), 3));
}
else
{
y = (L[i][j] / 903.3);
}
// cout << y << endl;
if (v1 == 0)
{
x = 0;
z = 0;
}
else
{
x = y*(2.25)*(u1 / v1);
z = (y*(3 - (0.75 * u1) - (5 * v1))) / v1;
}
// cout << x << "/" << y << "/" << z << endl;
//linear RGB
double r = (3.240479 * x) + (-1.53715 * y) + (-0.498535 * z);
double g = (-0.969256 * x) + (1.875991 * y) + (0.041556 * z);
double b = (0.055648 * x) + (-0.204043 * y) + (1.057311 * z);
//linear to non-linear RGB - gamma correction
if (r < 0.00304)
{
r = 12.92*r;
}
else
{
r = (1.055*pow(r, (0.4167))) - 0.055;
}
if (g < 0.00304)
{
g = 12.92*g;
}
else
{
g = (1.055*pow(g, (0.4167))) - 0.055;
}
if (b < 0.00304)
{
b = 12.92*b;
}
else
{
b = (1.055*pow(b, (0.4167))) - 0.055;
}
//cout << Ir << "/" << Ig << "/" << Ib << endl;
//clipping
if (r > 1)
{
r = 1;
}
else if (r < 0)
{
r = 0;
}
if (g > 1)
{
g = 1;
}
else if (g < 0)
{
g = 0;
}
if (b > 1)
{
b = 1;
}
else if (b < 0)
{
b = 0;
}
// cout << r << "/" << g << "/ " << b << endl;
//multiply by 255 - conversion to sRGB
R[i][j] = (int)(r * 255);
G[i][j] = (int)(g * 255);
B[i][j] = (int)(b * 255);
// cout << R[i][j] << "/" << G[i][j] << "/" << B[i][j] << endl;
}
Mat oR(rows, cols, CV_8UC1);
Mat oG(rows, cols, CV_8UC1);
Mat oB(rows, cols, CV_8UC1);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++) {
oR.at<uchar>(i, j) = R[i][j];;
oG.at<uchar>(i, j) = G[i][j];;
oB.at<uchar>(i, j) = B[i][j];;
}
Mat o_planes[] = { oB, oG, oR };
Mat outImage;
merge(o_planes, 3, outImage);
namedWindow("output", CV_WINDOW_AUTOSIZE);
imshow("output", outImage);
imwrite(outName, outImage);
}
int main(int argc, char** argv) {
if (argc != 7) {
cerr << argv[0] << ": "
<< "got " << argc - 1
<< " arguments. Expecting six: w1 h1 w2 h2 ImageIn ImageOut."
<< endl;
cerr << "Example: proj1b 0.2 0.1 0.8 0.5 fruits.jpg out.bmp" << endl;
return(-1);
}
double w1 = atof(argv[1]);
double h1 = atof(argv[2]);
double w2 = atof(argv[3]);
double h2 = atof(argv[4]);
char *inputName = argv[5];
char *outputName = argv[6];
if (w1<0 || h1<0 || w2 <= w1 || h2 <= h1 || w2>1 || h2>1) {
cerr << " arguments must satisfy 0 <= w1 < w2 <= 1"
<< " , 0 <= h1 < h2 <= 1" << endl;
return(-1);
}
Mat inputImage = imread(inputName, CV_LOAD_IMAGE_UNCHANGED);
if (inputImage.empty()) {
cout << "Could not open or find the image " << inputName << endl;
return(-1);
}
string windowInput("input: ");
windowInput += inputName;
namedWindow(windowInput, CV_WINDOW_AUTOSIZE);
imshow(windowInput, inputImage);
if (inputImage.type() != CV_8UC3) {
cout << inputName << " is not a standard color image " << endl;
return(-1);
}
int rows = inputImage.rows;
int cols = inputImage.cols;
int W1 = (int)(w1*(cols - 1));
int H1 = (int)(h1*(rows - 1));
int W2 = (int)(w2*(cols - 1));
int H2 = (int)(h2*(rows - 1));
runOnWindow(W1, H1, W2, H2, inputImage, outputName);
waitKey(0); // Wait for a keystroke
return(0);
}