00001 #include "image.h"
00002 #include "utils.h"
00003 #include "blas.h"
00004 #include "cuda.h"
00005 #include <stdio.h>
00006 #include <math.h>
00007
00008 #define STB_IMAGE_IMPLEMENTATION
00009 #include "stb_image.h"
00010 #define STB_IMAGE_WRITE_IMPLEMENTATION
00011 #include "stb_image_write.h"
00012
00013 #ifdef OPENCV
00014 #include "opencv2/highgui/highgui_c.h"
00015 #include "opencv2/imgproc/imgproc_c.h"
00016 #endif
00017
00018
00019 int windows = 0;
00020
00021 float colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };
00022
00023 float get_color(int c, int x, int max)
00024 {
00025 float ratio = ((float)x/max)*5;
00026 int i = floor(ratio);
00027 int j = ceil(ratio);
00028 ratio -= i;
00029 float r = (1-ratio) * colors[i][c] + ratio*colors[j][c];
00030
00031 return r;
00032 }
00033
00034 void composite_image(image source, image dest, int dx, int dy)
00035 {
00036 int x,y,k;
00037 for(k = 0; k < source.c; ++k){
00038 for(y = 0; y < source.h; ++y){
00039 for(x = 0; x < source.w; ++x){
00040 float val = get_pixel(source, x, y, k);
00041 float val2 = get_pixel_extend(dest, dx+x, dy+y, k);
00042 set_pixel(dest, dx+x, dy+y, k, val * val2);
00043 }
00044 }
00045 }
00046 }
00047
00048 image border_image(image a, int border)
00049 {
00050 image b = make_image(a.w + 2*border, a.h + 2*border, a.c);
00051 int x,y,k;
00052 for(k = 0; k < b.c; ++k){
00053 for(y = 0; y < b.h; ++y){
00054 for(x = 0; x < b.w; ++x){
00055 float val = get_pixel_extend(a, x - border, y - border, k);
00056 if(x - border < 0 || x - border >= a.w || y - border < 0 || y - border >= a.h) val = 1;
00057 set_pixel(b, x, y, k, val);
00058 }
00059 }
00060 }
00061 return b;
00062 }
00063
00064 image tile_images(image a, image b, int dx)
00065 {
00066 if(a.w == 0) return copy_image(b);
00067 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);
00068 fill_cpu(c.w*c.h*c.c, 1, c.data, 1);
00069 embed_image(a, c, 0, 0);
00070 composite_image(b, c, a.w + dx, 0);
00071 return c;
00072 }
00073
00074 image get_label(image **characters, char *string, int size)
00075 {
00076 if(size > 7) size = 7;
00077 image label = make_empty_image(0,0,0);
00078 while(*string){
00079 image l = characters[size][(int)*string];
00080 image n = tile_images(label, l, -size - 1 + (size+1)/2);
00081 free_image(label);
00082 label = n;
00083 ++string;
00084 }
00085 image b = border_image(label, label.h*.25);
00086 free_image(label);
00087 return b;
00088 }
00089
00090 void draw_label(image a, int r, int c, image label, const float *rgb)
00091 {
00092 int w = label.w;
00093 int h = label.h;
00094 if (r - h >= 0) r = r - h;
00095
00096 int i, j, k;
00097 for(j = 0; j < h && j + r < a.h; ++j){
00098 for(i = 0; i < w && i + c < a.w; ++i){
00099 for(k = 0; k < label.c; ++k){
00100 float val = get_pixel(label, i, j, k);
00101 set_pixel(a, i+c, j+r, k, rgb[k] * val);
00102 }
00103 }
00104 }
00105 }
00106
00107 void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b)
00108 {
00109
00110 int i;
00111 if(x1 < 0) x1 = 0;
00112 if(x1 >= a.w) x1 = a.w-1;
00113 if(x2 < 0) x2 = 0;
00114 if(x2 >= a.w) x2 = a.w-1;
00115
00116 if(y1 < 0) y1 = 0;
00117 if(y1 >= a.h) y1 = a.h-1;
00118 if(y2 < 0) y2 = 0;
00119 if(y2 >= a.h) y2 = a.h-1;
00120
00121 for(i = x1; i <= x2; ++i){
00122 a.data[i + y1*a.w + 0*a.w*a.h] = r;
00123 a.data[i + y2*a.w + 0*a.w*a.h] = r;
00124
00125 a.data[i + y1*a.w + 1*a.w*a.h] = g;
00126 a.data[i + y2*a.w + 1*a.w*a.h] = g;
00127
00128 a.data[i + y1*a.w + 2*a.w*a.h] = b;
00129 a.data[i + y2*a.w + 2*a.w*a.h] = b;
00130 }
00131 for(i = y1; i <= y2; ++i){
00132 a.data[x1 + i*a.w + 0*a.w*a.h] = r;
00133 a.data[x2 + i*a.w + 0*a.w*a.h] = r;
00134
00135 a.data[x1 + i*a.w + 1*a.w*a.h] = g;
00136 a.data[x2 + i*a.w + 1*a.w*a.h] = g;
00137
00138 a.data[x1 + i*a.w + 2*a.w*a.h] = b;
00139 a.data[x2 + i*a.w + 2*a.w*a.h] = b;
00140 }
00141 }
00142
00143 void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b)
00144 {
00145 int i;
00146 for(i = 0; i < w; ++i){
00147 draw_box(a, x1+i, y1+i, x2-i, y2-i, r, g, b);
00148 }
00149 }
00150
00151 void draw_bbox(image a, box bbox, int w, float r, float g, float b)
00152 {
00153 int left = (bbox.x-bbox.w/2)*a.w;
00154 int right = (bbox.x+bbox.w/2)*a.w;
00155 int top = (bbox.y-bbox.h/2)*a.h;
00156 int bot = (bbox.y+bbox.h/2)*a.h;
00157
00158 int i;
00159 for(i = 0; i < w; ++i){
00160 draw_box(a, left+i, top+i, right-i, bot-i, r, g, b);
00161 }
00162 }
00163
00164 image **load_alphabet()
00165 {
00166 int i, j;
00167 const int nsize = 8;
00168 image **alphabets = calloc(nsize, sizeof(image));
00169 for(j = 0; j < nsize; ++j){
00170 alphabets[j] = calloc(128, sizeof(image));
00171 for(i = 32; i < 127; ++i){
00172 char buff[256];
00173 sprintf(buff, "data/labels/%d_%d.png", i, j);
00174 alphabets[j][i] = load_image_color(buff, 0, 0);
00175 }
00176 }
00177 return alphabets;
00178 }
00179
00180 void draw_detections(image im, int num, float thresh, box *boxes, float **probs, char **names, image **alphabet, int classes)
00181 {
00182 int i;
00183
00184 for(i = 0; i < num; ++i){
00185 int class = max_index(probs[i], classes);
00186 float prob = probs[i][class];
00187 if(prob > thresh){
00188
00189 int width = im.h * .012;
00190
00191 if(0){
00192 width = pow(prob, 1./2.)*10+1;
00193 alphabet = 0;
00194 }
00195
00196 printf("%s: %.0f%%\n", names[class], prob*100);
00197 int offset = class*123457 % classes;
00198 float red = get_color(2,offset,classes);
00199 float green = get_color(1,offset,classes);
00200 float blue = get_color(0,offset,classes);
00201 float rgb[3];
00202
00203
00204
00205 rgb[0] = red;
00206 rgb[1] = green;
00207 rgb[2] = blue;
00208 box b = boxes[i];
00209
00210 int left = (b.x-b.w/2.)*im.w;
00211 int right = (b.x+b.w/2.)*im.w;
00212 int top = (b.y-b.h/2.)*im.h;
00213 int bot = (b.y+b.h/2.)*im.h;
00214
00215 if(left < 0) left = 0;
00216 if(right > im.w-1) right = im.w-1;
00217 if(top < 0) top = 0;
00218 if(bot > im.h-1) bot = im.h-1;
00219
00220 draw_box_width(im, left, top, right, bot, width, red, green, blue);
00221 if (alphabet) {
00222 image label = get_label(alphabet, names[class], (im.h*.03)/10);
00223 draw_label(im, top + width, left, label, rgb);
00224 }
00225 }
00226 }
00227 }
00228
00229 void transpose_image(image im)
00230 {
00231 assert(im.w == im.h);
00232 int n, m;
00233 int c;
00234 for(c = 0; c < im.c; ++c){
00235 for(n = 0; n < im.w-1; ++n){
00236 for(m = n + 1; m < im.w; ++m){
00237 float swap = im.data[m + im.w*(n + im.h*c)];
00238 im.data[m + im.w*(n + im.h*c)] = im.data[n + im.w*(m + im.h*c)];
00239 im.data[n + im.w*(m + im.h*c)] = swap;
00240 }
00241 }
00242 }
00243 }
00244
00245 void rotate_image_cw(image im, int times)
00246 {
00247 assert(im.w == im.h);
00248 times = (times + 400) % 4;
00249 int i, x, y, c;
00250 int n = im.w;
00251 for(i = 0; i < times; ++i){
00252 for(c = 0; c < im.c; ++c){
00253 for(x = 0; x < n/2; ++x){
00254 for(y = 0; y < (n-1)/2 + 1; ++y){
00255 float temp = im.data[y + im.w*(x + im.h*c)];
00256 im.data[y + im.w*(x + im.h*c)] = im.data[n-1-x + im.w*(y + im.h*c)];
00257 im.data[n-1-x + im.w*(y + im.h*c)] = im.data[n-1-y + im.w*(n-1-x + im.h*c)];
00258 im.data[n-1-y + im.w*(n-1-x + im.h*c)] = im.data[x + im.w*(n-1-y + im.h*c)];
00259 im.data[x + im.w*(n-1-y + im.h*c)] = temp;
00260 }
00261 }
00262 }
00263 }
00264 }
00265
00266 void flip_image(image a)
00267 {
00268 int i,j,k;
00269 for(k = 0; k < a.c; ++k){
00270 for(i = 0; i < a.h; ++i){
00271 for(j = 0; j < a.w/2; ++j){
00272 int index = j + a.w*(i + a.h*(k));
00273 int flip = (a.w - j - 1) + a.w*(i + a.h*(k));
00274 float swap = a.data[flip];
00275 a.data[flip] = a.data[index];
00276 a.data[index] = swap;
00277 }
00278 }
00279 }
00280 }
00281
00282 image image_distance(image a, image b)
00283 {
00284 int i,j;
00285 image dist = make_image(a.w, a.h, 1);
00286 for(i = 0; i < a.c; ++i){
00287 for(j = 0; j < a.h*a.w; ++j){
00288 dist.data[j] += pow(a.data[i*a.h*a.w+j]-b.data[i*a.h*a.w+j],2);
00289 }
00290 }
00291 for(j = 0; j < a.h*a.w; ++j){
00292 dist.data[j] = sqrt(dist.data[j]);
00293 }
00294 return dist;
00295 }
00296
00297 void embed_image(image source, image dest, int dx, int dy)
00298 {
00299 int x,y,k;
00300 for(k = 0; k < source.c; ++k){
00301 for(y = 0; y < source.h; ++y){
00302 for(x = 0; x < source.w; ++x){
00303 float val = get_pixel(source, x,y,k);
00304 set_pixel(dest, dx+x, dy+y, k, val);
00305 }
00306 }
00307 }
00308 }
00309
00310 image collapse_image_layers(image source, int border)
00311 {
00312 int h = source.h;
00313 h = (h+border)*source.c - border;
00314 image dest = make_image(source.w, h, 1);
00315 int i;
00316 for(i = 0; i < source.c; ++i){
00317 image layer = get_image_layer(source, i);
00318 int h_offset = i*(source.h+border);
00319 embed_image(layer, dest, 0, h_offset);
00320 free_image(layer);
00321 }
00322 return dest;
00323 }
00324
00325 void constrain_image(image im)
00326 {
00327 int i;
00328 for(i = 0; i < im.w*im.h*im.c; ++i){
00329 if(im.data[i] < 0) im.data[i] = 0;
00330 if(im.data[i] > 1) im.data[i] = 1;
00331 }
00332 }
00333
00334 void normalize_image(image p)
00335 {
00336 int i;
00337 float min = 9999999;
00338 float max = -999999;
00339
00340 for(i = 0; i < p.h*p.w*p.c; ++i){
00341 float v = p.data[i];
00342 if(v < min) min = v;
00343 if(v > max) max = v;
00344 }
00345 if(max - min < .000000001){
00346 min = 0;
00347 max = 1;
00348 }
00349 for(i = 0; i < p.c*p.w*p.h; ++i){
00350 p.data[i] = (p.data[i] - min)/(max-min);
00351 }
00352 }
00353
00354 void normalize_image2(image p)
00355 {
00356 float *min = calloc(p.c, sizeof(float));
00357 float *max = calloc(p.c, sizeof(float));
00358 int i,j;
00359 for(i = 0; i < p.c; ++i) min[i] = max[i] = p.data[i*p.h*p.w];
00360
00361 for(j = 0; j < p.c; ++j){
00362 for(i = 0; i < p.h*p.w; ++i){
00363 float v = p.data[i+j*p.h*p.w];
00364 if(v < min[j]) min[j] = v;
00365 if(v > max[j]) max[j] = v;
00366 }
00367 }
00368 for(i = 0; i < p.c; ++i){
00369 if(max[i] - min[i] < .000000001){
00370 min[i] = 0;
00371 max[i] = 1;
00372 }
00373 }
00374 for(j = 0; j < p.c; ++j){
00375 for(i = 0; i < p.w*p.h; ++i){
00376 p.data[i+j*p.h*p.w] = (p.data[i+j*p.h*p.w] - min[j])/(max[j]-min[j]);
00377 }
00378 }
00379 free(min);
00380 free(max);
00381 }
00382
00383 image copy_image(image p)
00384 {
00385 image copy = p;
00386 copy.data = calloc(p.h*p.w*p.c, sizeof(float));
00387 memcpy(copy.data, p.data, p.h*p.w*p.c*sizeof(float));
00388 return copy;
00389 }
00390
00391 void rgbgr_image(image im)
00392 {
00393 int i;
00394 for(i = 0; i < im.w*im.h; ++i){
00395 float swap = im.data[i];
00396 im.data[i] = im.data[i+im.w*im.h*2];
00397 im.data[i+im.w*im.h*2] = swap;
00398 }
00399 }
00400
00401 #ifdef OPENCV
00402 void show_image_cv(image p, const char *name)
00403 {
00404 int x,y,k;
00405 image copy = copy_image(p);
00406 constrain_image(copy);
00407 if(p.c == 3) rgbgr_image(copy);
00408
00409
00410 char buff[256];
00411
00412 sprintf(buff, "%s", name);
00413
00414 IplImage *disp = cvCreateImage(cvSize(p.w,p.h), IPL_DEPTH_8U, p.c);
00415 int step = disp->widthStep;
00416 cvNamedWindow(buff, CV_WINDOW_NORMAL);
00417
00418 ++windows;
00419 for(y = 0; y < p.h; ++y){
00420 for(x = 0; x < p.w; ++x){
00421 for(k= 0; k < p.c; ++k){
00422 disp->imageData[y*step + x*p.c + k] = (unsigned char)(get_pixel(copy,x,y,k)*255);
00423 }
00424 }
00425 }
00426 free_image(copy);
00427 if(0){
00428 int w = 448;
00429 int h = w*p.h/p.w;
00430 if(h > 1000){
00431 h = 1000;
00432 w = h*p.w/p.h;
00433 }
00434 IplImage *buffer = disp;
00435 disp = cvCreateImage(cvSize(w, h), buffer->depth, buffer->nChannels);
00436 cvResize(buffer, disp, CV_INTER_LINEAR);
00437 cvReleaseImage(&buffer);
00438 }
00439 cvShowImage(buff, disp);
00440 cvReleaseImage(&disp);
00441 }
00442 #endif
00443
00444 void show_image(image p, const char *name)
00445 {
00446 #ifdef OPENCV
00447 show_image_cv(p, name);
00448 #else
00449 fprintf(stderr, "Not compiled with OpenCV, saving to %s.png instead\n", name);
00450 save_image(p, name);
00451 #endif
00452 }
00453
00454 #ifdef OPENCV
00455
00456 image ipl_to_image(IplImage* src)
00457 {
00458 unsigned char *data = (unsigned char *)src->imageData;
00459 int h = src->height;
00460 int w = src->width;
00461 int c = src->nChannels;
00462 int step = src->widthStep;
00463 image out = make_image(w, h, c);
00464 int i, j, k, count=0;;
00465
00466 for(k= 0; k < c; ++k){
00467 for(i = 0; i < h; ++i){
00468 for(j = 0; j < w; ++j){
00469 out.data[count++] = data[i*step + j*c + k]/255.;
00470 }
00471 }
00472 }
00473 return out;
00474 }
00475
00476 image load_image_cv(char *filename, int channels)
00477 {
00478 IplImage* src = 0;
00479 int flag = -1;
00480 if (channels == 0) flag = -1;
00481 else if (channels == 1) flag = 0;
00482 else if (channels == 3) flag = 1;
00483 else {
00484 fprintf(stderr, "OpenCV can't force load with %d channels\n", channels);
00485 }
00486
00487 if( (src = cvLoadImage(filename, flag)) == 0 )
00488 {
00489 fprintf(stderr, "Cannot load image \"%s\"\n", filename);
00490 char buff[256];
00491 sprintf(buff, "echo %s >> bad.list", filename);
00492 system(buff);
00493 return make_image(10,10,3);
00494
00495 }
00496 image out = ipl_to_image(src);
00497 cvReleaseImage(&src);
00498 rgbgr_image(out);
00499 return out;
00500 }
00501
00502 image get_image_from_stream(CvCapture *cap)
00503 {
00504 IplImage* src = cvQueryFrame(cap);
00505 if (!src) return make_empty_image(0,0,0);
00506 image im = ipl_to_image(src);
00507 rgbgr_image(im);
00508 return im;
00509 }
00510
00511 void save_image_jpg(image p, const char *name)
00512 {
00513 image copy = copy_image(p);
00514 if(p.c == 3) rgbgr_image(copy);
00515 int x,y,k;
00516
00517 char buff[256];
00518 sprintf(buff, "%s.jpg", name);
00519
00520 IplImage *disp = cvCreateImage(cvSize(p.w,p.h), IPL_DEPTH_8U, p.c);
00521 int step = disp->widthStep;
00522 for(y = 0; y < p.h; ++y){
00523 for(x = 0; x < p.w; ++x){
00524 for(k= 0; k < p.c; ++k){
00525 disp->imageData[y*step + x*p.c + k] = (unsigned char)(get_pixel(copy,x,y,k)*255);
00526 }
00527 }
00528 }
00529 cvSaveImage(buff, disp,0);
00530 cvReleaseImage(&disp);
00531 free_image(copy);
00532 }
00533 #endif
00534
00535 void save_image_png(image im, const char *name)
00536 {
00537 char buff[256];
00538
00539 sprintf(buff, "%s.png", name);
00540 unsigned char *data = calloc(im.w*im.h*im.c, sizeof(char));
00541 int i,k;
00542 for(k = 0; k < im.c; ++k){
00543 for(i = 0; i < im.w*im.h; ++i){
00544 data[i*im.c+k] = (unsigned char) (255*im.data[i + k*im.w*im.h]);
00545 }
00546 }
00547 int success = stbi_write_png(buff, im.w, im.h, im.c, data, im.w*im.c);
00548 free(data);
00549 if(!success) fprintf(stderr, "Failed to write image %s\n", buff);
00550 }
00551
00552 void save_image(image im, const char *name)
00553 {
00554 #ifdef OPENCV
00555 save_image_jpg(im, name);
00556 #else
00557 save_image_png(im, name);
00558 #endif
00559 }
00560
00561
00562 void show_image_layers(image p, char *name)
00563 {
00564 int i;
00565 char buff[256];
00566 for(i = 0; i < p.c; ++i){
00567 sprintf(buff, "%s - Layer %d", name, i);
00568 image layer = get_image_layer(p, i);
00569 show_image(layer, buff);
00570 free_image(layer);
00571 }
00572 }
00573
00574 void show_image_collapsed(image p, char *name)
00575 {
00576 image c = collapse_image_layers(p, 1);
00577 show_image(c, name);
00578 free_image(c);
00579 }
00580
00581 image make_empty_image(int w, int h, int c)
00582 {
00583 image out;
00584 out.data = 0;
00585 out.h = h;
00586 out.w = w;
00587 out.c = c;
00588 return out;
00589 }
00590
00591 image make_image(int w, int h, int c)
00592 {
00593 image out = make_empty_image(w,h,c);
00594 out.data = calloc(h*w*c, sizeof(float));
00595 return out;
00596 }
00597
00598 image make_random_image(int w, int h, int c)
00599 {
00600 image out = make_empty_image(w,h,c);
00601 out.data = calloc(h*w*c, sizeof(float));
00602 int i;
00603 for(i = 0; i < w*h*c; ++i){
00604 out.data[i] = (rand_normal() * .25) + .5;
00605 }
00606 return out;
00607 }
00608
00609 image float_to_image(int w, int h, int c, float *data)
00610 {
00611 image out = make_empty_image(w,h,c);
00612 out.data = data;
00613 return out;
00614 }
00615
00616
00617 image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect)
00618 {
00619 int x, y, c;
00620 float cx = im.w/2.;
00621 float cy = im.h/2.;
00622 image rot = make_image(w, h, im.c);
00623 for(c = 0; c < im.c; ++c){
00624 for(y = 0; y < h; ++y){
00625 for(x = 0; x < w; ++x){
00626 float rx = cos(rad)*((x - w/2.)/s*aspect + dx/s*aspect) - sin(rad)*((y - h/2.)/s + dy/s) + cx;
00627 float ry = sin(rad)*((x - w/2.)/s*aspect + dx/s*aspect) + cos(rad)*((y - h/2.)/s + dy/s) + cy;
00628 float val = bilinear_interpolate(im, rx, ry, c);
00629 set_pixel(rot, x, y, c, val);
00630 }
00631 }
00632 }
00633 return rot;
00634 }
00635
00636 image rotate_image(image im, float rad)
00637 {
00638 int x, y, c;
00639 float cx = im.w/2.;
00640 float cy = im.h/2.;
00641 image rot = make_image(im.w, im.h, im.c);
00642 for(c = 0; c < im.c; ++c){
00643 for(y = 0; y < im.h; ++y){
00644 for(x = 0; x < im.w; ++x){
00645 float rx = cos(rad)*(x-cx) - sin(rad)*(y-cy) + cx;
00646 float ry = sin(rad)*(x-cx) + cos(rad)*(y-cy) + cy;
00647 float val = bilinear_interpolate(im, rx, ry, c);
00648 set_pixel(rot, x, y, c, val);
00649 }
00650 }
00651 }
00652 return rot;
00653 }
00654
00655 void translate_image(image m, float s)
00656 {
00657 int i;
00658 for(i = 0; i < m.h*m.w*m.c; ++i) m.data[i] += s;
00659 }
00660
00661 void scale_image(image m, float s)
00662 {
00663 int i;
00664 for(i = 0; i < m.h*m.w*m.c; ++i) m.data[i] *= s;
00665 }
00666
00667 image crop_image(image im, int dx, int dy, int w, int h)
00668 {
00669 image cropped = make_image(w, h, im.c);
00670 int i, j, k;
00671 for(k = 0; k < im.c; ++k){
00672 for(j = 0; j < h; ++j){
00673 for(i = 0; i < w; ++i){
00674 int r = j + dy;
00675 int c = i + dx;
00676 float val = 0;
00677 r = constrain_int(r, 0, im.h-1);
00678 c = constrain_int(c, 0, im.w-1);
00679 if (r >= 0 && r < im.h && c >= 0 && c < im.w) {
00680 val = get_pixel(im, c, r, k);
00681 }
00682 set_pixel(cropped, i, j, k, val);
00683 }
00684 }
00685 }
00686 return cropped;
00687 }
00688
00689 int best_3d_shift_r(image a, image b, int min, int max)
00690 {
00691 if(min == max) return min;
00692 int mid = floor((min + max) / 2.);
00693 image c1 = crop_image(b, 0, mid, b.w, b.h);
00694 image c2 = crop_image(b, 0, mid+1, b.w, b.h);
00695 float d1 = dist_array(c1.data, a.data, a.w*a.h*a.c, 10);
00696 float d2 = dist_array(c2.data, a.data, a.w*a.h*a.c, 10);
00697 free_image(c1);
00698 free_image(c2);
00699 if(d1 < d2) return best_3d_shift_r(a, b, min, mid);
00700 else return best_3d_shift_r(a, b, mid+1, max);
00701 }
00702
00703 int best_3d_shift(image a, image b, int min, int max)
00704 {
00705 int i;
00706 int best = 0;
00707 float best_distance = FLT_MAX;
00708 for(i = min; i <= max; i += 2){
00709 image c = crop_image(b, 0, i, b.w, b.h);
00710 float d = dist_array(c.data, a.data, a.w*a.h*a.c, 100);
00711 if(d < best_distance){
00712 best_distance = d;
00713 best = i;
00714 }
00715 printf("%d %f\n", i, d);
00716 free_image(c);
00717 }
00718 return best;
00719 }
00720
00721 void composite_3d(char *f1, char *f2, char *out, int delta)
00722 {
00723 if(!out) out = "out";
00724 image a = load_image(f1, 0,0,0);
00725 image b = load_image(f2, 0,0,0);
00726 int shift = best_3d_shift_r(a, b, -a.h/100, a.h/100);
00727
00728 image c1 = crop_image(b, 10, shift, b.w, b.h);
00729 float d1 = dist_array(c1.data, a.data, a.w*a.h*a.c, 100);
00730 image c2 = crop_image(b, -10, shift, b.w, b.h);
00731 float d2 = dist_array(c2.data, a.data, a.w*a.h*a.c, 100);
00732
00733 if(d2 < d1 && 0){
00734 image swap = a;
00735 a = b;
00736 b = swap;
00737 shift = -shift;
00738 printf("swapped, %d\n", shift);
00739 }
00740 else{
00741 printf("%d\n", shift);
00742 }
00743
00744 image c = crop_image(b, delta, shift, a.w, a.h);
00745 int i;
00746 for(i = 0; i < c.w*c.h; ++i){
00747 c.data[i] = a.data[i];
00748 }
00749 #ifdef OPENCV
00750 save_image_jpg(c, out);
00751 #else
00752 save_image(c, out);
00753 #endif
00754 }
00755
00756 image resize_max(image im, int max)
00757 {
00758 int w = im.w;
00759 int h = im.h;
00760 if(w > h){
00761 h = (h * max) / w;
00762 w = max;
00763 } else {
00764 w = (w * max) / h;
00765 h = max;
00766 }
00767 if(w == im.w && h == im.h) return im;
00768 image resized = resize_image(im, w, h);
00769 return resized;
00770 }
00771
00772 image resize_min(image im, int min)
00773 {
00774 int w = im.w;
00775 int h = im.h;
00776 if(w < h){
00777 h = (h * min) / w;
00778 w = min;
00779 } else {
00780 w = (w * min) / h;
00781 h = min;
00782 }
00783 if(w == im.w && h == im.h) return im;
00784 image resized = resize_image(im, w, h);
00785 return resized;
00786 }
00787
00788 image random_crop_image(image im, int w, int h)
00789 {
00790 int dx = rand_int(0, im.w - w);
00791 int dy = rand_int(0, im.h - h);
00792 image crop = crop_image(im, dx, dy, w, h);
00793 return crop;
00794 }
00795
00796 image random_augment_image(image im, float angle, float aspect, int low, int high, int size)
00797 {
00798 aspect = rand_scale(aspect);
00799 int r = rand_int(low, high);
00800 int min = (im.h < im.w*aspect) ? im.h : im.w*aspect;
00801 float scale = (float)r / min;
00802
00803 float rad = rand_uniform(-angle, angle) * TWO_PI / 360.;
00804
00805 float dx = (im.w*scale/aspect - size) / 2.;
00806 float dy = (im.h*scale - size) / 2.;
00807 if(dx < 0) dx = 0;
00808 if(dy < 0) dy = 0;
00809 dx = rand_uniform(-dx, dx);
00810 dy = rand_uniform(-dy, dy);
00811
00812 image crop = rotate_crop_image(im, rad, scale, size, size, dx, dy, aspect);
00813
00814 return crop;
00815 }
00816
00817 float three_way_max(float a, float b, float c)
00818 {
00819 return (a > b) ? ( (a > c) ? a : c) : ( (b > c) ? b : c) ;
00820 }
00821
00822 float three_way_min(float a, float b, float c)
00823 {
00824 return (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c) ;
00825 }
00826
00827
00828 void rgb_to_hsv(image im)
00829 {
00830 assert(im.c == 3);
00831 int i, j;
00832 float r, g, b;
00833 float h, s, v;
00834 for(j = 0; j < im.h; ++j){
00835 for(i = 0; i < im.w; ++i){
00836 r = get_pixel(im, i , j, 0);
00837 g = get_pixel(im, i , j, 1);
00838 b = get_pixel(im, i , j, 2);
00839 float max = three_way_max(r,g,b);
00840 float min = three_way_min(r,g,b);
00841 float delta = max - min;
00842 v = max;
00843 if(max == 0){
00844 s = 0;
00845 h = 0;
00846 }else{
00847 s = delta/max;
00848 if(r == max){
00849 h = (g - b) / delta;
00850 } else if (g == max) {
00851 h = 2 + (b - r) / delta;
00852 } else {
00853 h = 4 + (r - g) / delta;
00854 }
00855 if (h < 0) h += 6;
00856 h = h/6.;
00857 }
00858 set_pixel(im, i, j, 0, h);
00859 set_pixel(im, i, j, 1, s);
00860 set_pixel(im, i, j, 2, v);
00861 }
00862 }
00863 }
00864
00865 void hsv_to_rgb(image im)
00866 {
00867 assert(im.c == 3);
00868 int i, j;
00869 float r, g, b;
00870 float h, s, v;
00871 float f, p, q, t;
00872 for(j = 0; j < im.h; ++j){
00873 for(i = 0; i < im.w; ++i){
00874 h = 6 * get_pixel(im, i , j, 0);
00875 s = get_pixel(im, i , j, 1);
00876 v = get_pixel(im, i , j, 2);
00877 if (s == 0) {
00878 r = g = b = v;
00879 } else {
00880 int index = floor(h);
00881 f = h - index;
00882 p = v*(1-s);
00883 q = v*(1-s*f);
00884 t = v*(1-s*(1-f));
00885 if(index == 0){
00886 r = v; g = t; b = p;
00887 } else if(index == 1){
00888 r = q; g = v; b = p;
00889 } else if(index == 2){
00890 r = p; g = v; b = t;
00891 } else if(index == 3){
00892 r = p; g = q; b = v;
00893 } else if(index == 4){
00894 r = t; g = p; b = v;
00895 } else {
00896 r = v; g = p; b = q;
00897 }
00898 }
00899 set_pixel(im, i, j, 0, r);
00900 set_pixel(im, i, j, 1, g);
00901 set_pixel(im, i, j, 2, b);
00902 }
00903 }
00904 }
00905
00906 image grayscale_image(image im)
00907 {
00908 assert(im.c == 3);
00909 int i, j, k;
00910 image gray = make_image(im.w, im.h, 1);
00911 float scale[] = {0.587, 0.299, 0.114};
00912 for(k = 0; k < im.c; ++k){
00913 for(j = 0; j < im.h; ++j){
00914 for(i = 0; i < im.w; ++i){
00915 gray.data[i+im.w*j] += scale[k]*get_pixel(im, i, j, k);
00916 }
00917 }
00918 }
00919 return gray;
00920 }
00921
00922 image threshold_image(image im, float thresh)
00923 {
00924 int i;
00925 image t = make_image(im.w, im.h, im.c);
00926 for(i = 0; i < im.w*im.h*im.c; ++i){
00927 t.data[i] = im.data[i]>thresh ? 1 : 0;
00928 }
00929 return t;
00930 }
00931
00932 image blend_image(image fore, image back, float alpha)
00933 {
00934 assert(fore.w == back.w && fore.h == back.h && fore.c == back.c);
00935 image blend = make_image(fore.w, fore.h, fore.c);
00936 int i, j, k;
00937 for(k = 0; k < fore.c; ++k){
00938 for(j = 0; j < fore.h; ++j){
00939 for(i = 0; i < fore.w; ++i){
00940 float val = alpha * get_pixel(fore, i, j, k) +
00941 (1 - alpha)* get_pixel(back, i, j, k);
00942 set_pixel(blend, i, j, k, val);
00943 }
00944 }
00945 }
00946 return blend;
00947 }
00948
00949 void scale_image_channel(image im, int c, float v)
00950 {
00951 int i, j;
00952 for(j = 0; j < im.h; ++j){
00953 for(i = 0; i < im.w; ++i){
00954 float pix = get_pixel(im, i, j, c);
00955 pix = pix*v;
00956 set_pixel(im, i, j, c, pix);
00957 }
00958 }
00959 }
00960
00961 void translate_image_channel(image im, int c, float v)
00962 {
00963 int i, j;
00964 for(j = 0; j < im.h; ++j){
00965 for(i = 0; i < im.w; ++i){
00966 float pix = get_pixel(im, i, j, c);
00967 pix = pix+v;
00968 set_pixel(im, i, j, c, pix);
00969 }
00970 }
00971 }
00972
00973 image binarize_image(image im)
00974 {
00975 image c = copy_image(im);
00976 int i;
00977 for(i = 0; i < im.w * im.h * im.c; ++i){
00978 if(c.data[i] > .5) c.data[i] = 1;
00979 else c.data[i] = 0;
00980 }
00981 return c;
00982 }
00983
00984 void saturate_image(image im, float sat)
00985 {
00986 rgb_to_hsv(im);
00987 scale_image_channel(im, 1, sat);
00988 hsv_to_rgb(im);
00989 constrain_image(im);
00990 }
00991
00992 void hue_image(image im, float hue)
00993 {
00994 rgb_to_hsv(im);
00995 int i;
00996 for(i = 0; i < im.w*im.h; ++i){
00997 im.data[i] = im.data[i] + hue;
00998 if (im.data[i] > 1) im.data[i] -= 1;
00999 if (im.data[i] < 0) im.data[i] += 1;
01000 }
01001 hsv_to_rgb(im);
01002 constrain_image(im);
01003 }
01004
01005 void exposure_image(image im, float sat)
01006 {
01007 rgb_to_hsv(im);
01008 scale_image_channel(im, 2, sat);
01009 hsv_to_rgb(im);
01010 constrain_image(im);
01011 }
01012
01013 void distort_image(image im, float hue, float sat, float val)
01014 {
01015 rgb_to_hsv(im);
01016 scale_image_channel(im, 1, sat);
01017 scale_image_channel(im, 2, val);
01018 int i;
01019 for(i = 0; i < im.w*im.h; ++i){
01020 im.data[i] = im.data[i] + hue;
01021 if (im.data[i] > 1) im.data[i] -= 1;
01022 if (im.data[i] < 0) im.data[i] += 1;
01023 }
01024 hsv_to_rgb(im);
01025 constrain_image(im);
01026 }
01027
01028 void random_distort_image(image im, float hue, float saturation, float exposure)
01029 {
01030 float dhue = rand_uniform(-hue, hue);
01031 float dsat = rand_scale(saturation);
01032 float dexp = rand_scale(exposure);
01033 distort_image(im, dhue, dsat, dexp);
01034 }
01035
01036 void saturate_exposure_image(image im, float sat, float exposure)
01037 {
01038 rgb_to_hsv(im);
01039 scale_image_channel(im, 1, sat);
01040 scale_image_channel(im, 2, exposure);
01041 hsv_to_rgb(im);
01042 constrain_image(im);
01043 }
01044
01045 float bilinear_interpolate(image im, float x, float y, int c)
01046 {
01047 int ix = (int) floorf(x);
01048 int iy = (int) floorf(y);
01049
01050 float dx = x - ix;
01051 float dy = y - iy;
01052
01053 float val = (1-dy) * (1-dx) * get_pixel_extend(im, ix, iy, c) +
01054 dy * (1-dx) * get_pixel_extend(im, ix, iy+1, c) +
01055 (1-dy) * dx * get_pixel_extend(im, ix+1, iy, c) +
01056 dy * dx * get_pixel_extend(im, ix+1, iy+1, c);
01057 return val;
01058 }
01059
01060 image resize_image(image im, int w, int h)
01061 {
01062 image resized = make_image(w, h, im.c);
01063 image part = make_image(w, im.h, im.c);
01064 int r, c, k;
01065 float w_scale = (float)(im.w - 1) / (w - 1);
01066 float h_scale = (float)(im.h - 1) / (h - 1);
01067 for(k = 0; k < im.c; ++k){
01068 for(r = 0; r < im.h; ++r){
01069 for(c = 0; c < w; ++c){
01070 float val = 0;
01071 if(c == w-1 || im.w == 1){
01072 val = get_pixel(im, im.w-1, r, k);
01073 } else {
01074 float sx = c*w_scale;
01075 int ix = (int) sx;
01076 float dx = sx - ix;
01077 val = (1 - dx) * get_pixel(im, ix, r, k) + dx * get_pixel(im, ix+1, r, k);
01078 }
01079 set_pixel(part, c, r, k, val);
01080 }
01081 }
01082 }
01083 for(k = 0; k < im.c; ++k){
01084 for(r = 0; r < h; ++r){
01085 float sy = r*h_scale;
01086 int iy = (int) sy;
01087 float dy = sy - iy;
01088 for(c = 0; c < w; ++c){
01089 float val = (1-dy) * get_pixel(part, c, iy, k);
01090 set_pixel(resized, c, r, k, val);
01091 }
01092 if(r == h-1 || im.h == 1) continue;
01093 for(c = 0; c < w; ++c){
01094 float val = dy * get_pixel(part, c, iy+1, k);
01095 add_pixel(resized, c, r, k, val);
01096 }
01097 }
01098 }
01099
01100 free_image(part);
01101 return resized;
01102 }
01103
01104
01105 void test_resize(char *filename)
01106 {
01107 image im = load_image(filename, 0,0, 3);
01108 float mag = mag_array(im.data, im.w*im.h*im.c);
01109 printf("L2 Norm: %f\n", mag);
01110 image gray = grayscale_image(im);
01111
01112 image c1 = copy_image(im);
01113 image c2 = copy_image(im);
01114 image c3 = copy_image(im);
01115 image c4 = copy_image(im);
01116 distort_image(c1, .1, 1.5, 1.5);
01117 distort_image(c2, -.1, .66666, .66666);
01118 distort_image(c3, .1, 1.5, .66666);
01119 distort_image(c4, .1, .66666, 1.5);
01120
01121
01122 show_image(im, "Original");
01123 show_image(gray, "Gray");
01124 show_image(c1, "C1");
01125 show_image(c2, "C2");
01126 show_image(c3, "C3");
01127 show_image(c4, "C4");
01128 #ifdef OPENCV
01129 while(1){
01130 image aug = random_augment_image(im, 0, .75, 320, 448, 320);
01131 show_image(aug, "aug");
01132 free_image(aug);
01133
01134
01135 float exposure = 1.15;
01136 float saturation = 1.15;
01137 float hue = .05;
01138
01139 image c = copy_image(im);
01140
01141 float dexp = rand_scale(exposure);
01142 float dsat = rand_scale(saturation);
01143 float dhue = rand_uniform(-hue, hue);
01144
01145 distort_image(c, dhue, dsat, dexp);
01146 show_image(c, "rand");
01147 printf("%f %f %f\n", dhue, dsat, dexp);
01148 free_image(c);
01149 cvWaitKey(0);
01150 }
01151 #endif
01152 }
01153
01154
01155 image load_image_stb(char *filename, int channels)
01156 {
01157 int w, h, c;
01158 unsigned char *data = stbi_load(filename, &w, &h, &c, channels);
01159 if (!data) {
01160 fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n", filename, stbi_failure_reason());
01161 exit(0);
01162 }
01163 if(channels) c = channels;
01164 int i,j,k;
01165 image im = make_image(w, h, c);
01166 for(k = 0; k < c; ++k){
01167 for(j = 0; j < h; ++j){
01168 for(i = 0; i < w; ++i){
01169 int dst_index = i + w*j + w*h*k;
01170 int src_index = k + c*i + c*w*j;
01171 im.data[dst_index] = (float)data[src_index]/255.;
01172 }
01173 }
01174 }
01175 free(data);
01176 return im;
01177 }
01178
01179 image load_image(char *filename, int w, int h, int c)
01180 {
01181 #ifdef OPENCV
01182 image out = load_image_cv(filename, c);
01183 #else
01184 image out = load_image_stb(filename, c);
01185 #endif
01186
01187 if((h && w) && (h != out.h || w != out.w)){
01188 image resized = resize_image(out, w, h);
01189 free_image(out);
01190 out = resized;
01191 }
01192 return out;
01193 }
01194
01195 image load_image_color(char *filename, int w, int h)
01196 {
01197 return load_image(filename, w, h, 3);
01198 }
01199
01200 image get_image_layer(image m, int l)
01201 {
01202 image out = make_image(m.w, m.h, 1);
01203 int i;
01204 for(i = 0; i < m.h*m.w; ++i){
01205 out.data[i] = m.data[i+l*m.h*m.w];
01206 }
01207 return out;
01208 }
01209
01210 float get_pixel(image m, int x, int y, int c)
01211 {
01212 assert(x < m.w && y < m.h && c < m.c);
01213 return m.data[c*m.h*m.w + y*m.w + x];
01214 }
01215 float get_pixel_extend(image m, int x, int y, int c)
01216 {
01217 if(x < 0) x = 0;
01218 if(x >= m.w) x = m.w-1;
01219 if(y < 0) y = 0;
01220 if(y >= m.h) y = m.h-1;
01221 if(c < 0 || c >= m.c) return 0;
01222 return get_pixel(m, x, y, c);
01223 }
01224 void set_pixel(image m, int x, int y, int c, float val)
01225 {
01226 if (x < 0 || y < 0 || c < 0 || x >= m.w || y >= m.h || c >= m.c) return;
01227 assert(x < m.w && y < m.h && c < m.c);
01228 m.data[c*m.h*m.w + y*m.w + x] = val;
01229 }
01230 void add_pixel(image m, int x, int y, int c, float val)
01231 {
01232 assert(x < m.w && y < m.h && c < m.c);
01233 m.data[c*m.h*m.w + y*m.w + x] += val;
01234 }
01235
01236 void print_image(image m)
01237 {
01238 int i, j, k;
01239 for(i =0 ; i < m.c; ++i){
01240 for(j =0 ; j < m.h; ++j){
01241 for(k = 0; k < m.w; ++k){
01242 printf("%.2lf, ", m.data[i*m.h*m.w + j*m.w + k]);
01243 if(k > 30) break;
01244 }
01245 printf("\n");
01246 if(j > 30) break;
01247 }
01248 printf("\n");
01249 }
01250 printf("\n");
01251 }
01252
01253 image collapse_images_vert(image *ims, int n)
01254 {
01255 int color = 1;
01256 int border = 1;
01257 int h,w,c;
01258 w = ims[0].w;
01259 h = (ims[0].h + border) * n - border;
01260 c = ims[0].c;
01261 if(c != 3 || !color){
01262 w = (w+border)*c - border;
01263 c = 1;
01264 }
01265
01266 image filters = make_image(w, h, c);
01267 int i,j;
01268 for(i = 0; i < n; ++i){
01269 int h_offset = i*(ims[0].h+border);
01270 image copy = copy_image(ims[i]);
01271
01272 if(c == 3 && color){
01273 embed_image(copy, filters, 0, h_offset);
01274 }
01275 else{
01276 for(j = 0; j < copy.c; ++j){
01277 int w_offset = j*(ims[0].w+border);
01278 image layer = get_image_layer(copy, j);
01279 embed_image(layer, filters, w_offset, h_offset);
01280 free_image(layer);
01281 }
01282 }
01283 free_image(copy);
01284 }
01285 return filters;
01286 }
01287
01288 image collapse_images_horz(image *ims, int n)
01289 {
01290 int color = 1;
01291 int border = 1;
01292 int h,w,c;
01293 int size = ims[0].h;
01294 h = size;
01295 w = (ims[0].w + border) * n - border;
01296 c = ims[0].c;
01297 if(c != 3 || !color){
01298 h = (h+border)*c - border;
01299 c = 1;
01300 }
01301
01302 image filters = make_image(w, h, c);
01303 int i,j;
01304 for(i = 0; i < n; ++i){
01305 int w_offset = i*(size+border);
01306 image copy = copy_image(ims[i]);
01307
01308 if(c == 3 && color){
01309 embed_image(copy, filters, w_offset, 0);
01310 }
01311 else{
01312 for(j = 0; j < copy.c; ++j){
01313 int h_offset = j*(size+border);
01314 image layer = get_image_layer(copy, j);
01315 embed_image(layer, filters, w_offset, h_offset);
01316 free_image(layer);
01317 }
01318 }
01319 free_image(copy);
01320 }
01321 return filters;
01322 }
01323
01324 void show_image_normalized(image im, const char *name)
01325 {
01326 image c = copy_image(im);
01327 normalize_image(c);
01328 show_image(c, name);
01329 free_image(c);
01330 }
01331
01332 void show_images(image *ims, int n, char *window)
01333 {
01334 image m = collapse_images_vert(ims, n);
01335
01336
01337
01338
01339
01340
01341
01342
01343
01344 normalize_image(m);
01345 save_image(m, window);
01346 show_image(m, window);
01347 free_image(m);
01348 }
01349
01350 void free_image(image m)
01351 {
01352 if(m.data){
01353 free(m.data);
01354 }
01355 }