You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1744 lines
41 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "image.h"
  2. #include "utils.h"
  3. #include "blas.h"
  4. #include "dn_cuda.h"
  5. #include <stdio.h>
  6. #include <math.h>
  7. #define STB_IMAGE_IMPLEMENTATION
  8. #include "stb_image.h"
  9. #define STB_IMAGE_WRITE_IMPLEMENTATION
  10. #include "stb_image_write.h"
  11. using namespace cv;
  12. namespace DN{
  13. int windows = 0;
  14. float colors[6][3] = { {1,0,1}, {0,0,1},{0,1,1},{0,1,0},{1,1,0},{1,0,0} };
  15. float get_color(int c, int x, int max)
  16. {
  17. float ratio = ((float)x/max)*5;
  18. int i = (int)floor(ratio);
  19. int j = (int)ceil(ratio);
  20. ratio -= i;
  21. float r = (1-ratio) * colors[i][c] + ratio*colors[j][c];
  22. //printf("%f\n", r);
  23. return r;
  24. }
  25. image mask_to_rgb(image mask)
  26. {
  27. #ifndef EXT_CV
  28. int n = mask.c;
  29. image im = make_image(mask.w, mask.h, 3);
  30. int i, j;
  31. for(j = 0; j < n; ++j){
  32. int offset = j*123457 % n;
  33. float red = get_color(2,offset,n);
  34. float green = get_color(1,offset,n);
  35. float blue = get_color(0,offset,n);
  36. for(i = 0; i < im.w*im.h; ++i){
  37. im.data[i + 0*im.w*im.h] += mask.data[j*im.h*im.w + i]*red;
  38. im.data[i + 1*im.w*im.h] += mask.data[j*im.h*im.w + i]*green;
  39. im.data[i + 2*im.w*im.h] += mask.data[j*im.h*im.w + i]*blue;
  40. }
  41. }
  42. #else
  43. int n = mask.channels();
  44. image im;
  45. int i, j;
  46. for (j = 0; j < n; ++j){
  47. int offset = j * 123457 % n;
  48. float red = get_color(2, offset, n);
  49. float green = get_color(1, offset, n);
  50. float blue = get_color(0, offset, n);
  51. for (i = 0; i < im.cols*im.rows; ++i){
  52. im.data[i + 0 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * red;
  53. im.data[i + 1 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * green;
  54. im.data[i + 2 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * blue;
  55. }
  56. }
  57. #endif
  58. return im;
  59. }
  60. void composite_image(image source, image dest, int dx, int dy)
  61. {
  62. int x,y,k;
  63. #ifndef EXT_CV
  64. for(k = 0; k < source.c; ++k){
  65. for(y = 0; y < source.h; ++y){
  66. for(x = 0; x < source.w; ++x){
  67. float val = get_pixel(source, x, y, k);
  68. float val2 = get_pixel_extend(dest, dx+x, dy+y, k);
  69. set_pixel(dest, dx+x, dy+y, k, val * val2);
  70. }
  71. }
  72. }
  73. #else
  74. for (k = 0; k < source.channels(); ++k){
  75. for (y = 0; y < source.rows; ++y){
  76. for (x = 0; x < source.cols; ++x){
  77. float val = get_pixel(source, x, y, k);
  78. float val2 = get_pixel_extend(dest, dx + x, dy + y, k);
  79. set_pixel(dest, dx + x, dy + y, k, val * val2);
  80. }
  81. }
  82. }
  83. #endif
  84. }
  85. image border_image(image a, int border)
  86. {
  87. #ifndef EXT_CV
  88. image b = make_image(a.w + 2*border, a.h + 2*border, a.c);
  89. int x,y,k;
  90. for(k = 0; k < b.c; ++k){
  91. for(y = 0; y < b.h; ++y){
  92. for(x = 0; x < b.w; ++x){
  93. float val = get_pixel_extend(a, x - border, y - border, k);
  94. if(x - border < 0 || x - border >= a.w || y - border < 0 || y - border >= a.h) val = 1;
  95. set_pixel(b, x, y, k, val);
  96. }
  97. }
  98. }
  99. #else
  100. image b = image(a.rows + 2 * border, a.cols + 2 * border, a.type());
  101. int x, y, k;
  102. for (k = 0; k < b.channels(); ++k){
  103. for (y = 0; y < b.rows; ++y){
  104. for (x = 0; x < b.cols; ++x){
  105. float val = get_pixel_extend(a, x - border, y - border, k);
  106. if (x - border < 0 || x - border >= a.cols || y - border < 0 || y - border >= a.rows) val = 1;
  107. set_pixel(b, x, y, k, val);
  108. }
  109. }
  110. }
  111. #endif
  112. return b;
  113. }
  114. image tile_images(image a, image b, int dx)
  115. {
  116. #ifndef EXT_CV
  117. if(a.w == 0) return copy_image(b);
  118. 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);
  119. fill_cpu(c.w*c.h*c.c, 1, c.data, 1);
  120. embed_image(a, c, 0, 0);
  121. composite_image(b, c, a.w + dx, 0);
  122. #else
  123. image c;
  124. if (a.cols == 0){
  125. b.copyTo(c);
  126. } else {
  127. c = image((a.rows > b.rows) ? a.rows : b.rows, a.cols + b.cols + dx, (a.channels() > b.channels()) ? a.type() : b.type());
  128. //fill_cpu(c.cols*c.rows*c.channels(), 1, c.data, 1);
  129. embed_image(a, c, 0, 0);
  130. composite_image(b, c, a.cols + dx, 0);
  131. };
  132. #endif
  133. return c;
  134. }
  135. image get_label(image **characters, char *string, int size)
  136. {
  137. if(size > 7) size = 7;
  138. image label = make_empty_image(0,0,0);
  139. while(*string){
  140. image l = characters[size][(int)*string];
  141. image n = tile_images(label, l, -size - 1 + (size+1)/2);
  142. free_image(label);
  143. label = n;
  144. ++string;
  145. }
  146. image b = border_image(label, (int)(label.h*.25));
  147. free_image(label);
  148. return b;
  149. }
  150. void draw_label(image a, int r, int c, image label, const float *rgb)
  151. {
  152. int w = label.w;
  153. int h = label.h;
  154. if (r - h >= 0) r = r - h;
  155. int i, j, k;
  156. for(j = 0; j < h && j + r < a.h; ++j){
  157. for(i = 0; i < w && i + c < a.w; ++i){
  158. for(k = 0; k < label.c; ++k){
  159. float val = get_pixel(label, i, j, k);
  160. set_pixel(a, i+c, j+r, k, rgb[k] * val);
  161. }
  162. }
  163. }
  164. }
  165. void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b)
  166. {
  167. //normalize_image(a);
  168. int i;
  169. if(x1 < 0) x1 = 0;
  170. if(x1 >= a.w) x1 = a.w-1;
  171. if(x2 < 0) x2 = 0;
  172. if(x2 >= a.w) x2 = a.w-1;
  173. if(y1 < 0) y1 = 0;
  174. if(y1 >= a.h) y1 = a.h-1;
  175. if(y2 < 0) y2 = 0;
  176. if(y2 >= a.h) y2 = a.h-1;
  177. for(i = x1; i <= x2; ++i){
  178. a.data[i + y1*a.w + 0*a.w*a.h] = r;
  179. a.data[i + y2*a.w + 0*a.w*a.h] = r;
  180. a.data[i + y1*a.w + 1*a.w*a.h] = g;
  181. a.data[i + y2*a.w + 1*a.w*a.h] = g;
  182. a.data[i + y1*a.w + 2*a.w*a.h] = b;
  183. a.data[i + y2*a.w + 2*a.w*a.h] = b;
  184. }
  185. for(i = y1; i <= y2; ++i){
  186. a.data[x1 + i*a.w + 0*a.w*a.h] = r;
  187. a.data[x2 + i*a.w + 0*a.w*a.h] = r;
  188. a.data[x1 + i*a.w + 1*a.w*a.h] = g;
  189. a.data[x2 + i*a.w + 1*a.w*a.h] = g;
  190. a.data[x1 + i*a.w + 2*a.w*a.h] = b;
  191. a.data[x2 + i*a.w + 2*a.w*a.h] = b;
  192. }
  193. }
  194. void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b)
  195. {
  196. int i;
  197. for(i = 0; i < w; ++i){
  198. draw_box(a, x1+i, y1+i, x2-i, y2-i, r, g, b);
  199. }
  200. }
  201. void draw_bbox(image a, box bbox, int w, float r, float g, float b)
  202. {
  203. int left = (int)(bbox.x-bbox.w/2)*a.w;
  204. int right = (int)(bbox.x + bbox.w / 2)*a.w;
  205. int top = (int)(bbox.y - bbox.h / 2)*a.h;
  206. int bot = (int)(bbox.y + bbox.h / 2)*a.h;
  207. int i;
  208. for(i = 0; i < w; ++i){
  209. draw_box(a, left+i, top+i, right-i, bot-i, r, g, b);
  210. }
  211. }
  212. image **load_alphabet()
  213. {
  214. int i, j;
  215. const int nsize = 8;
  216. image **alphabets = (image**)calloc(nsize, sizeof(image));
  217. for(j = 0; j < nsize; ++j){
  218. alphabets[j] = (image*)calloc(128, sizeof(image));
  219. for(i = 32; i < 127; ++i){
  220. char buff[256];
  221. sprintf(buff, "data/labels/%d_%d.png", i, j);
  222. alphabets[j][i] = load_image_color(buff, 0, 0);
  223. }
  224. }
  225. return alphabets;
  226. }
  227. void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes){
  228. int i;
  229. for(i = 0; i < num; ++i){
  230. int cls = max_index(probs[i], classes);
  231. float prob = probs[i][cls];
  232. if(prob > thresh){
  233. int width = (int)(im.h * .006);
  234. if(0){
  235. width = (int)( pow(prob, (float)(1./2.))*10+1);
  236. alphabet = 0;
  237. }
  238. //printf("%d %s: %.0f%%\n", i, names[class], prob*100);
  239. printf("%s: %.0f%%\n", names[cls], prob*100);
  240. int offset = cls*123457 % classes;
  241. float red = get_color(2,offset,classes);
  242. float green = get_color(1,offset,classes);
  243. float blue = get_color(0,offset,classes);
  244. float rgb[3];
  245. //width = prob*20+2;
  246. rgb[0] = red;
  247. rgb[1] = green;
  248. rgb[2] = blue;
  249. box b = boxes[i];
  250. int left = (int)(b.x - b.w / 2.)*im.w;
  251. int right = (int)(b.x + b.w / 2.)*im.w;
  252. int top = (int)(b.y - b.h / 2.)*im.h;
  253. int bot = (int)(b.y + b.h / 2.)*im.h;
  254. if(left < 0) left = 0;
  255. if(right > im.w-1) right = im.w-1;
  256. if(top < 0) top = 0;
  257. if(bot > im.h-1) bot = im.h-1;
  258. draw_box_width(im, left, top, right, bot, width, red, green, blue);
  259. if (alphabet) {
  260. image label = get_label(alphabet, names[cls], (int)((im.h*.03) / 10));
  261. draw_label(im, top + width, left, label, rgb);
  262. free_image(label);
  263. }
  264. if (masks){
  265. image mask = float_to_image(14, 14, 1, masks[i]);
  266. image resized_mask = resize_image(mask, (int)(b.w*im.w), (int)(b.h*im.h));
  267. image tmask = threshold_image(resized_mask, .5);
  268. embed_image(tmask, im, left, top);
  269. free_image(mask);
  270. free_image(resized_mask);
  271. free_image(tmask);
  272. }
  273. }
  274. }
  275. }
  276. void drawMAT_detections(cv::Mat &im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, int classes){
  277. char Buffer[220];
  278. sprintf(Buffer, "Detected %d objects", num );
  279. rectangle( im, Point(im.cols - 120, 5), Point(im.cols - 10, 15), Scalar(0, 0, 255), CV_FILLED);
  280. putText( im, Buffer, Point(im.cols - 115, 13), FONT_HERSHEY_SIMPLEX,.3, Scalar(255, 255, 0), 1);
  281. for (int i = 0; i < num; ++i){
  282. int cls = max_index(probs[i], classes);
  283. float prob = probs[i][cls];
  284. if (prob > thresh){
  285. //int width = (int)(im.rows * .006);
  286. //printf("%d %s: %.0f%%\n", i, names[class], prob*100);
  287. sprintf(Buffer, "%s: %.0f%%", names[cls], prob * 100);
  288. int offset = cls * 123457 % classes;
  289. float red = get_color(2, offset, classes);
  290. float green = get_color(1, offset, classes);
  291. float blue = get_color(0, offset, classes);
  292. //float rgb[3];
  293. //width = prob*20+2;
  294. //rgb[0] = red;
  295. //rgb[1] = green;
  296. //rgb[2] = blue;
  297. box b = boxes[i];
  298. int left = (int)((b.x - (b.w / 2.f))*im.cols);
  299. int right = (int)((b.x + (b.w / 2.f))*im.cols);
  300. int top = (int)((b.y - (b.h / 2.f))*im.rows);
  301. int bot = (int)((b.y + (b.h / 2.f))*im.rows);
  302. if (left < 0) left = 0;
  303. if (right > im.cols - 1) right = im.cols - 1;
  304. if (top < 0) top = 0;
  305. if (bot > im.rows - 1) bot = im.rows - 1;
  306. putText(im, Buffer, Point(b.x*im.cols, b.y*im.rows), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 0), 2);
  307. rectangle(im, Point(left, top), Point(right, bot), Scalar(0, 255, 0), 2);
  308. //draw_box_width(im, left, top, right, bot, width, red, green, blue);
  309. };
  310. };
  311. }
  312. void transpose_image(image im)
  313. {
  314. assert(im.w == im.h);
  315. int n, m;
  316. int c;
  317. for(c = 0; c < im.c; ++c){
  318. for(n = 0; n < im.w-1; ++n){
  319. for(m = n + 1; m < im.w; ++m){
  320. float swap = im.data[m + im.w*(n + im.h*c)];
  321. im.data[m + im.w*(n + im.h*c)] = im.data[n + im.w*(m + im.h*c)];
  322. im.data[n + im.w*(m + im.h*c)] = swap;
  323. }
  324. }
  325. }
  326. }
  327. void rotate_image_cw(image im, int times)
  328. {
  329. assert(im.w == im.h);
  330. times = (times + 400) % 4;
  331. int i, x, y, c;
  332. int n = im.w;
  333. for(i = 0; i < times; ++i){
  334. for(c = 0; c < im.c; ++c){
  335. for(x = 0; x < n/2; ++x){
  336. for(y = 0; y < (n-1)/2 + 1; ++y){
  337. float temp = im.data[y + im.w*(x + im.h*c)];
  338. im.data[y + im.w*(x + im.h*c)] = im.data[n-1-x + im.w*(y + im.h*c)];
  339. im.data[n-1-x + im.w*(y + im.h*c)] = im.data[n-1-y + im.w*(n-1-x + im.h*c)];
  340. im.data[n-1-y + im.w*(n-1-x + im.h*c)] = im.data[x + im.w*(n-1-y + im.h*c)];
  341. im.data[x + im.w*(n-1-y + im.h*c)] = temp;
  342. }
  343. }
  344. }
  345. }
  346. }
  347. void flip_image(image a)
  348. {
  349. int i,j,k;
  350. for(k = 0; k < a.c; ++k){
  351. for(i = 0; i < a.h; ++i){
  352. for(j = 0; j < a.w/2; ++j){
  353. int index = j + a.w*(i + a.h*(k));
  354. int flip = (a.w - j - 1) + a.w*(i + a.h*(k));
  355. float swap = a.data[flip];
  356. a.data[flip] = a.data[index];
  357. a.data[index] = swap;
  358. }
  359. }
  360. }
  361. }
  362. image image_distance(image a, image b)
  363. {
  364. int i,j;
  365. image dist = make_image(a.w, a.h, 1);
  366. for(i = 0; i < a.c; ++i){
  367. for(j = 0; j < a.h*a.w; ++j){
  368. dist.data[j] += pow(a.data[i*a.h*a.w+j]-b.data[i*a.h*a.w+j],2);
  369. }
  370. }
  371. for(j = 0; j < a.h*a.w; ++j){
  372. dist.data[j] = sqrt(dist.data[j]);
  373. }
  374. return dist;
  375. }
  376. void ghost_image(image source, image dest, int dx, int dy)
  377. {
  378. int x,y,k;
  379. float max_dist = (float)sqrt((-source.w/2. + .5)*(-source.w/2. + .5));
  380. for(k = 0; k < source.c; ++k){
  381. for(y = 0; y < source.h; ++y){
  382. for(x = 0; x < source.w; ++x){
  383. float dist = (float)sqrt((x - source.w/2. + .5)*(x - source.w/2. + .5) + (y - source.h/2. + .5)*(y - source.h/2. + .5));
  384. float alpha = (1 - dist/max_dist);
  385. if(alpha < 0) alpha = 0;
  386. float v1 = get_pixel(source, x,y,k);
  387. float v2 = get_pixel(dest, dx+x,dy+y,k);
  388. float val = alpha*v1 + (1-alpha)*v2;
  389. set_pixel(dest, dx+x, dy+y, k, val);
  390. }
  391. }
  392. }
  393. }
  394. void embed_image(image source, image dest, int dx, int dy)
  395. {
  396. int x,y,k;
  397. for(k = 0; k < source.c; ++k){
  398. for(y = 0; y < source.h; ++y){
  399. for(x = 0; x < source.w; ++x){
  400. float val = get_pixel(source, x,y,k);
  401. set_pixel(dest, dx+x, dy+y, k, val);
  402. }
  403. }
  404. }
  405. }
  406. image collapse_image_layers(image source, int border)
  407. {
  408. int h = source.h;
  409. h = (h+border)*source.c - border;
  410. image dest = make_image(source.w, h, 1);
  411. int i;
  412. for(i = 0; i < source.c; ++i){
  413. image layer = get_image_layer(source, i);
  414. int h_offset = i*(source.h+border);
  415. embed_image(layer, dest, 0, h_offset);
  416. free_image(layer);
  417. }
  418. return dest;
  419. }
  420. void constrain_image(image im)
  421. {
  422. int i;
  423. for(i = 0; i < im.w*im.h*im.c; ++i){
  424. if(im.data[i] < 0) im.data[i] = 0;
  425. if(im.data[i] > 1) im.data[i] = 1;
  426. }
  427. }
  428. void normalize_image(image p)
  429. {
  430. int i;
  431. float min = 9999999;
  432. float max = -999999;
  433. for(i = 0; i < p.h*p.w*p.c; ++i){
  434. float v = p.data[i];
  435. if(v < min) min = v;
  436. if(v > max) max = v;
  437. }
  438. if(max - min < .000000001){
  439. min = 0;
  440. max = 1;
  441. }
  442. for(i = 0; i < p.c*p.w*p.h; ++i){
  443. p.data[i] = (p.data[i] - min)/(max-min);
  444. }
  445. }
  446. void normalize_image2(image p)
  447. {
  448. float *min = (float*)calloc(p.c, sizeof(float));
  449. float *max = (float*)calloc(p.c, sizeof(float));
  450. int i,j;
  451. for(i = 0; i < p.c; ++i) min[i] = max[i] = p.data[i*p.h*p.w];
  452. for(j = 0; j < p.c; ++j){
  453. for(i = 0; i < p.h*p.w; ++i){
  454. float v = p.data[i+j*p.h*p.w];
  455. if(v < min[j]) min[j] = v;
  456. if(v > max[j]) max[j] = v;
  457. }
  458. }
  459. for(i = 0; i < p.c; ++i){
  460. if(max[i] - min[i] < .000000001){
  461. min[i] = 0;
  462. max[i] = 1;
  463. }
  464. }
  465. for(j = 0; j < p.c; ++j){
  466. for(i = 0; i < p.w*p.h; ++i){
  467. p.data[i+j*p.h*p.w] = (p.data[i+j*p.h*p.w] - min[j])/(max[j]-min[j]);
  468. }
  469. }
  470. free(min);
  471. free(max);
  472. }
  473. void copy_image_into(image src, image dest)
  474. {
  475. memcpy(dest.data, src.data, src.h*src.w*src.c*sizeof(float));
  476. }
  477. image copy_image(image p)
  478. {
  479. image copy = p;
  480. copy.data = (float*)calloc(p.h*p.w*p.c, sizeof(float));
  481. memcpy(copy.data, p.data, p.h*p.w*p.c*sizeof(float));
  482. return copy;
  483. }
  484. void rgbgr_image(image im)
  485. {
  486. int i;
  487. for(i = 0; i < im.w*im.h; ++i){
  488. float swap = im.data[i];
  489. im.data[i] = im.data[i+im.w*im.h*2];
  490. im.data[i+im.w*im.h*2] = swap;
  491. }
  492. }
  493. #ifdef OPENCV
  494. void show_image_cv(image p, const char *name, IplImage *disp)
  495. {
  496. int x,y,k;
  497. if(p.c == 3) rgbgr_image(p);
  498. //normalize_image(copy);
  499. char buff[256];
  500. //sprintf(buff, "%s (%d)", name, windows);
  501. sprintf(buff, "%s", name);
  502. int step = disp->widthStep;
  503. cvNamedWindow(buff, CV_WINDOW_NORMAL);
  504. //cvMoveWindow(buff, 100*(windows%10) + 200*(windows/10), 100*(windows%10));
  505. ++windows;
  506. for(y = 0; y < p.h; ++y){
  507. for(x = 0; x < p.w; ++x){
  508. for(k= 0; k < p.c; ++k){
  509. disp->imageData[y*step + x*p.c + k] = (unsigned char)(get_pixel(p,x,y,k)*255);
  510. }
  511. }
  512. }
  513. if(0){
  514. int w = 448;
  515. int h = w*p.h/p.w;
  516. if(h > 1000){
  517. h = 1000;
  518. w = h*p.w/p.h;
  519. }
  520. IplImage *buffer = disp;
  521. disp = cvCreateImage(cvSize(w, h), buffer->depth, buffer->nChannels);
  522. cvResize(buffer, disp, CV_INTER_LINEAR);
  523. cvReleaseImage(&buffer);
  524. }
  525. cvShowImage(buff, disp);
  526. }
  527. #endif
  528. void show_image(image p, const char *name)
  529. {
  530. #ifdef OPENCV
  531. IplImage *disp = cvCreateImage(cvSize(p.w,p.h), IPL_DEPTH_8U, p.c);
  532. image copy = copy_image(p);
  533. constrain_image(copy);
  534. show_image_cv(copy, name, disp);
  535. free_image(copy);
  536. cvReleaseImage(&disp);
  537. #else
  538. fprintf(stderr, "Not compiled with OpenCV, saving to %s.png instead\n", name);
  539. save_image(p, name);
  540. #endif
  541. }
  542. #ifdef OPENCV
  543. void ipl_into_image(IplImage* src, image im)
  544. {
  545. unsigned char *data = (unsigned char *)src->imageData;
  546. int h = src->height;
  547. int w = src->width;
  548. int c = src->nChannels;
  549. int step = src->widthStep;
  550. int i, j, k;
  551. for(i = 0; i < h; ++i){
  552. for(k= 0; k < c; ++k){
  553. for(j = 0; j < w; ++j){
  554. im.data[k*w*h + i*w + j] = data[i*step + j*c + k]/255.;
  555. }
  556. }
  557. }
  558. }
  559. void image_into_ipl(image im, IplImage* dst){
  560. unsigned char *data = (unsigned char *)dst->imageData;
  561. int h = dst->height;
  562. int w = dst->width;
  563. int c = dst->nChannels;
  564. int step = dst->widthStep;
  565. int i, j, k;
  566. for (i = 0; i < h; ++i){
  567. for (k = 0; k < c; ++k){
  568. for (j = 0; j < w; ++j){
  569. data[i*step + j*c + k] = ((int)( 255. * im.data[k*w*h + i*w + j])) & 0xFF;
  570. };
  571. };
  572. };
  573. };
  574. image ipl_to_image(IplImage* src)
  575. {
  576. int h = src->height;
  577. int w = src->width;
  578. int c = src->nChannels;
  579. image out = make_image(w, h, c);
  580. ipl_into_image(src, out);
  581. return out;
  582. }
  583. image load_image_cv(char *filename, int channels)
  584. {
  585. IplImage* src = 0;
  586. int flag = -1;
  587. if (channels == 0) flag = -1;
  588. else if (channels == 1) flag = 0;
  589. else if (channels == 3) flag = 1;
  590. else {
  591. fprintf(stderr, "OpenCV can't force load with %d channels\n", channels);
  592. }
  593. if( (src = cvLoadImage(filename, flag)) == 0 )
  594. {
  595. fprintf(stderr, "Cannot load image \"%s\"\n", filename);
  596. char buff[256];
  597. sprintf(buff, "echo %s >> bad.list", filename);
  598. system(buff);
  599. return make_image(10,10,3);
  600. //exit(0);
  601. }
  602. image out = ipl_to_image(src);
  603. cvReleaseImage(&src);
  604. rgbgr_image(out);
  605. return out;
  606. }
  607. void flush_stream_buffer(CvCapture *cap, int n)
  608. {
  609. int i;
  610. for(i = 0; i < n; ++i) {
  611. cvQueryFrame(cap);
  612. }
  613. }
  614. image get_image_from_stream(CvCapture *cap)
  615. {
  616. IplImage* src = cvQueryFrame(cap);
  617. if (!src) return make_empty_image(0,0,0);
  618. image im = ipl_to_image(src);
  619. rgbgr_image(im);
  620. return im;
  621. }
  622. int fill_image_from_stream(CvCapture *cap, image im)
  623. {
  624. IplImage* src = cvQueryFrame(cap);
  625. if (!src) return 0;
  626. ipl_into_image(src, im);
  627. rgbgr_image(im);
  628. return 1;
  629. }
  630. void save_image_jpg(image p, const char *name)
  631. {
  632. image copy = copy_image(p);
  633. if(p.c == 3) rgbgr_image(copy);
  634. int x,y,k;
  635. char buff[256];
  636. sprintf(buff, "%s.jpg", name);
  637. IplImage *disp = cvCreateImage(cvSize(p.w,p.h), IPL_DEPTH_8U, p.c);
  638. int step = disp->widthStep;
  639. for(y = 0; y < p.h; ++y){
  640. for(x = 0; x < p.w; ++x){
  641. for(k= 0; k < p.c; ++k){
  642. disp->imageData[y*step + x*p.c + k] = (unsigned char)(get_pixel(copy,x,y,k)*255);
  643. }
  644. }
  645. }
  646. cvSaveImage(buff, disp,0);
  647. cvReleaseImage(&disp);
  648. free_image(copy);
  649. }
  650. #endif
  651. void save_image_png(image im, const char *name)
  652. {
  653. char buff[256];
  654. //sprintf(buff, "%s (%d)", name, windows);
  655. sprintf(buff, "%s.png", name);
  656. unsigned char *data = (unsigned char*)calloc(im.w*im.h*im.c, sizeof(char));
  657. int i,k;
  658. for(k = 0; k < im.c; ++k){
  659. for(i = 0; i < im.w*im.h; ++i){
  660. data[i*im.c+k] = (unsigned char) (255*im.data[i + k*im.w*im.h]);
  661. }
  662. }
  663. int success = stbi_write_png(buff, im.w, im.h, im.c, data, im.w*im.c);
  664. free(data);
  665. if(!success) fprintf(stderr, "Failed to write image %s\n", buff);
  666. }
  667. void save_image(image im, const char *name)
  668. {
  669. #ifdef OPENCV
  670. save_image_jpg(im, name);
  671. #else
  672. save_image_png(im, name);
  673. #endif
  674. }
  675. void show_image_layers(image p, char *name)
  676. {
  677. int i;
  678. char buff[256];
  679. for(i = 0; i < p.c; ++i){
  680. sprintf(buff, "%s - Layer %d", name, i);
  681. image layer = get_image_layer(p, i);
  682. show_image(layer, buff);
  683. free_image(layer);
  684. }
  685. }
  686. void show_image_collapsed(image p, char *name)
  687. {
  688. image c = collapse_image_layers(p, 1);
  689. show_image(c, name);
  690. free_image(c);
  691. }
  692. image make_empty_image(int w, int h, int c)
  693. {
  694. #ifndef EXT_CV
  695. image out;
  696. out.data = 0;
  697. out.h = h;
  698. out.w = w;
  699. out.c = c;
  700. #else
  701. image out;
  702. switch(c){
  703. case 1: out = image(h, w, CV_8UC1); break;
  704. case 2: out = image(h, w, CV_8UC2); break;
  705. case 3: out = image(h, w, CV_8UC3); break;
  706. case 4: out = image(h, w, CV_8UC4); break;
  707. };
  708. #endif
  709. return out;
  710. }
  711. image make_image(int w, int h, int c)
  712. {
  713. image out = make_empty_image(w,h,c);
  714. #ifndef EXT_CV
  715. out.data = (float*)calloc(h*w*c, sizeof(float));
  716. #endif
  717. return out;
  718. }
  719. image make_random_image(int w, int h, int c)
  720. {
  721. image out = make_empty_image(w,h,c);
  722. #ifndef EXT_CV
  723. out.data = (float*)calloc(h*w*c, sizeof(float));
  724. #endif
  725. int i;
  726. for(i = 0; i < w*h*c; ++i){
  727. #ifndef EXT_CV
  728. out.data[i] = (float)((rand_normal() * .25) + .5);
  729. #else
  730. out.data[i] = ((unsigned char)(rand_normal() * 255) & 0xFF);
  731. #endif
  732. }
  733. return out;
  734. }
  735. image float_to_image(int w, int h, int c, float *data)
  736. {
  737. image out = make_empty_image(w,h,c);
  738. #ifndef EXT_CV
  739. out.data = data;
  740. #else
  741. for (int i = 0; i < out.rows*out.cols*out.channels();i++)
  742. out.data[i] = ((unsigned char)(data[i] * 255) & 0xFF );
  743. #endif
  744. return out;
  745. }
  746. void place_image(image im, int w, int h, int dx, int dy, image canvas)
  747. {
  748. int x, y, c;
  749. #ifndef EXT_CV
  750. for(c = 0; c < im.c; ++c){
  751. #else
  752. for (c = 0; c < im.channels(); ++c){
  753. #endif
  754. for(y = 0; y < h; ++y){
  755. for(x = 0; x < w; ++x){
  756. #ifndef EXT_CV
  757. int rx = (int)((float)x / w) * im.w;
  758. int ry = (int)((float)y / h) * im.h;
  759. #else
  760. int rx = (int)((float)x / w) * im.cols;
  761. int ry = (int)((float)y / h) * im.rows;
  762. #endif
  763. float val = bilinear_interpolate( im, (float)rx, (float)ry, c );
  764. set_pixel(canvas, x + dx, y + dy, c, val);
  765. }
  766. }
  767. }
  768. }
  769. image center_crop_image(image im, int w, int h)
  770. {
  771. #ifndef EXT_CV
  772. int m = (im.w < im.h) ? im.w : im.h;
  773. image c = crop_image(im, (im.w - m) / 2, (im.h - m)/2, m, m);
  774. #else
  775. int m = (im.cols < im.rows) ? im.cols : im.rows;
  776. image c = crop_image(im, (im.cols - m) / 2, (im.rows - m) / 2, m, m);
  777. #endif
  778. image r = resize_image(c, w, h);
  779. free_image(c);
  780. return r;
  781. }
  782. image rotate_crop_image(image im, float rad, float s, int w, int h, float dx, float dy, float aspect)
  783. {
  784. int x, y, c;
  785. #ifndef EXT_CV
  786. float cx = (float)(im.w/2.);
  787. float cy = (float)(im.h/2.);
  788. image rot = make_image(w, h, im.c);
  789. for(c = 0; c < im.c; ++c){
  790. #else
  791. float cx = (float)(im.cols / 2.);
  792. float cy = (float)(im.rows / 2.);
  793. image rot = make_image(w, h, im.channels());
  794. for (c = 0; c < im.channels(); ++c){
  795. #endif
  796. for(y = 0; y < h; ++y){
  797. for(x = 0; x < w; ++x){
  798. float rx = (float)(cos(rad)*((x - w/2.)/s*aspect + dx/s*aspect) - sin(rad)*((y - h/2.)/s + dy/s) + cx);
  799. float ry = (float)(sin(rad)*((x - w/2.)/s*aspect + dx/s*aspect) + cos(rad)*((y - h/2.)/s + dy/s) + cy);
  800. float val = bilinear_interpolate(im, rx, ry, c);
  801. set_pixel(rot, x, y, c, val);
  802. }
  803. }
  804. }
  805. return rot;
  806. }
  807. image rotate_image(image im, float rad)
  808. {
  809. int x, y, c;
  810. #ifndef EXT_CV
  811. float cx = (float)( im.w/2.);
  812. float cy = (float)(im.h/2.);
  813. image rot = make_image(im.w, im.h, im.c);
  814. for(c = 0; c < im.c; ++c){
  815. for(y = 0; y < im.h; ++y){
  816. for(x = 0; x < im.w; ++x){
  817. #else
  818. float cx = (float)(im.cols / 2.);
  819. float cy = (float)(im.rows / 2.);
  820. image rot = make_image(im.cols, im.rows, im.channels());
  821. for (c = 0; c < im.channels(); ++c){
  822. for (y = 0; y < im.rows; ++y){
  823. for (x = 0; x < im.cols; ++x){
  824. #endif
  825. float rx = (float)(cos(rad)*(x-cx) - sin(rad)*(y-cy) + cx);
  826. float ry = (float)(sin(rad)*(x-cx) + cos(rad)*(y-cy) + cy);
  827. float val = bilinear_interpolate(im, rx, ry, c);
  828. set_pixel(rot, x, y, c, val);
  829. }
  830. }
  831. }
  832. return rot;
  833. }
  834. void fill_image(image m, float s)
  835. {
  836. int i;
  837. for(i = 0; i < m.h*m.w*m.c; ++i) m.data[i] = s;
  838. }
  839. void translate_image(image m, float s)
  840. {
  841. int i;
  842. for(i = 0; i < m.h*m.w*m.c; ++i) m.data[i] += s;
  843. }
  844. void scale_image(image m, float s)
  845. {
  846. int i;
  847. for(i = 0; i < m.h*m.w*m.c; ++i) m.data[i] *= s;
  848. }
  849. image crop_image(image im, int dx, int dy, int w, int h)
  850. {
  851. image cropped = make_image(w, h, im.c);
  852. int i, j, k;
  853. for(k = 0; k < im.c; ++k){
  854. for(j = 0; j < h; ++j){
  855. for(i = 0; i < w; ++i){
  856. int r = j + dy;
  857. int c = i + dx;
  858. float val = 0;
  859. r = constrain_int(r, 0, im.h-1);
  860. c = constrain_int(c, 0, im.w-1);
  861. val = get_pixel(im, c, r, k);
  862. set_pixel(cropped, i, j, k, val);
  863. }
  864. }
  865. }
  866. return cropped;
  867. }
  868. int best_3d_shift_r(image a, image b, int min, int max)
  869. {
  870. if(min == max) return min;
  871. int mid = (int)floor((min + max) / 2.);
  872. image c1 = crop_image(b, 0, mid, b.w, b.h);
  873. image c2 = crop_image(b, 0, mid+1, b.w, b.h);
  874. float d1 = dist_array(c1.data, a.data, a.w*a.h*a.c, 10);
  875. float d2 = dist_array(c2.data, a.data, a.w*a.h*a.c, 10);
  876. free_image(c1);
  877. free_image(c2);
  878. if(d1 < d2) return best_3d_shift_r(a, b, min, mid);
  879. else return best_3d_shift_r(a, b, mid+1, max);
  880. }
  881. int best_3d_shift(image a, image b, int min, int max)
  882. {
  883. int i;
  884. int best = 0;
  885. float best_distance = FLT_MAX;
  886. for(i = min; i <= max; i += 2){
  887. image c = crop_image(b, 0, i, b.w, b.h);
  888. float d = dist_array(c.data, a.data, a.w*a.h*a.c, 100);
  889. if(d < best_distance){
  890. best_distance = d;
  891. best = i;
  892. }
  893. printf("%d %f\n", i, d);
  894. free_image(c);
  895. }
  896. return best;
  897. }
  898. void composite_3d(char *f1, char *f2, char *out, int delta)
  899. {
  900. if(!out) out = "out";
  901. image a = load_image(f1, 0,0,0);
  902. image b = load_image(f2, 0,0,0);
  903. int shift = best_3d_shift_r(a, b, -a.h/100, a.h/100);
  904. image c1 = crop_image(b, 10, shift, b.w, b.h);
  905. float d1 = dist_array(c1.data, a.data, a.w*a.h*a.c, 100);
  906. image c2 = crop_image(b, -10, shift, b.w, b.h);
  907. float d2 = dist_array(c2.data, a.data, a.w*a.h*a.c, 100);
  908. if(d2 < d1 && 0){
  909. image swap = a;
  910. a = b;
  911. b = swap;
  912. shift = -shift;
  913. printf("swapped, %d\n", shift);
  914. }
  915. else{
  916. printf("%d\n", shift);
  917. }
  918. image c = crop_image(b, delta, shift, a.w, a.h);
  919. int i;
  920. for(i = 0; i < c.w*c.h; ++i){
  921. c.data[i] = a.data[i];
  922. }
  923. #ifdef OPENCV
  924. save_image_jpg(c, out);
  925. #else
  926. save_image(c, out);
  927. #endif
  928. }
  929. void letterbox_image_into(image im, int w, int h, image boxed)
  930. {
  931. int new_w = im.w;
  932. int new_h = im.h;
  933. if (((float)w/im.w) < ((float)h/im.h)) {
  934. new_w = w;
  935. new_h = (im.h * w)/im.w;
  936. } else {
  937. new_h = h;
  938. new_w = (im.w * h)/im.h;
  939. }
  940. image resized = resize_image(im, new_w, new_h);
  941. embed_image(resized, boxed, (w-new_w)/2, (h-new_h)/2);
  942. free_image(resized);
  943. }
  944. image letterbox_image(image im, int w, int h)
  945. {
  946. int new_w = im.w;
  947. int new_h = im.h;
  948. if (((float)w/im.w) < ((float)h/im.h)) {
  949. new_w = w;
  950. new_h = (im.h * w)/im.w;
  951. } else {
  952. new_h = h;
  953. new_w = (im.w * h)/im.h;
  954. }
  955. image resized = resize_image(im, new_w, new_h);
  956. image boxed = make_image(w, h, im.c);
  957. fill_image(boxed, .5);
  958. //int i;
  959. //for(i = 0; i < boxed.w*boxed.h*boxed.c; ++i) boxed.data[i] = 0;
  960. embed_image(resized, boxed, (w-new_w)/2, (h-new_h)/2);
  961. free_image(resized);
  962. return boxed;
  963. }
  964. image resize_max(image im, int max)
  965. {
  966. int w = im.w;
  967. int h = im.h;
  968. if(w > h){
  969. h = (h * max) / w;
  970. w = max;
  971. } else {
  972. w = (w * max) / h;
  973. h = max;
  974. }
  975. if(w == im.w && h == im.h) return im;
  976. image resized = resize_image(im, w, h);
  977. return resized;
  978. }
  979. image resize_min(image im, int min)
  980. {
  981. int w = im.w;
  982. int h = im.h;
  983. if(w < h){
  984. h = (h * min) / w;
  985. w = min;
  986. } else {
  987. w = (w * min) / h;
  988. h = min;
  989. }
  990. if(w == im.w && h == im.h) return im;
  991. image resized = resize_image(im, w, h);
  992. return resized;
  993. }
  994. image random_crop_image(image im, int w, int h)
  995. {
  996. int dx = rand_int(0, im.w - w);
  997. int dy = rand_int(0, im.h - h);
  998. image crop = crop_image(im, dx, dy, w, h);
  999. return crop;
  1000. }
  1001. augment_args random_augment_args(image im, float angle, float aspect, int low, int high, int w, int h)
  1002. {
  1003. augment_args a = {0};
  1004. aspect = rand_scale(aspect);
  1005. int r = rand_int(low, high);
  1006. int min = (int)((im.h < im.w*aspect) ? im.h : im.w*aspect);
  1007. float scale = (float)r / min;
  1008. float rad = (float)(rand_uniform(-angle, angle) * TWO_PI / 360.);
  1009. float dx = (float)((im.w*scale/aspect - w) / 2.);
  1010. float dy = (float)((im.h*scale - w) / 2.);
  1011. //if(dx < 0) dx = 0;
  1012. //if(dy < 0) dy = 0;
  1013. dx = rand_uniform(-dx, dx);
  1014. dy = rand_uniform(-dy, dy);
  1015. a.rad = rad;
  1016. a.scale = scale;
  1017. a.w = w;
  1018. a.h = h;
  1019. a.dx = dx;
  1020. a.dy = dy;
  1021. a.aspect = aspect;
  1022. return a;
  1023. }
  1024. image random_augment_image(image im, float angle, float aspect, int low, int high, int w, int h)
  1025. {
  1026. augment_args a = random_augment_args(im, angle, aspect, low, high, w, h);
  1027. image crop = rotate_crop_image(im, a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect);
  1028. return crop;
  1029. }
  1030. float three_way_max(float a, float b, float c)
  1031. {
  1032. return (a > b) ? ( (a > c) ? a : c) : ( (b > c) ? b : c) ;
  1033. }
  1034. float three_way_min(float a, float b, float c)
  1035. {
  1036. return (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c) ;
  1037. }
  1038. void yuv_to_rgb(image im)
  1039. {
  1040. assert(im.c == 3);
  1041. int i, j;
  1042. float r, g, b;
  1043. float y, u, v;
  1044. for(j = 0; j < im.h; ++j){
  1045. for(i = 0; i < im.w; ++i){
  1046. y = get_pixel(im, i , j, 0);
  1047. u = get_pixel(im, i , j, 1);
  1048. v = get_pixel(im, i , j, 2);
  1049. r = (float)(y + 1.13983*v);
  1050. g = (float)(y + -.39465*u + -.58060*v);
  1051. b = (float)(y + 2.03211*u);
  1052. set_pixel(im, i, j, 0, r);
  1053. set_pixel(im, i, j, 1, g);
  1054. set_pixel(im, i, j, 2, b);
  1055. }
  1056. }
  1057. }
  1058. void rgb_to_yuv(image im)
  1059. {
  1060. assert(im.c == 3);
  1061. int i, j;
  1062. float r, g, b;
  1063. float y, u, v;
  1064. for(j = 0; j < im.h; ++j){
  1065. for(i = 0; i < im.w; ++i){
  1066. r = get_pixel(im, i , j, 0);
  1067. g = get_pixel(im, i , j, 1);
  1068. b = get_pixel(im, i , j, 2);
  1069. y = (float)(.299*r + .587*g + .114*b);
  1070. u = (float)(-.14713*r + -.28886*g + .436*b);
  1071. v = (float)(.615*r + -.51499*g + -.10001*b);
  1072. set_pixel(im, i, j, 0, y);
  1073. set_pixel(im, i, j, 1, u);
  1074. set_pixel(im, i, j, 2, v);
  1075. }
  1076. }
  1077. }
  1078. // http://www.cs.rit.edu/~ncs/color/t_convert.html
  1079. void rgb_to_hsv(image im)
  1080. {
  1081. assert(im.c == 3);
  1082. int i, j;
  1083. float r, g, b;
  1084. float h, s, v;
  1085. for(j = 0; j < im.h; ++j){
  1086. for(i = 0; i < im.w; ++i){
  1087. r = get_pixel(im, i , j, 0);
  1088. g = get_pixel(im, i , j, 1);
  1089. b = get_pixel(im, i , j, 2);
  1090. float max = three_way_max(r,g,b);
  1091. float min = three_way_min(r,g,b);
  1092. float delta = max - min;
  1093. v = max;
  1094. if(max == 0){
  1095. s = 0;
  1096. h = 0;
  1097. }else{
  1098. s = delta/max;
  1099. if(r == max){
  1100. h = (g - b) / delta;
  1101. } else if (g == max) {
  1102. h = 2 + (b - r) / delta;
  1103. } else {
  1104. h = 4 + (r - g) / delta;
  1105. }
  1106. if (h < 0) h += 6;
  1107. h = (float)(h/6.);
  1108. }
  1109. set_pixel(im, i, j, 0, h);
  1110. set_pixel(im, i, j, 1, s);
  1111. set_pixel(im, i, j, 2, v);
  1112. }
  1113. }
  1114. }
  1115. void hsv_to_rgb(image im)
  1116. {
  1117. assert(im.c == 3);
  1118. int i, j;
  1119. float r, g, b;
  1120. float h, s, v;
  1121. float f, p, q, t;
  1122. for(j = 0; j < im.h; ++j){
  1123. for(i = 0; i < im.w; ++i){
  1124. h = 6 * get_pixel(im, i , j, 0);
  1125. s = get_pixel(im, i , j, 1);
  1126. v = get_pixel(im, i , j, 2);
  1127. if (s == 0) {
  1128. r = g = b = v;
  1129. } else {
  1130. int index = (int)floor(h);
  1131. f = h - index;
  1132. p = v*(1-s);
  1133. q = v*(1-s*f);
  1134. t = v*(1-s*(1-f));
  1135. if(index == 0){
  1136. r = v; g = t; b = p;
  1137. } else if(index == 1){
  1138. r = q; g = v; b = p;
  1139. } else if(index == 2){
  1140. r = p; g = v; b = t;
  1141. } else if(index == 3){
  1142. r = p; g = q; b = v;
  1143. } else if(index == 4){
  1144. r = t; g = p; b = v;
  1145. } else {
  1146. r = v; g = p; b = q;
  1147. }
  1148. }
  1149. set_pixel(im, i, j, 0, r);
  1150. set_pixel(im, i, j, 1, g);
  1151. set_pixel(im, i, j, 2, b);
  1152. }
  1153. }
  1154. }
  1155. void grayscale_image_3c(image im)
  1156. {
  1157. assert(im.c == 3);
  1158. int i, j, k;
  1159. float scale[] = {(float)0.299, (float)0.587, 0.114f};
  1160. for(j = 0; j < im.h; ++j){
  1161. for(i = 0; i < im.w; ++i){
  1162. float val = 0;
  1163. for(k = 0; k < 3; ++k){
  1164. val += scale[k]*get_pixel(im, i, j, k);
  1165. }
  1166. im.data[0*im.h*im.w + im.w*j + i] = val;
  1167. im.data[1*im.h*im.w + im.w*j + i] = val;
  1168. im.data[2*im.h*im.w + im.w*j + i] = val;
  1169. }
  1170. }
  1171. }
  1172. image grayscale_image(image im)
  1173. {
  1174. assert(im.c == 3);
  1175. int i, j, k;
  1176. image gray = make_image(im.w, im.h, 1);
  1177. float scale[] = {0.299f, 0.587f, 0.114f};
  1178. for(k = 0; k < im.c; ++k){
  1179. for(j = 0; j < im.h; ++j){
  1180. for(i = 0; i < im.w; ++i){
  1181. gray.data[i+im.w*j] += scale[k]*get_pixel(im, i, j, k);
  1182. }
  1183. }
  1184. }
  1185. return gray;
  1186. }
  1187. image threshold_image(image im, float thresh)
  1188. {
  1189. int i;
  1190. image t = make_image(im.w, im.h, im.c);
  1191. for(i = 0; i < im.w*im.h*im.c; ++i){
  1192. t.data[i] = (float)(im.data[i]>thresh ? 1 : 0);
  1193. }
  1194. return t;
  1195. }
  1196. image blend_image(image fore, image back, float alpha)
  1197. {
  1198. assert(fore.w == back.w && fore.h == back.h && fore.c == back.c);
  1199. image blend = make_image(fore.w, fore.h, fore.c);
  1200. int i, j, k;
  1201. for(k = 0; k < fore.c; ++k){
  1202. for(j = 0; j < fore.h; ++j){
  1203. for(i = 0; i < fore.w; ++i){
  1204. float val = alpha * get_pixel(fore, i, j, k) +
  1205. (1 - alpha)* get_pixel(back, i, j, k);
  1206. set_pixel(blend, i, j, k, val);
  1207. }
  1208. }
  1209. }
  1210. return blend;
  1211. }
  1212. void scale_image_channel(image im, int c, float v)
  1213. {
  1214. int i, j;
  1215. for(j = 0; j < im.h; ++j){
  1216. for(i = 0; i < im.w; ++i){
  1217. float pix = get_pixel(im, i, j, c);
  1218. pix = pix*v;
  1219. set_pixel(im, i, j, c, pix);
  1220. }
  1221. }
  1222. }
  1223. void translate_image_channel(image im, int c, float v)
  1224. {
  1225. int i, j;
  1226. for(j = 0; j < im.h; ++j){
  1227. for(i = 0; i < im.w; ++i){
  1228. float pix = get_pixel(im, i, j, c);
  1229. pix = pix+v;
  1230. set_pixel(im, i, j, c, pix);
  1231. }
  1232. }
  1233. }
  1234. image binarize_image(image im)
  1235. {
  1236. image c = copy_image(im);
  1237. int i;
  1238. for(i = 0; i < im.w * im.h * im.c; ++i){
  1239. if(c.data[i] > .5) c.data[i] = 1;
  1240. else c.data[i] = 0;
  1241. }
  1242. return c;
  1243. }
  1244. void saturate_image(image im, float sat)
  1245. {
  1246. rgb_to_hsv(im);
  1247. scale_image_channel(im, 1, sat);
  1248. hsv_to_rgb(im);
  1249. constrain_image(im);
  1250. }
  1251. void hue_image(image im, float hue)
  1252. {
  1253. rgb_to_hsv(im);
  1254. int i;
  1255. for(i = 0; i < im.w*im.h; ++i){
  1256. im.data[i] = im.data[i] + hue;
  1257. if (im.data[i] > 1) im.data[i] -= 1;
  1258. if (im.data[i] < 0) im.data[i] += 1;
  1259. }
  1260. hsv_to_rgb(im);
  1261. constrain_image(im);
  1262. }
  1263. void exposure_image(image im, float sat)
  1264. {
  1265. rgb_to_hsv(im);
  1266. scale_image_channel(im, 2, sat);
  1267. hsv_to_rgb(im);
  1268. constrain_image(im);
  1269. }
  1270. void distort_image(image im, float hue, float sat, float val)
  1271. {
  1272. rgb_to_hsv(im);
  1273. scale_image_channel(im, 1, sat);
  1274. scale_image_channel(im, 2, val);
  1275. int i;
  1276. for(i = 0; i < im.w*im.h; ++i){
  1277. im.data[i] = im.data[i] + hue;
  1278. if (im.data[i] > 1) im.data[i] -= 1;
  1279. if (im.data[i] < 0) im.data[i] += 1;
  1280. }
  1281. hsv_to_rgb(im);
  1282. constrain_image(im);
  1283. }
  1284. void random_distort_image(image im, float hue, float saturation, float exposure)
  1285. {
  1286. float dhue = rand_uniform(-hue, hue);
  1287. float dsat = rand_scale(saturation);
  1288. float dexp = rand_scale(exposure);
  1289. distort_image(im, dhue, dsat, dexp);
  1290. }
  1291. void saturate_exposure_image(image im, float sat, float exposure)
  1292. {
  1293. rgb_to_hsv(im);
  1294. scale_image_channel(im, 1, sat);
  1295. scale_image_channel(im, 2, exposure);
  1296. hsv_to_rgb(im);
  1297. constrain_image(im);
  1298. }
  1299. float bilinear_interpolate(image im, float x, float y, int c)
  1300. {
  1301. int ix = (int) floorf(x);
  1302. int iy = (int) floorf(y);
  1303. float dx = x - ix;
  1304. float dy = y - iy;
  1305. float val = (1-dy) * (1-dx) * get_pixel_extend(im, ix, iy, c) +
  1306. dy * (1-dx) * get_pixel_extend(im, ix, iy+1, c) +
  1307. (1-dy) * dx * get_pixel_extend(im, ix+1, iy, c) +
  1308. dy * dx * get_pixel_extend(im, ix+1, iy+1, c);
  1309. return val;
  1310. }
  1311. image resize_image(image im, int w, int h)
  1312. {
  1313. image resized = make_image(w, h, im.c);
  1314. image part = make_image(w, im.h, im.c);
  1315. int r, c, k;
  1316. float w_scale = (float)(im.w - 1) / (w - 1);
  1317. float h_scale = (float)(im.h - 1) / (h - 1);
  1318. for(k = 0; k < im.c; ++k){
  1319. for(r = 0; r < im.h; ++r){
  1320. for(c = 0; c < w; ++c){
  1321. float val = 0;
  1322. if(c == w-1 || im.w == 1){
  1323. val = get_pixel(im, im.w-1, r, k);
  1324. } else {
  1325. float sx = c*w_scale;
  1326. int ix = (int) sx;
  1327. float dx = sx - ix;
  1328. val = (1 - dx) * get_pixel(im, ix, r, k) + dx * get_pixel(im, ix+1, r, k);
  1329. }
  1330. set_pixel(part, c, r, k, val);
  1331. }
  1332. }
  1333. }
  1334. for(k = 0; k < im.c; ++k){
  1335. for(r = 0; r < h; ++r){
  1336. float sy = r*h_scale;
  1337. int iy = (int) sy;
  1338. float dy = sy - iy;
  1339. for(c = 0; c < w; ++c){
  1340. float val = (1-dy) * get_pixel(part, c, iy, k);
  1341. set_pixel(resized, c, r, k, val);
  1342. }
  1343. if(r == h-1 || im.h == 1) continue;
  1344. for(c = 0; c < w; ++c){
  1345. float val = dy * get_pixel(part, c, iy+1, k);
  1346. add_pixel(resized, c, r, k, val);
  1347. }
  1348. }
  1349. }
  1350. free_image(part);
  1351. return resized;
  1352. }
  1353. void test_resize(char *filename)
  1354. {
  1355. image im = load_image(filename, 0,0, 3);
  1356. float mag = mag_array(im.data, im.w*im.h*im.c);
  1357. printf("L2 Norm: %f\n", mag);
  1358. image gray = grayscale_image(im);
  1359. image c1 = copy_image(im);
  1360. image c2 = copy_image(im);
  1361. image c3 = copy_image(im);
  1362. image c4 = copy_image(im);
  1363. distort_image(c1, .1f, 1.5f, 1.5f);
  1364. distort_image(c2, -.1f, .66666f, .66666f);
  1365. distort_image(c3, .1f, 1.5f, .66666f);
  1366. distort_image(c4, .1f, .66666f, 1.5f);
  1367. show_image(im, "Original");
  1368. show_image(gray, "Gray");
  1369. show_image(c1, "C1");
  1370. show_image(c2, "C2");
  1371. show_image(c3, "C3");
  1372. show_image(c4, "C4");
  1373. #ifdef OPENCV
  1374. while(1){
  1375. image aug = random_augment_image(im, 0, .75, 320, 448, 320, 320);
  1376. show_image(aug, "aug");
  1377. free_image(aug);
  1378. float exposure = 1.15;
  1379. float saturation = 1.15;
  1380. float hue = .05;
  1381. image c = copy_image(im);
  1382. float dexp = rand_scale(exposure);
  1383. float dsat = rand_scale(saturation);
  1384. float dhue = rand_uniform(-hue, hue);
  1385. distort_image(c, dhue, dsat, dexp);
  1386. show_image(c, "rand");
  1387. printf("%f %f %f\n", dhue, dsat, dexp);
  1388. free_image(c);
  1389. cvWaitKey(0);
  1390. }
  1391. #endif
  1392. }
  1393. image load_image_stb(char *filename, int channels)
  1394. {
  1395. int w, h, c;
  1396. unsigned char *data = stbi_load(filename, &w, &h, &c, channels);
  1397. if (!data) {
  1398. fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n", filename, stbi_failure_reason());
  1399. exit(0);
  1400. }
  1401. if(channels) c = channels;
  1402. int i,j,k;
  1403. image im = make_image(w, h, c);
  1404. for(k = 0; k < c; ++k){
  1405. for(j = 0; j < h; ++j){
  1406. for(i = 0; i < w; ++i){
  1407. int dst_index = i + w*j + w*h*k;
  1408. int src_index = k + c*i + c*w*j;
  1409. im.data[dst_index] = (float)(data[src_index]/255.);
  1410. }
  1411. }
  1412. }
  1413. free(data);
  1414. return im;
  1415. }
  1416. image load_image(char *filename, int w, int h, int c)
  1417. {
  1418. #ifdef OPENCV
  1419. image out = load_image_cv(filename, c);
  1420. #else
  1421. image out = load_image_stb(filename, c);
  1422. #endif
  1423. if((h && w) && (h != out.h || w != out.w)){
  1424. image resized = resize_image(out, w, h);
  1425. free_image(out);
  1426. out = resized;
  1427. }
  1428. return out;
  1429. }
  1430. image load_image_color(char *filename, int w, int h)
  1431. {
  1432. return load_image(filename, w, h, 3);
  1433. }
  1434. image get_image_layer(image m, int l)
  1435. {
  1436. image out = make_image(m.w, m.h, 1);
  1437. int i;
  1438. for(i = 0; i < m.h*m.w; ++i){
  1439. out.data[i] = m.data[i+l*m.h*m.w];
  1440. }
  1441. return out;
  1442. }
  1443. float get_pixel(image m, int x, int y, int c)
  1444. {
  1445. assert(x < m.w && y < m.h && c < m.c);
  1446. return m.data[c*m.h*m.w + y*m.w + x];
  1447. }
  1448. float get_pixel_extend(image m, int x, int y, int c)
  1449. {
  1450. if(x < 0 || x >= m.w || y < 0 || y >= m.h) return 0;
  1451. /*
  1452. if(x < 0) x = 0;
  1453. if(x >= m.w) x = m.w-1;
  1454. if(y < 0) y = 0;
  1455. if(y >= m.h) y = m.h-1;
  1456. */
  1457. if(c < 0 || c >= m.c) return 0;
  1458. return get_pixel(m, x, y, c);
  1459. }
  1460. void set_pixel(image m, int x, int y, int c, float val)
  1461. {
  1462. if (x < 0 || y < 0 || c < 0 || x >= m.w || y >= m.h || c >= m.c) return;
  1463. assert(x < m.w && y < m.h && c < m.c);
  1464. m.data[c*m.h*m.w + y*m.w + x] = val;
  1465. }
  1466. void add_pixel(image m, int x, int y, int c, float val)
  1467. {
  1468. assert(x < m.w && y < m.h && c < m.c);
  1469. m.data[c*m.h*m.w + y*m.w + x] += val;
  1470. }
  1471. void print_image(image m)
  1472. {
  1473. int i, j, k;
  1474. for(i =0 ; i < m.c; ++i){
  1475. for(j =0 ; j < m.h; ++j){
  1476. for(k = 0; k < m.w; ++k){
  1477. printf("%.2lf, ", m.data[i*m.h*m.w + j*m.w + k]);
  1478. if(k > 30) break;
  1479. }
  1480. printf("\n");
  1481. if(j > 30) break;
  1482. }
  1483. printf("\n");
  1484. }
  1485. printf("\n");
  1486. }
  1487. image collapse_images_vert(image *ims, int n)
  1488. {
  1489. int color = 1;
  1490. int border = 1;
  1491. int h,w,c;
  1492. w = ims[0].w;
  1493. h = (ims[0].h + border) * n - border;
  1494. c = ims[0].c;
  1495. if(c != 3 || !color){
  1496. w = (w+border)*c - border;
  1497. c = 1;
  1498. }
  1499. image filters = make_image(w, h, c);
  1500. int i,j;
  1501. for(i = 0; i < n; ++i){
  1502. int h_offset = i*(ims[0].h+border);
  1503. image copy = copy_image(ims[i]);
  1504. //normalize_image(copy);
  1505. if(c == 3 && color){
  1506. embed_image(copy, filters, 0, h_offset);
  1507. }
  1508. else{
  1509. for(j = 0; j < copy.c; ++j){
  1510. int w_offset = j*(ims[0].w+border);
  1511. image layer = get_image_layer(copy, j);
  1512. embed_image(layer, filters, w_offset, h_offset);
  1513. free_image(layer);
  1514. }
  1515. }
  1516. free_image(copy);
  1517. }
  1518. return filters;
  1519. }
  1520. image collapse_images_horz(image *ims, int n)
  1521. {
  1522. int color = 1;
  1523. int border = 1;
  1524. int h,w,c;
  1525. int size = ims[0].h;
  1526. h = size;
  1527. w = (ims[0].w + border) * n - border;
  1528. c = ims[0].c;
  1529. if(c != 3 || !color){
  1530. h = (h+border)*c - border;
  1531. c = 1;
  1532. }
  1533. image filters = make_image(w, h, c);
  1534. int i,j;
  1535. for(i = 0; i < n; ++i){
  1536. int w_offset = i*(size+border);
  1537. image copy = copy_image(ims[i]);
  1538. //normalize_image(copy);
  1539. if(c == 3 && color){
  1540. embed_image(copy, filters, w_offset, 0);
  1541. }
  1542. else{
  1543. for(j = 0; j < copy.c; ++j){
  1544. int h_offset = j*(size+border);
  1545. image layer = get_image_layer(copy, j);
  1546. embed_image(layer, filters, w_offset, h_offset);
  1547. free_image(layer);
  1548. }
  1549. }
  1550. free_image(copy);
  1551. }
  1552. return filters;
  1553. }
  1554. void show_image_normalized(image im, const char *name)
  1555. {
  1556. image c = copy_image(im);
  1557. normalize_image(c);
  1558. show_image(c, name);
  1559. free_image(c);
  1560. }
  1561. void show_images(image *ims, int n, char *window)
  1562. {
  1563. image m = collapse_images_vert(ims, n);
  1564. /*
  1565. int w = 448;
  1566. int h = ((float)m.h/m.w) * 448;
  1567. if(h > 896){
  1568. h = 896;
  1569. w = ((float)m.w/m.h) * 896;
  1570. }
  1571. image sized = resize_image(m, w, h);
  1572. */
  1573. normalize_image(m);
  1574. save_image(m, window);
  1575. show_image(m, window);
  1576. free_image(m);
  1577. }
  1578. void free_image(image m)
  1579. {
  1580. if(m.data){
  1581. free(m.data);
  1582. }
  1583. }
  1584. };