-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw5.cpp
285 lines (275 loc) · 9.38 KB
/
hw5.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
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
// author: Alex Hung
#include <opencv/cv.hpp>
#include <iostream>
#include <exception>
#include <cmath>
#include <vector>
#include <algorithm>
#include <random>
#include <chrono>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_spline.h>
using namespace cv;
using namespace std;
void gsl_1d_interpolation(const Mat& src, Mat& tgt) {
tgt = src.clone();
double* xarry = new double[src.cols];
double* yarry = new double[src.cols];
// green
for (int r = 0; r < src.rows; r++) {
int idx = 0;
for (int c = r % 2; c < src.cols; c += 2) {
xarry[idx] = c;
yarry[idx] = src.at<Vec3b>(r, c)[1];
idx++;
}
gsl_spline* interp = gsl_spline_alloc(gsl_interp_cspline, idx);
gsl_interp_accel* acc = gsl_interp_accel_alloc();
gsl_spline_init(interp, xarry, yarry, idx);
for (int c = (r % 2) + 1; c < src.cols; c += 2) {
if (c >= interp->interp->xmin && c <= interp->interp->xmax)
tgt.at<Vec3b>(r, c)[1] = static_cast<unsigned char>(
min(255.0, max(0.0, gsl_spline_eval(interp, c, acc) / 2.0)));
}
gsl_spline_free(interp);
gsl_interp_accel_free(acc);
}
for (int c = 0; c < src.cols; c++) {
int idx = 0;
for (int r = c % 2; r < src.rows; r += 2) {
xarry[idx] = r;
yarry[idx] = src.at<Vec3b>(r, c)[1];
idx++;
}
gsl_spline* interp = gsl_spline_alloc(gsl_interp_cspline, idx);
gsl_interp_accel* acc = gsl_interp_accel_alloc();
gsl_spline_init(interp, xarry, yarry, idx);
for (int r = (c % 2) + 1; r < src.rows; r += 2) {
if (r >= interp->interp->xmin && r <= interp->interp->xmax)
tgt.at<Vec3b>(r, c)[1] += static_cast<unsigned char>(
min(255.0, max(0.0, gsl_spline_eval(interp, r, acc) / 2.0)));
}
gsl_spline_free(interp);
gsl_interp_accel_free(acc);
}
// blue
for (int r = 0; r < src.rows; r += 2) {
int idx = 0;
for (int c = 1; c < src.cols; c += 2) {
xarry[idx] = c;
yarry[idx] = src.at<Vec3b>(r, c)[0];
idx++;
}
gsl_spline* interp = gsl_spline_alloc(gsl_interp_cspline, idx);
gsl_interp_accel* acc = gsl_interp_accel_alloc();
gsl_spline_init(interp, xarry, yarry, idx);
for (int c = 0; c < src.cols; c += 2) {
if (c >= interp->interp->xmin && c <= interp->interp->xmax)
tgt.at<Vec3b>(r, c)[0] = static_cast<unsigned char>(
min(255.0, max(0.0, gsl_spline_eval(interp, c, acc))));
}
gsl_spline_free(interp);
gsl_interp_accel_free(acc);
}
for (int c = 0; c < src.cols; c++) {
int idx = 0;
for (int r = 0; r < src.rows; r += 2) {
xarry[idx] = r;
yarry[idx] = tgt.at<Vec3b>(r, c)[0];
idx++;
}
gsl_spline* interp = gsl_spline_alloc(gsl_interp_cspline, idx);
gsl_interp_accel* acc = gsl_interp_accel_alloc();
gsl_spline_init(interp, xarry, yarry, idx);
for (int r = 1; r < src.rows; r += 2) {
if (r >= interp->interp->xmin && r <= interp->interp->xmax)
tgt.at<Vec3b>(r, c)[0] = static_cast<unsigned char>(
min(255.0, max(0.0, gsl_spline_eval(interp, r, acc))));
}
gsl_spline_free(interp);
gsl_interp_accel_free(acc);
}
// red
for (int r = 1; r < src.rows; r += 2) {
int idx = 0;
for (int c = 0; c < src.cols; c += 2) {
xarry[idx] = c;
yarry[idx] = src.at<Vec3b>(r, c)[2];
idx++;
}
gsl_spline* interp = gsl_spline_alloc(gsl_interp_cspline, idx);
gsl_interp_accel* acc = gsl_interp_accel_alloc();
gsl_spline_init(interp, xarry, yarry, idx);
for (int c = 1; c < src.cols; c += 2) {
if (c >= interp->interp->xmin && c <= interp->interp->xmax)
tgt.at<Vec3b>(r, c)[2] = static_cast<unsigned char>(
min(255.0, max(0.0, gsl_spline_eval(interp, c, acc))));
}
gsl_spline_free(interp);
gsl_interp_accel_free(acc);
}
for (int c = 0; c < src.cols; c++) {
int idx = 0;
for (int r = 1; r < src.rows; r += 2) {
xarry[idx] = r;
yarry[idx] = tgt.at<Vec3b>(r, c)[2];
idx++;
}
gsl_spline* interp = gsl_spline_alloc(gsl_interp_cspline, idx);
gsl_interp_accel* acc = gsl_interp_accel_alloc();
gsl_spline_init(interp, xarry, yarry, idx);
for (int r = 0; r < src.rows; r += 2) {
if (r >= interp->interp->xmin && r <= interp->interp->xmax)
tgt.at<Vec3b>(r, c)[2] = static_cast<unsigned char>(
min(255.0, max(0.0, gsl_spline_eval(interp, r, acc))));
}
gsl_spline_free(interp);
gsl_interp_accel_free(acc);
}
delete[] xarry;
delete[] yarry;
}
inline unsigned char fourPointsAvg(const Mat& src, int dim, Point2i a,
Point2i b, Point2i c, Point2i d) {
Rect rect(Point(), src.size());
double sum = 0;
int count = 0;
if (rect.contains(a)) {
sum += src.at<Vec3b>(a)[dim];
count++;
}
if (rect.contains(b)) {
sum += src.at<Vec3b>(b)[dim];
count++;
}
if (rect.contains(c)) {
sum += src.at<Vec3b>(c)[dim];
count++;
}
if (rect.contains(d)) {
sum += src.at<Vec3b>(d)[dim];
count++;
}
return static_cast<unsigned char>(sum / count);
}
inline unsigned char twoPointsAvg(const Mat& src, int dim, Point2i a,
Point2i b) {
Rect rect(Point(), src.size());
double sum = 0;
int count = 0;
if (rect.contains(a)) {
sum += src.at<Vec3b>(a)[dim];
count++;
}
if (rect.contains(b)) {
sum += src.at<Vec3b>(b)[dim];
count++;
}
return static_cast<unsigned char>(sum / count);
}
void bilinearInterpolationFromBayerImg(const Mat& src, Mat& tgt) {
tgt = src.clone();
for (int r = 1; r < tgt.rows - 1; r++) {
for (int c = 1; c < tgt.cols - 1; c++) {
/* fill Green */
if ((r + c) % 2 == 1) {
tgt.at<Vec3b>(r, c)[1] =
fourPointsAvg(tgt, 1, Point(c, r + 1), Point(c, r - 1),
Point(c + 1, r), Point(c - 1, r));
}
/*fill Blue*/
if (r % 2 == 0 && c % 2 == 0) {
tgt.at<Vec3b>(r, c)[0] =
twoPointsAvg(tgt, 0, Point(c + 1, r), Point(c - 1, r));
} else if (r % 2 == 1 && c % 2 == 1) {
tgt.at<Vec3b>(r, c)[0] =
twoPointsAvg(tgt, 0, Point(c, r + 1), Point(c, r - 1));
} else if (r % 2 == 1 && c % 2 == 0) {
tgt.at<Vec3b>(r, c)[0] =
fourPointsAvg(tgt, 0, Point(c + 1, r + 1), Point(c - 1, r + 1),
Point(c + 1, r - 1), Point(c - 1, r - 1));
}
/*fill red*/
if (r % 2 == 0 && c % 2 == 0) {
tgt.at<Vec3b>(r, c)[2] =
twoPointsAvg(tgt, 2, Point(c, r + 1), Point(c, r - 1));
} else if (r % 2 == 1 && c % 2 == 1) {
tgt.at<Vec3b>(r, c)[2] =
twoPointsAvg(tgt, 2, Point(c + 1, r), Point(c - 1, r));
} else if (r % 2 == 0 && c % 2 == 1) {
tgt.at<Vec3b>(r, c)[2] =
fourPointsAvg(tgt, 2, Point(c + 1, r + 1), Point(c - 1, r + 1),
Point(c + 1, r - 1), Point(c - 1, r - 1));
}
}
}
}
void getBayerImg(const Mat& src, Mat& tgt) {
/* src is BGR format */
tgt = src.clone();
/* remove colors */
for (int r = 0; r < tgt.rows; r++) {
for (int c = 0; c < tgt.cols; c++) {
if ((r + c) % 2 == 0) {
/*keep green*/
tgt.at<Vec3b>(r, c)[0] = tgt.at<Vec3b>(r, c)[2] = 0;
} else if (r % 2 == 0) {
/*keep blue*/
tgt.at<Vec3b>(r, c)[1] = tgt.at<Vec3b>(r, c)[2] = 0;
} else {
/*keep red*/
tgt.at<Vec3b>(r, c)[0] = tgt.at<Vec3b>(r, c)[1] = 0;
}
}
}
}
int main(int argc, char* argv[]) {
if (argc < 2) {
cerr << "usage: ./hw5 img.tiff\n"
"eg: ./hw5\n";
exit(1);
}
try {
Mat img = imread(argv[1], CV_LOAD_IMAGE_COLOR);
Mat bayer_img;
getBayerImg(img, bayer_img);
Mat linear_img;
bilinearInterpolationFromBayerImg(bayer_img, linear_img);
Mat diff_img = max(linear_img, img) - min(linear_img, img);
Mat spline_img;
gsl_1d_interpolation(bayer_img, spline_img);
Mat diff_img2 = max(spline_img, img) - min(spline_img, img);
cout << (int)spline_img.at<Vec3b>(3, 2)[0] << endl
<< (int)img.at<Vec3b>(3, 2)[0] << endl
<< (int)bayer_img.at<Vec3b>(3, 2)[0] << endl;
#ifdef SHOW_IMG_WINDOW
namedWindow("Color image",
CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
imshow("Color image", img);
moveWindow("Color image", 0, 0);
namedWindow("Bayer image",
CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
imshow("Bayer image", bayer_img);
moveWindow("Bayer image", img.rows + 20, 0);
namedWindow("Linear interpolation",
CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
imshow("Linear interpolation", linear_img);
moveWindow("Linear interpolation", 0, img.cols + 20);
namedWindow("difference",
CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
imshow("difference", diff_img);
moveWindow("difference", img.rows + 20, img.cols + 20);
namedWindow("Spline interpolation",
CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
imshow("Spline interpolation", spline_img);
moveWindow("Spline interpolation", 0, (img.cols + 20) * 2);
namedWindow("difference(Spline)",
CV_WINDOW_AUTOSIZE | CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
imshow("difference(Spline)", diff_img2);
moveWindow("difference(Spline)", img.rows + 20, (img.cols + 20) * 2);
#endif
waitKey(0);
} catch (exception& e) {
cerr << "exception caught: " << e.what() << '\n';
}
}