-
Notifications
You must be signed in to change notification settings - Fork 0
/
CVImage.cpp
350 lines (312 loc) · 11.4 KB
/
CVImage.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
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
#include "CVImage.h"
int getSize(Mat image) {
return image.rows * image.cols * image.channels();
}
void get(Mat Image, int* width, int* height, int* channels, int* size) {
*width = Image.cols;
*height = Image.rows;
*channels = Image.channels();
*size = (*width) * (*height) * (*channels);
}
Mat addImages(Mat a, Mat b) {
if (a.cols != b.cols || a.rows != b.rows || a.channels() != b.channels()) {
printf("IMAGES NOT ADDABLE");
return a;
}
int width, height, channels, size;
get(a, &width, &height, &channels, &size);
unsigned char* newData = (unsigned char*)malloc(sizeof(unsigned char) * size);
for (int i = 0;i < size;i++) {
int pixel = a.data[i] + b.data[i];
newData[i] = (pixel > 255) ? 255 : pixel;
}
a.data = newData;
return a;
}
void brightnessUp(Mat image, int brightness) {
//This uses truncation to ensure it doesn't go above 255
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
for (int i = 0;i < size;i++) {
int temp = image.data[i] + brightness;
image.data[i] = (temp > 255) ? 255 : temp;
}
}
void brightnessDown(Mat image, int brightness) {
//This uses truncation to ensure it doesn't go below 0
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
for (int i = 0;i < size;i++) {
int temp = image.data[i] - brightness;
image.data[i] = (temp < 0) ? 0 : temp;
}
}
void saltAndPepper(Mat image, float prob) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
int x, y, p1, p2, p;
p = (int)(prob * 32768 / 2);
p1 = p + 16384;
p2 = 16384 - p;
for (int r = 0;r < height;r++)
for (int c = 0;c < width;c++)
{
p = rand();
if (p >= 16384 && p < p1)
for (int chan = 0;chan < channels;chan++) image.data[r * width * channels + c * channels + chan] = 0;
if (p >= p2 && p < 16384)
for (int chan = 0;chan < channels;chan++) image.data[r * width * channels + c * channels + chan] = 255;
}
}
void addPadding(Mat image, int pad) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
int tempWidth = width + pad * 2;
int tempHeight = height + pad * 2;
unsigned char* newData = (unsigned char*)malloc(sizeof(unsigned char) * (tempWidth * tempHeight) * (channels));
for (int r = 0;r < tempHeight;r++) {
for (int c = 0;c < tempWidth;c++) {
for (int chan = 0;chan < channels;chan++) {
int newPixel = r * tempWidth * channels + c * channels + chan;
int pixel = (r - 1) * width * channels + (c - 1) * channels + chan;
if (r == 0 || c == 0 || r == tempHeight - 1 || c == tempWidth - 1) newData[newPixel] = 0;
else newData[newPixel] = image.data[pixel];
}
}
}
size = tempHeight * tempWidth;
image.data = newData;
}
int getValue(Mat image, int row, int col, int channel) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
if (row < height && row >= 0 && col < width && col >= 0) {
return image.data[(row * width * channels) + (col * channels) + channel];
}
return 0;
}
int convolute(Mat image, int row, int col, int channel, float** kernel, int kernelSize) {
float sum = 0;
int end = (int)((kernelSize / 2.0) + .5);
int start = end - kernelSize;
for (int i = start;i < end;i++) {
for (int j = start;j < end;j++) {
sum += getValue(image, row + i, col + j, channel) * kernel[i + (kernelSize / 2)][j + (kernelSize / 2)];
}
}
if (sum > 255) return 255;
if (sum < 0) return 0;
return (int)sum;
}
int poolMax(Mat image, int row, int col, int channel, int poolSize) {
float sum = 0;
int end = (int)((poolSize / 2.0) + .5);
int start = end - poolSize;
int max = 0;
int pixel = 0;
for (int i = start;i < end;i++) {
for (int j = start;j < end;j++) {
pixel = getValue(image, row + i, col + j, channel);
if (pixel > max) max = pixel;
}
}
return max;
}
int poolAvg(Mat image, int row, int col, int channel, int poolSize) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
float sum = 0;
int end = (int)((poolSize / 2.0) + .5);
int start = end - poolSize;
double avg = 0;
for (int i = start;i < end;i++) {
for (int j = start;j < end;j++) {
avg += getValue(image, row + i, col + j, channel);
}
}
return (int)(avg / (poolSize*poolSize));
}
Mat applyKernel(Mat image, const double mask[][3]) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
float** kernel = new float* [3];
for (int i = 0; i < 3; i++) {
kernel[i] = new float[3];
}
for (int i = 0;i < 3;i++) {
for (int j = 0;j < 3;j++) {
kernel[i][j] = mask[i][j];
}
}
Mat newImage = image.clone();
unsigned char* newData = (unsigned char*)malloc(sizeof(unsigned char) * size);
printf("size: %d\n", sizeof(newData));
int pixel = 0;
int count = 0;
int row = 0;
int col = 0;
for (int r = 0;r < height;r++) {
for (int c = 0;c < width;c++) {
for (int chan = 0;chan < channels;chan++) {
pixel = (r * width * channels) + (c * channels) + chan;
row = r * width;
col = c;
//printf("convoluting pixel %d...\n", pixel);
//newData[pixel] = convolute(image, r, c, chan, kernel, 3);
newImage.data[pixel] = convolute(image, r, c, chan, kernel, 3);
count++;
}
}
namedWindow("Display frame", WINDOW_AUTOSIZE);
imshow("Display frame", newImage);
waitKey(1);
}
//image.data = newData;
for (int i = 0;i < size;i++) {
image.data[i] = newData[i];
}
//imshow("Display frame", image);
//waitKey(0);
return newImage;
}
void Blur(Mat image, float strength) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
float** kernel = new float* [strength];
for (int i = 0; i < strength; i++) {
kernel[i] = new float[strength];
}
for (int i = 0;i < strength;i++) {
for (int j = 0;j < strength;j++) {
kernel[i][j] = 1 / (strength * strength);
}
}
unsigned char* newData = (unsigned char*)malloc(sizeof(unsigned char) * size);
int pixel = 0;
int count = 0;
for (int r = 0;r < height;r++) {
for (int c = 0;c < width;c++) {
for (int chan = 0;chan < channels;chan++) {
pixel = (r * width * channels) + (c * channels) + chan;
//printf("convoluting pixel %d...\n", pixel);
newData[pixel] = convolute(image, r, c, chan, kernel, (int)strength);
}
}
}
image.data = newData;
}
void applySepia(Mat image) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
float sepia[3][3] = { {.393,.769,.189}, {.349,.686,.168}, {.272,.534,.131} };
int r, g, b;
for (int i = 0;i < size;i += 3) {
r = image.data[i] * sepia[0][0] + image.data[i + 1] * sepia[0][1] + image.data[i + 2] * sepia[0][2];
g = image.data[i] * sepia[1][0] + image.data[i + 1] * sepia[1][1] + image.data[i + 2] * sepia[1][2];
b = image.data[i] * sepia[2][0] + image.data[i + 1] * sepia[2][1] + image.data[i + 2] * sepia[2][2];
image.data[i] = (b < 255) ? b : 255;
image.data[i + 1] = (g < 255) ? g : 255;
image.data[i + 2] = (r < 255) ? r : 255;
}
}
void downSize(Mat image, int skipRate) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
unsigned char* newData = (unsigned char*)malloc(sizeof(unsigned char) * size);
//just for better viewing
for (int r = 0;r < height;r++) {
for (int c = 0;c < width;c++) {
for (int chan = 0;chan < channels;chan++) {
newData[(r * width * channels) + (c * channels) + chan] = 0;
}
}
}
int newR, newC;
int lastC = 0;
newR = newC = 0;
int r = 0;
int c = 0;
for (r = 0;r < height;r++) {
if (r % skipRate != 0) {
for (c = 0;c < width;c++) {
if (c % skipRate != 0) {
for (int chan = 0;chan < channels;chan++) {
newData[newR * width * channels + newC * channels + chan] = image.data[r * width * channels + c * channels + chan];
}
newC++;
}
}
newR++;
}
if (newC > lastC) lastC = newC;
newC = 0;
}
height = newR;
width = lastC;
size = height * width * channels;
image.data = newData;
}
Mat sobelEdges(Mat image) {
Mat image2 = image.clone();
image = applyKernel(image, SOBEL_HORIZ);
image2 = applyKernel(image2, SOBEL_VERT);
image = addImages(image, image2);
return image;
}
Mat pooling(Mat image, int poolSize) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
int newWidth = ceil((double)width / poolSize);
int newHeight = ceil((double)height / poolSize);
int newSize = width * newHeight * channels;
unsigned char* newData = (unsigned char*)malloc(sizeof(unsigned char) * newSize);
int rStart = poolSize / 2;
int cStart = poolSize / 2;
printf("POOLING %dx%d IMAGE TO %dx%d IMAGE\n", height, width, newHeight, newWidth);
int newC, newR;
newC = newR = 0;
for (int r = rStart;r < rStart * height;r += poolSize) {
for (int c = cStart;c < cStart * width;c += poolSize) {
for (int chan = 0;chan < channels;chan++) {
newData[(newR * width * channels) + (newC * channels) + chan] = poolMax(image, r, c, chan, poolSize);
}
newC++;
}
newR++;
newC = 0;
}
//printf("%dx%d\n", image.rows, image.cols);
image = image(Range(0, newHeight), Range(0, newWidth));
//printf("%dx%d\n", image.rows, image.cols);
image.data = newData;
return image;
}
Mat avgPooling(Mat image, int poolSize) {
int width, height, channels, size;
get(image, &width, &height, &channels, &size);
int newWidth = ceil((double)width / poolSize);
int newHeight = ceil((double)height / poolSize);
int newSize = width * newHeight * channels;
unsigned char* newData = (unsigned char*)malloc(sizeof(unsigned char) * newSize);
int rStart = poolSize / 2;
int cStart = poolSize / 2;
printf("POOLING %dx%d IMAGE TO %dx%d IMAGE\n", height, width, newHeight, newWidth);
int newC, newR;
newC = newR = 0;
for (int r = rStart;r < height;r += poolSize) {
for (int c = cStart;c < width;c += poolSize) {
for (int chan = 0;chan < channels;chan++) {
newData[(newR * width * channels) + (newC * channels) + chan] = poolAvg(image, r, c, chan, poolSize);
}
newC++;
}
newR++;
newC = 0;
//namedWindow("Display frame", WINDOW_AUTOSIZE);
//imshow("Display frame", image);
//waitKey(1);
}
image = image(Range(0, newHeight), Range(0, newWidth));
image.data = newData;
return image;
}