-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathimage.cpp
More file actions
523 lines (482 loc) · 14.1 KB
/
image.cpp
File metadata and controls
523 lines (482 loc) · 14.1 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//========================================================================
// Image
//========================================================================
// @brief: loading image data
#include "image.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image_read.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
int windows = 0;
float colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };
// return 8*128*image
// load labels 8(different size), 32~126 (different type)
// store information&value of labels: w,h,c,*data
image **load_alphabet()
{
const int nsize = LABEL_SIZE;
image **alphabets = (image **)calloc(nsize, sizeof(image *));
//
for (int j = 0; j < nsize; j++)
{
alphabets[j] = (image *)calloc(LABEL_TYPE, sizeof(image));
for (int i = 32; i < LABEL_TYPE - 1; i++)
{
char buffer[256];
sprintf(buffer, "data/labels/%d_%d.png", i, j);
// buffer: filename of labels
alphabets[j][i] = load_image_color(buffer, 0, 0);
}
}
return alphabets;
}
// pass value?
image load_image_color(char *filename, int w, int h)
{
return load_image(filename, w, h, 3);
}
// load image top function
image load_image(char *filename, int w, int h, int c) //003
{
// load image value BUG
image out = load_image_stb(filename, c);
//printf("out.h: %d; out.w: %d; out.c: %d;\n",out.h,out.w,out.c);
// when resize???
if((h && w) && (h != out.h || w != out.w))
{
image resized = resize_image(out, w, h);
free_image(out);
out = resized;
}
return out;
}
// return im.data: w(width); h(height); z(depth,channel)
image load_image_stb(char *filename, int channels) // filename, 3
{
int w, h, c;
// standard image load function
// stbi_load output: z(depth, channel); w(width); h(height)
//printf("filename: %s; channels: %d;\n",filename,channels);
unsigned char *data = stbi_load(filename, &w, &h, &c, channels);
//printf("out.h: %d; out.w: %d; out.c: %d;\n",h,w,c);
if(channels)
{
c = channels;
}
// make new image
image im = make_image(w, h, c);
for (int k = 0; k < c; k++)
{
for (int j = 0; j < h; j++)
{
for(int i = 0; i < w; i++)
{
int index_dst = i + w*j + w*h*k;
int index_src = k + c*i + c*w*j;
im.data[index_dst] = (float)data[index_src]/255.0;
}
}
}
free(data);
return im;
}
// make_image top function
image make_image(int w, int h, int c)
{
image out = make_empty_image(w, h, c);
out.data = (float *)calloc(h*w*c, sizeof(float));
return out;
}
// make empty image (data pointer: 0)
image make_empty_image(int w, int h, int c)
{
image out;
out.data = 0;
out.h = h;
out.w = w;
out.c = c;
return out;
}
// resize the given image (w*h)
image resize_image(image im, int w, int h)
{
image resized = make_image(w, h, im.c);
image part = make_image(w, im.h, im.c);
float w_scale = (float)(im.w - 1)/(w - 1);
float h_scale = (float)(im.h - 1)/(h - 1);
// stage 1: resize image within given width (column)
for (int k = 0; k < im.c; k++)
{
for (int r = 0; r < im.h; r++) // row
{
for(int c = 0; c < w; c++) // column
{
float val = 0;
// last column || only one column
if (c == w-1 || im.w == 1)
{ // simply fetch the original final column
val = get_pixel(im, im.w - 1, r, k);
}
else
{
float sx = c*w_scale;
int ix = (int)sx;
float dx = sx - ix;
// weighted sum for other columns
val = (1 - dx) * get_pixel(im, ix, r, k) + dx * get_pixel(im, ix+1, r, k);
}
// store val into image part
set_pixel(part, c, r, k, val);
}
}
}
// stage 2: resize image within given height (row)
for (int k = 0; k < im.c; k++)
{
for(int r = 0; r < h; r++)
{
float sy = r*h_scale;
int iy = (int)sy;
float dy = sy - iy;
//
for (int c = 0; c < w; c++)
{
float val = (1 - dy) * get_pixel(part, c, iy, k);
// store val into image resized
set_pixel(resized, c, r, k, val);
}
// the last row || only one row
if (r == h-1 || im.h == 1)
{
continue;
}
//
for (int c = 0; c < w; c++)
{
float val = dy * get_pixel(part, c, iy+1, k);
add_pixel(resized, c, r, k, val);
}
}
}
free_image(part);
return resized;
}
// pick up pixel in m.data: x - width, y - height, c - channel
float get_pixel(image m, int x, int y, int c)
{
// x < m.w && y < m.h && c < m.c == 0: assert
assert(x < m.w && y < m.h && c < m.c);
return m.data[c*m.h*m.w + y*m.w + x];
}
// fetch extra pixels
float get_pixel_extend(image m, int x, int y, int c)
{
if(x < 0)
{
x = 0;
}
if(x >= m.w)
{
x = m.w-1;
}
if(y < 0)
{
y = 0;
}
if(y >= m.h)
{
y = m.h-1;
}
if(c < 0 || c >= m.c)
{
return 0;
}
return get_pixel(m, x, y, c);
}
// check the validity of data && store data into image
void set_pixel(image m, int x, int y, int c, float val)
{
if (x < 0 || y < 0 || c < 0 || x >= m.w || y >= m.h || c >= m.c)
{
return;
}
// x < m.w && y < m.h && c < m.c == 0: assert
assert(x < m.w && y < m.h && c < m.c);
m.data[c*m.h*m.w + y*m.w + x] = val;
}
// add value to pixels
void add_pixel(image m, int x, int y, int c, float val)
{
// x < m.w && y < m.h && c < m.c == 0: assert
assert(x < m.w && y < m.h && c < m.c);
m.data[c*m.h*m.w + y*m.w + x] += val;
}
// draw detecting results
void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
{
//printf("probs[176][2]:%.12f; probs[176][7]:%.12f;\n",probs[176][2],probs[176][7]);
for (int i = 0; i < num; i++)
{
//printf("ch0;\n");
int class_max = max_index(probs[i], classes); // max_index???
float prob = probs[i][class_max];
/*
if(probs[i][class_max] != 0)
{
printf("i:%d; class_max:%d; prob:%.12f; \n",i,class_max,prob);
}
*/
if (prob > thresh)
{
//printf("ch1;\n");
int width = im.h * 0.012;
/* ??????????????????????????????????????
if(0)
{
width = pow(prob, 1.0/2.0)*10 + 1;
alphabet = 0;
}
*/
printf("%s: %.0f%%\n", names[class_max], prob*100);
int offset = class_max * 123457 % classes;
float red = get_color(2, offset, classes);
float green = get_color(1, offset, classes);
float blue = get_color(0, offset, classes);
float rgb[3];
//
rgb[0] = red;
rgb[1] = green;
rgb[2] = blue;
box b = boxes[i];
//
int left = (b.x-b.w/2.0)*im.w;
int right = (b.x+b.w/2.0)*im.w;
int top = (b.y-b.h/2.0)*im.h;
int bot = (b.y+b.h/2.0)*im.h;
//
if(left < 0) left = 0;
if(right > im.w-1) right = im.w-1;
if(top < 0) top = 0;
if(bot > im.h-1) bot = im.h-1;
//printf("ch2;\n");
//
draw_box_width(im, left, top, right, bot, width, red, green, blue);
//printf("ch3;\n");
if(alphabet)
{
image label = get_label(alphabet, names[class_max], (im.h*0.03)/10);
draw_label(im, top + width, left, label, rgb);
}
}
//printf("ch4;\n");
}
}
// get label
image get_label(image **characters, char *string, int size)
{
if (size > 7)
{
size = 7;
}
image label = make_empty_image(0, 0, 0);
//
while(*string)
{
image l = characters[size][(int)*string];
image n = tile_images(label, l, -size - 1 + (size+1)/2);
free_image(label);
label = n;
string++;
}
image b = border_image(label, label.h*.25);
free_image(label);
return b;
}
// splite image
image tile_images(image a, image b, int dx)
{
if(a.w == 0)
{
return copy_image(b);
}
image c = make_image(a.w + b.w + dx, (a.h > b.h) ? a.h : b.h, (a.c > b.c) ? a.c : b.c);
fill_cpu(c.w*c.h*c.c, 1, c.data, 1);
embed_image(a, c, 0, 0);
composite_image(b, c, a.w + dx, 0);
return c;
}
// border/wrap up image
image border_image(image a, int border)
{
image b = make_image(a.w + 2*border, a.h + 2*border, a.c);
//
for(int k = 0; k < b.c; ++k)
{
for(int y = 0; y < b.h; ++y)
{
for(int x = 0; x < b.w; ++x)
{
float val = get_pixel_extend(a, x - border, y - border, k);
if(x - border < 0 || x - border >= a.w || y - border < 0 || y - border >= a.h)
{
val = 1;
}
set_pixel(b, x, y, k, val);
}
}
}
return b;
}
// copy image
image copy_image(image p)
{
image copy = p;
copy.data = (float *)calloc(p.h*p.w*p.c, sizeof(float));
memcpy(copy.data, p.data, p.h*p.w*p.c*sizeof(float));
return copy;
}
// embed image (image data transmission)
void embed_image(image source, image dest, int dx, int dy)
{
for(int k = 0; k < source.c; k++)
{
for(int y = 0; y < source.h; y++)
{
for(int x = 0; x < source.w; x++)
{
float val = get_pixel(source, x,y,k);
set_pixel(dest, dx+x, dy+y, k, val);
}
}
}
}
// merge images
void composite_image(image source, image dest, int dx, int dy)
{
for (int k = 0; k < source.c; k++)
{
for (int y = 0; y < source.h; y++)
{
for (int x = 0; x < source.w; x++)
{
float val = get_pixel(source, x, y, k);
float val2 = get_pixel_extend(dest, dx+x, dy+y, k);
set_pixel(dest, dx+x, dy+y, k, val * val2);
}
}
}
}
// get width of boxes
void draw_box_width(image a, int x1, int y1, int x2, int y2,int w, float r, float g, float b)
{
for (int i = 0; i < w; i++)
{
draw_box(a, x1+i, y1+i, x2-i, y2-i, r, g, b);
}
}
// draw one box
void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b)
{
// normalize_image(a)
// ensure the boxed in the picture
if(x1 < 0) x1 = 0;
if(x1 >= a.w) x1 = a.w-1;
if(x2 < 0) x2 = 0;
if(x2 >= a.w) x2 = a.w-1;
if(y1 < 0) y1 = 0;
if(y1 >= a.h) y1 = a.h-1;
if(y2 < 0) y2 = 0;
if(y2 >= a.h) y2 = a.h-1;
// draw boxes: rgb
for (int i = x1; i <= x2; i++)
{ // two horizontal lines
a.data[i + y1*a.w + 0*a.w*a.h] = r;
a.data[i + y2*a.w + 0*a.w*a.h] = r;
a.data[i + y1*a.w + 1*a.w*a.h] = g;
a.data[i + y2*a.w + 1*a.w*a.h] = g;
a.data[i + y1*a.w + 2*a.w*a.h] = b;
a.data[i + y2*a.w + 2*a.w*a.h] = b;
}
for (int i = y1; i <= y2; i++)
{ // two vertical lines
a.data[x1 + i*a.w + 0*a.w*a.h] = r;
a.data[x2 + i*a.w + 0*a.w*a.h] = r;
a.data[x1 + i*a.w + 1*a.w*a.h] = g;
a.data[x2 + i*a.w + 1*a.w*a.h] = g;
a.data[x1 + i*a.w + 2*a.w*a.h] = b;
a.data[x2 + i*a.w + 2*a.w*a.h] = b;
}
}
// draw labels
void draw_label(image a, int r, int c, image label, const float *rgb)
{
int w = label.w;
int h = label.h;
if(r - h >= 0)
{
r = r - h;
}
// replace corresponding pixels for labels
for (int j = 0; j < h && j + r < a.h; j++)
{
for (int i = 0; i < w && i + c < a.w; i++)
{
for (int k = 0; k < label.c; k++)
{
float val = get_pixel(label, i, j, k);
set_pixel(a, i+c, j+r, k, rgb[k] * val);
}
}
}
}
// get image color
float get_color(int c, int x, int max)
{
float ratio = ((float)x/max)*5;
int i = floor(ratio);
int j = ceil(ratio);
ratio -= i;
float r = (1-ratio) * colors[i][c] + ratio*colors[j][c];
return r;
}
// display image
void show_image(image p, const char *name)
{
fprintf(stderr,"Not compiled with OpenCV, saving to %s.png instead.\n", name);
save_image(p, name);
}
// save image top function
void save_image(image im, const char *name)
{
save_image_png(im, name);
}
// data: one pixel(three channels); im.data: all pixels for one channel, next channel, etc.
void save_image_png(image im, const char *name)
{
char buffer[256];
// save picture name into buffer
sprintf(buffer, "%s.png", name);
unsigned char *data = (unsigned char *)calloc(im.w*im.h*im.c, sizeof(char));
//
//printf("ch0;\n");
for(int k = 0; k < im.c; k++)
{
for(int i = 0; i < im.w*im.h; i++)
{
data[i*im.c + k] = (unsigned char) (255 * im.data[i + k*im.w*im.h]);
}
}
//printf("ch1;\n");
int success = stbi_write_png(buffer, im.w, im.h, im.c, data, im.w*im.c);
//printf("ch2;\n");
free(data);
if(!success) fprintf(stderr, "Failed to write image %s\n", buffer);
}
// free allocated memory
void free_image(image m)
{
if(m.data)
{
free(m.data);
}
}