-
Notifications
You must be signed in to change notification settings - Fork 0
/
Convolution.cpp
352 lines (312 loc) · 12.6 KB
/
Convolution.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
351
352
#include "convolution.h"
int getSize(int height, int width, int channels) {
return height * width * channels;
}
unsigned char* addImages(unsigned char* a, int aRows, int aCols, int aChannels, unsigned char* b, int bRows, int bCols, int bChannels) {
if (aCols != bCols || aRows != bRows || aChannels != bChannels) {
printf("IMAGES NOT ADDABLE");
return a;
}
int size = getSize(aRows, aCols, aChannels);
unsigned char* newData = (unsigned char*)malloc(sizeof(unsigned char) * size);
for (int i = 0;i < size;i++) {
int pixel = a[i] + b[i];
newData[i] = (pixel > 255) ? 255 : pixel;
}
return newData;
}
void brightnessUp(unsigned char** data, int height, int width, int channels, int brightness){
//This uses truncation to ensure it doesn't go above 255
int size = getSize(height, width, channels);
for (int i = 0;i < size;i++) {
int temp = (*data)[i] + brightness;
(*data)[i] = (temp > 255) ? 255 : temp;
}
}
void brightnessDown(unsigned char** data, int height, int width, int channels, int brightness) {
//This uses truncation to ensure it doesn't go below 0
int size = getSize(height, width, channels);
for (int i = 0;i < size;i++) {
int temp = (*data)[i] - brightness;
(*data)[i] = (temp < 0) ? 0 : temp;
}
}
void saltAndPepper(unsigned char** data, int height, int width, int channels, float prob) {
int size = getSize(height, width, channels);
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++) (*data)[r * width * channels + c * channels + chan] = 0;
if (p >= p2 && p < 16384)
for (int chan = 0;chan < channels;chan++) (*data)[r * width * channels + c * channels + chan] = 255;
}
}
void addPadding(unsigned char** data, int height, int width, int channels, int pad) {
int size = getSize(height, width, channels);
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] = (*data)[pixel];
}
}
}
size = tempHeight * tempWidth;
(*data) = newData;
}
int getValue(unsigned char* data, int height, int width, int channels, int row, int col, int channel) {
if (row < height && row >= 0 && col < width && col >= 0) {
return data[(row * width * channels) + (col * channels) + channel];
}
return 0;
}
int convolute(unsigned char* data, int height, int width, int channels, 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(data, height, width, channels, 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(unsigned char* data, int height, int width, int channels, 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(data, height, width, channels, row + i, col + j, channel);
if (pixel > max) max = pixel;
}
}
return max;
}
int poolAvg(unsigned char* data, int height, int width, int channels, int row, int col, int channel, int poolSize) {
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(data, height, width, channels, row + i, col + j, channel);
}
}
return (int)(avg / (poolSize * poolSize));
}
void applyKernel(unsigned char** data, int height, int width, int channels, const double mask[][3]) {
int size = getSize(height, width, channels);
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];
}
}
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((*data), height, width, channels, r, c, chan, kernel, 3);
count++;
}
}
}
(*data) = newData;
}
void Blur(unsigned char** data, int height, int width, int channels, float strength) {
int size = getSize(height, width, channels);
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((*data), height, width, channels, r, c, chan, kernel, strength);
}
}
}
(*data) = newData;
}
void applySepia(unsigned char** data, int height, int width, int channels) {
int size = getSize(height, width, channels);
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 = (*data)[i] * sepia[0][0] + (*data)[i + 1] * sepia[0][1] + (*data)[i + 2] * sepia[0][2];
g = (*data)[i] * sepia[1][0] + (*data)[i + 1] * sepia[1][1] + (*data)[i + 2] * sepia[1][2];
b = (*data)[i] * sepia[2][0] + (*data)[i + 1] * sepia[2][1] + (*data)[i + 2] * sepia[2][2];
(*data)[i] = (b < 255) ? b : 255;
(*data)[i + 1] = (g < 255) ? g : 255;
(*data)[i + 2] = (r < 255) ? r : 255;
}
}
void downSize(unsigned char** data, int height, int width, int channels, int skipRate) {
int size = getSize(height, width, channels);
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] = (*data)[r * width * channels + c * channels + chan];
}
newC++;
}
}
newR++;
}
if (newC > lastC) lastC = newC;
newC = 0;
}
height = newR;
width = lastC;
size = height * width * channels;
(*data) = newData;
}
void sobelEdges(unsigned char** data, int height, int width, int channels) {
int size = getSize(height, width, channels);
unsigned char* horiz = *data;
applyKernel(&horiz, height, width, channels, SOBEL_HORIZ);
unsigned char* vert = *data;
applyKernel(&vert, height, width, channels, SOBEL_VERT);
(*data) = addImages(horiz, height, width, channels, vert, height, width, channels);
}
void prewittEdges(unsigned char** data, int height, int width, int channels) {
int size = getSize(height, width, channels);
unsigned char* horiz = *data;
applyKernel(&horiz, height, width, channels, PREWITT_HORIZ);
unsigned char* vert = *data;
applyKernel(&vert, height, width, channels, PREWITT_VERT);
(*data) = addImages(horiz, height, width, channels, vert, height, width, channels);
}
unsigned char* maxPooling(unsigned char** data, int height, int width, int channels, int poolSize) {
int size = getSize(height, width, channels);
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] = poolMax((*data), height, width, channels, r, c, chan, poolSize);
}
newC++;
}
newR++;
newC = 0;
}
return newData;
}
unsigned char* avgPooling(unsigned char** data, int height, int width, int channels, int poolSize) {
int size = getSize(height, width, channels);
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((*data), height, width, channels, r, c, chan, poolSize);
}
newC++;
}
newR++;
newC = 0;
}
return newData;
}
void scalePoint(unsigned char pixel, unsigned char** newData, int height, int width, int channels, int row, int col, int channel, int scale) {
int end = (int)((scale / 2.0) + .5);
int start = end - scale;
double avg = 0;
for (int i = start;i < end;i++) {
for (int j = start;j < end;j++) {
(*newData)[(row + i) * width * channels + (col + j) * channels + channel] = pixel;
}
}
}
unsigned char* upscale(unsigned char** data, int height, int width, int channels, int scale) {
int size = getSize(height, width, channels);
int newWidth = width * scale;
int newHeight = height * scale;
int newSize = getSize(newHeight, newWidth, channels);
unsigned char* newData = (unsigned char*)malloc(sizeof(unsigned char) * newSize);
int rStart = scale / 2;
int cStart = scale / 2;
printf("SCALING %dx%d IMAGE TO %dx%d IMAGE\n", height, width, newHeight, newWidth);
int oldC, oldR;
oldC = oldR = 0;
for (int r = rStart;r < newHeight;r += scale) {
for (int c = cStart;c < newWidth;c += scale) {
for (int chan = 0;chan < channels;chan++) {
scalePoint((*data)[oldR * width * channels + oldC * channels + chan], &newData, newHeight, newWidth, channels, r, c, chan, scale);
}
oldC++;
}
oldR++;
oldC = 0;
}
return newData;
}