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.

1536 lines
36 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2017. All Rights Reserved.
  4. // Version 2.0 9/14/2017
  5. // Last modification: 9/14/2017
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: basicImage.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Header file of basicImage.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "basicImage.h"
  16. //#include "utils.h"
  17. //#include "blas.h"
  18. //#include "dn_cuda.h"
  19. #define STB_IMAGE_IMPLEMENTATION
  20. #include "../../media/pictures/stb_image.h"
  21. #define STB_IMAGE_WRITE_IMPLEMENTATION
  22. #include "../../media/pictures/stb_image_write.h"
  23. const uint16_t cortexCOLORS[6][3] = {
  24. { 1, 0, 1 },
  25. { 0, 0, 1 },
  26. { 0, 1, 1 },
  27. { 0, 1, 0 },
  28. { 1, 1, 0 },
  29. { 1, 0, 0 }
  30. };
  31. using namespace cv;
  32. cortexImage::cortexImage(){
  33. h = 0;
  34. w = 0;
  35. c = 0;
  36. step = 0;
  37. data = NULL;
  38. };
  39. cortexImage::cortexImage(IplImage* src){
  40. if( src !=NULL ) {
  41. uchar *ipldata = (uchar *)src->imageData;
  42. h = src->height;
  43. w = src->width;
  44. c = src->nChannels;
  45. step = src->widthStep;
  46. data = (float*)calloc(h*w*c, sizeof(float));
  47. size = h*w*c;
  48. if( data != NULL ) {
  49. int i, j, k;
  50. for(i = 0; i < h; ++i){
  51. for(k= 0; k < c; ++k){
  52. for(j = 0; j < w; ++j){
  53. data[k*w*h + i*w + j] = ((float)ipldata[i*step + j*c + k]) / 255.f;
  54. };
  55. };
  56. };
  57. } else {
  58. h = 0;
  59. w = 0;
  60. c = 0;
  61. size = 0;
  62. step = 0;
  63. };
  64. } else {
  65. h = 0;
  66. w = 0;
  67. c = 0;
  68. size = 0;
  69. step = 0;
  70. };
  71. };
  72. cortexImage::cortexImage(cv::Mat* src){
  73. if( src!=NULL ) {
  74. h= src->rows;
  75. w= src->cols;
  76. c= src->channels();
  77. step = w*c;
  78. size = h*w*c;
  79. data = (float*)calloc(h*w*c, sizeof(float));
  80. if( data != NULL ) {
  81. int i, j, k;
  82. for(i = 0; i < h; ++i){
  83. for(k= 0; k < c; ++k){
  84. for(j = 0; j < w; ++j){
  85. data[k*w*h + i*w + j] = ((float)src->data[i*step + j*c + k])/255.f;
  86. };
  87. };
  88. };
  89. } else {
  90. h=0;
  91. w=0;
  92. c=0;
  93. size = 0;
  94. step = 0;
  95. };
  96. } else {
  97. h=0;
  98. w=0;
  99. c=0;
  100. size = 0;
  101. step = 0;
  102. };
  103. };
  104. bool cortexImage::fillFromMat(cv::Mat* src){
  105. if (src != NULL && src!= NULL) {
  106. if ( h == src->rows &&
  107. w == src->cols &&
  108. c == src->channels()){
  109. int i, j, k;
  110. for (i = 0; i < h; ++i){
  111. for (k = 0; k < c; ++k){
  112. for (j = 0; j < w; ++j){
  113. data[k*w*h + i*w + j] = ((float)src->data[i*step + j*c + k]) / 255.f;
  114. };
  115. };
  116. };
  117. };
  118. };
  119. return false;
  120. };
  121. cortexImage::cortexImage( const cortexImage &CC ){
  122. h=CC.h;
  123. w=CC.w;
  124. c=CC.c;
  125. step=CC.step;
  126. size = CC.size;
  127. data = (float*)calloc(h*w*c, sizeof(float));
  128. if( data != NULL ) {
  129. memcpy( data, CC.data, h*w*c*sizeof(float));
  130. } else {
  131. h=0;
  132. w=0;
  133. c=0;
  134. size = 0;
  135. step = 0;
  136. };
  137. };
  138. cortexImage::cortexImage( const cortexImage *CC ){
  139. h = CC->h;
  140. w = CC->w;
  141. c = CC->c;
  142. step = CC->step;
  143. size = CC->size;
  144. data = (float*)calloc(h*w*c, sizeof(float));
  145. if (data != NULL) {
  146. memcpy(data, CC->data, h*w*c*sizeof(float));
  147. } else {
  148. h = 0;
  149. w = 0;
  150. c = 0;
  151. step = 0;
  152. size = 0;
  153. };
  154. };
  155. /*
  156. cortexImage::cortexImage( cortexImage &CC ){
  157. h = CC.h;
  158. w = CC.w;
  159. c = CC.c;
  160. step = CC.step;
  161. data = (float*)calloc(h*w*c, sizeof(float));
  162. if (data != NULL) {
  163. memcpy(data, CC.data, h*w*c*sizeof(float));
  164. } else {
  165. h = 0;
  166. w = 0;
  167. c = 0;
  168. step = 0;
  169. };
  170. };
  171. cortexImage::cortexImage( cortexImage *CC ){
  172. h = CC->h;
  173. w = CC->w;
  174. c = CC->c;
  175. step = CC->step;
  176. data = (float*)calloc(h*w*c, sizeof(float));
  177. if (data != NULL) {
  178. memcpy(data, CC->data, h*w*c*sizeof(float));
  179. } else {
  180. h = 0;
  181. w = 0;
  182. c = 0;
  183. step = 0;
  184. };
  185. };
  186. */
  187. cortexImage::~cortexImage(){
  188. freeData();
  189. };
  190. float cortexImage::getColor(uint32_t c, uint32_t x, uint32_t max){
  191. float ratio = ((float)x/max)*5;
  192. int i = (int)floor(ratio);
  193. int j = (int)ceil(ratio);
  194. ratio -= i;
  195. float r = ((1.f - ratio)* cortexCOLORS[i][c])+ ( ratio * cortexCOLORS[j][c]);
  196. return r;
  197. };
  198. void cortexImage::toMat(cv::Mat &dst){
  199. int mW, mH;
  200. mW = (w >= dst.cols) ? dst.cols : w;
  201. mH = (h >= dst.rows) ? dst.rows : h;
  202. int index = 0;
  203. for (int y = 0; y < mW; y++){
  204. for (int x = 0; x < mW*dst.channels(); x++){
  205. index = x + y*w*c;
  206. if(dst.channels()==1){
  207. uchar& BGR = dst.at<uchar>(y, x);
  208. BGR = (uchar)(((uint32_t)(255.f*data[index])) & 0xFF);
  209. } else if (dst.channels() == 3){
  210. Vec3b& BGR = dst.at<Vec3b>(y, x);
  211. BGR[0] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 0)])) & 0xFF);
  212. BGR[1] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 1)])) & 0xFF);
  213. BGR[2] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 2)])) & 0xFF);
  214. } else if (dst.channels() == 4){
  215. Vec4b& BGR = dst.at<Vec4b>(y, x);
  216. BGR[0] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 0)])) & 0xFF);
  217. BGR[1] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 1)])) & 0xFF);
  218. BGR[2] = (uchar)(((uint32_t)(255.f*data[index + ((c == 1) ? 0 : 2)])) & 0xFF);
  219. BGR[3] = 0xFF;
  220. } else return;
  221. };
  222. };
  223. };
  224. void cortexImage::toMat(cv::Mat *dst){
  225. if (dst != NULL)toMat(*dst);
  226. };
  227. cv::Mat *cortexImage::toMat(void){
  228. cv::Mat *RetVal;
  229. RetVal = new cv::Mat(h, w, CV_8UC3);
  230. if (RetVal != NULL)toMat(*RetVal);
  231. return RetVal;
  232. };
  233. /*
  234. cv::Mat *mask_to_rgb( cortexImage *mask ){
  235. int n = mask->c;
  236. cv::Mat *im;
  237. im = new cv::Mat( mask->h, mask->w, CV_32FC3 );
  238. int i, j;
  239. for (j = 0; j < n; ++j){
  240. int offset = j * 123457 % n;
  241. float red = getColor(2, offset, n);
  242. float green = getColor(1, offset, n);
  243. float blue = getColor(0, offset, n);
  244. for (i = 0; i < im.cols*im.rows; ++i){
  245. im.data[i + 0 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * red;
  246. im.data[i + 1 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * green;
  247. im.data[i + 2 * im.cols*im.rows] += mask.data[j*im.cols*im.rows + i] * blue;
  248. }
  249. }
  250. return im;
  251. };
  252. */
  253. bool cortexImage::composite(cortexImage *source, uint32_t dx, uint32_t dy){
  254. uint32_t x,y,k;
  255. for(k = 0; k < source->c; ++k){
  256. for(y = 0; y < source->h; ++y){
  257. for(x = 0; x < source->w; ++x){
  258. float val = source->getPixel( x, y, k );
  259. float val2 = getPixelEX( dx+x, dy+y, k );
  260. setPixel( dx+x, dy+y, k, val*val2 );
  261. };
  262. };
  263. };
  264. return true;
  265. };
  266. void cortexImage::border(uint32_t border){
  267. //image b = make_image(a.w + 2*border, a.h + 2*border, a.c);
  268. uint32_t x, y, k;
  269. for(k = 0; k < c; ++k){
  270. for(y = 0; y < h; ++y){
  271. for(x = 0; x < w; ++x){
  272. float val = getPixelEX(x - border, y - border, k);
  273. if(x - border < 0 || x - border >= w || y - border < 0 || y - border >= h) val = 1;
  274. setPixel( x, y, k, val );
  275. };
  276. };
  277. };
  278. };
  279. void cortexImage::drawLabel(uint32_t r, uint32_t c, cortexImage *label, const float *rgb){
  280. uint32_t tW = label->w;
  281. uint32_t tH = label->h;
  282. if (r - tH >= 0) r = r - tH;
  283. uint32_t i, j, k;
  284. for(j = 0; j < tH && ( j + r )< h; ++j){
  285. for(i = 0; i < tW && (i + c )< w; ++i){
  286. for(k = 0; k < label->c; ++k){
  287. float val = label->getPixel( i, j, k);
  288. setPixel( i+c, j+r, k, rgb[k]*val );
  289. };
  290. };
  291. };
  292. };
  293. void cortexImage::drawBoxEX(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, float r, float g, float b){
  294. //normalize_image(a);
  295. uint32_t i, a;
  296. if(x1 < 0) x1 = 0;
  297. if(x1 >= w) x1 = w-1;
  298. if(x2 < 0) x2 = 0;
  299. if(x2 >= w) x2 = w-1;
  300. if(y1 < 0) y1 = 0;
  301. if(y1 >= h) y1 = h-1;
  302. if(y2 < 0) y2 = 0;
  303. if(y2 >= h) y2 = h-1;
  304. a = w*h;
  305. for(i = x1; i <= x2; ++i){
  306. data[i + y1*w] = r;
  307. data[i + y2*w] = r;
  308. data[i + y1*w + a] = g;
  309. data[i + y2*w + a] = g;
  310. data[i + y1*w + (a<<1)] = b;
  311. data[i + y2*w + (a<<1)] = b;
  312. };
  313. for(i = y1; i <= y2; ++i){
  314. data[x1 + i*w] = r;
  315. data[x2 + i*w] = r;
  316. data[x1 + i*w + a] = g;
  317. data[x2 + i*w + a] = g;
  318. data[x1 + i*w + (a<<1)] = b;
  319. data[x2 + i*w + (a<<1)] = b;
  320. };
  321. };
  322. void cortexImage::drawBoxWEX(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint32_t w, float r, float g, float b){
  323. uint32_t i;
  324. for(i = 0; i < w; ++i){
  325. drawBoxEX( x1+i, y1+i, x2-i, y2-i, r, g, b);
  326. };
  327. };
  328. void cortexImage::drawBox(cortexBOX *bbox, uint32_t w, float r, float g, float b){
  329. uint32_t left = (int)((bbox->x - bbox->w) / 2)*w;
  330. uint32_t right = (int)((bbox->x + bbox->w) / 2)*w;
  331. uint32_t top = (int)((bbox->y - bbox->h) / 2)*h;
  332. uint32_t bot = (int)((bbox->y + bbox->h) / 2)*h;
  333. drawBoxWEX( left, top, right, bot, 2, r, g, b);
  334. };
  335. /*
  336. void draw_detections(image im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, image **alphabet, int classes){
  337. int i;
  338. for(i = 0; i < num; ++i){
  339. int cls = max_index(probs[i], classes);
  340. float prob = probs[i][cls];
  341. if(prob > thresh){
  342. int width = (int)(im.h * .006);
  343. if(0){
  344. width = (int)( pow(prob, (float)(1./2.))*10+1);
  345. alphabet = 0;
  346. }
  347. //printf("%d %s: %.0f%%\n", i, names[class], prob*100);
  348. printf("%s: %.0f%%\n", names[cls], prob*100);
  349. int offset = cls*123457 % classes;
  350. float red = get_color(2,offset,classes);
  351. float green = get_color(1,offset,classes);
  352. float blue = get_color(0,offset,classes);
  353. float rgb[3];
  354. //width = prob*20+2;
  355. rgb[0] = red;
  356. rgb[1] = green;
  357. rgb[2] = blue;
  358. box b = boxes[i];
  359. int left = (int)(b.x - b.w / 2.)*im.w;
  360. int right = (int)(b.x + b.w / 2.)*im.w;
  361. int top = (int)(b.y - b.h / 2.)*im.h;
  362. int bot = (int)(b.y + b.h / 2.)*im.h;
  363. if(left < 0) left = 0;
  364. if(right > im.w-1) right = im.w-1;
  365. if(top < 0) top = 0;
  366. if(bot > im.h-1) bot = im.h-1;
  367. draw_box_width(im, left, top, right, bot, width, red, green, blue);
  368. if (alphabet) {
  369. image label = get_label(alphabet, names[cls], (int)((im.h*.03) / 10));
  370. draw_label(im, top + width, left, label, rgb);
  371. free_image(label);
  372. }
  373. if (masks){
  374. image mask = float_to_image(14, 14, 1, masks[i]);
  375. image resized_mask = resize_image(mask, (int)(b.w*im.w), (int)(b.h*im.h));
  376. image tmask = threshold_image(resized_mask, .5);
  377. embed_image(tmask, im, left, top);
  378. free_image(mask);
  379. free_image(resized_mask);
  380. free_image(tmask);
  381. }
  382. }
  383. }
  384. }
  385. void drawMAT_detections(cv::Mat &im, int num, float thresh, box *boxes, float **probs, float **masks, char **names, int classes){
  386. char Buffer[220];
  387. sprintf(Buffer, "Detected %d objects", num );
  388. rectangle( im, Point(im.cols - 120, 5), Point(im.cols - 10, 15), Scalar(0, 0, 255), CV_FILLED);
  389. putText( im, Buffer, Point(im.cols - 115, 13), FONT_HERSHEY_SIMPLEX,.3, Scalar(255, 255, 0), 1);
  390. for (int i = 0; i < num; ++i){
  391. int cls = max_index(probs[i], classes);
  392. float prob = probs[i][cls];
  393. if (prob > thresh){
  394. //int width = (int)(im.rows * .006);
  395. //printf("%d %s: %.0f%%\n", i, names[class], prob*100);
  396. sprintf(Buffer, "%s: %.0f%%", names[cls], prob * 100);
  397. int offset = cls * 123457 % classes;
  398. float red = get_color(2, offset, classes);
  399. float green = get_color(1, offset, classes);
  400. float blue = get_color(0, offset, classes);
  401. //float rgb[3];
  402. //width = prob*20+2;
  403. //rgb[0] = red;
  404. //rgb[1] = green;
  405. //rgb[2] = blue;
  406. box b = boxes[i];
  407. int left = (int)((b.x - (b.w / 2.f))*im.cols);
  408. int right = (int)((b.x + (b.w / 2.f))*im.cols);
  409. int top = (int)((b.y - (b.h / 2.f))*im.rows);
  410. int bot = (int)((b.y + (b.h / 2.f))*im.rows);
  411. if (left < 0) left = 0;
  412. if (right > im.cols - 1) right = im.cols - 1;
  413. if (top < 0) top = 0;
  414. if (bot > im.rows - 1) bot = im.rows - 1;
  415. putText(im, Buffer, Point(b.x*im.cols, b.y*im.rows), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(255, 255, 0), 2);
  416. rectangle(im, Point(left, top), Point(right, bot), Scalar(0, 255, 0), 2);
  417. //draw_box_width(im, left, top, right, bot, width, red, green, blue);
  418. };
  419. };
  420. }
  421. */
  422. void cortexImage::transpose(void){
  423. assert(w == h);
  424. uint16_t i, j, k;
  425. for(i = 0; i < c; ++i){
  426. for(j = 0; j < w-1; ++j){
  427. for(k = j + 1; k < w; ++k){
  428. float swap = data[k + w*(j + h*i)];
  429. data[k + w*(j + h*i)] = data[j + w*(k + h*i)];
  430. data[j + w*(k + h*i)] = swap;
  431. };
  432. };
  433. };
  434. };
  435. void cortexImage::rotateCW(uint32_t times){
  436. assert(w == h);
  437. times = (times + 400) % 4;
  438. uint32_t i, x, y, l;
  439. uint32_t n = w;
  440. for(i = 0; i < times; ++i){
  441. for(l = 0; l < c; ++l){
  442. for(x = 0; x < n/2; ++x){
  443. for(y = 0; y < (n-1)/2 + 1; ++y){
  444. float temp = data[y + w*(x + h*l)];
  445. data[y + w*(x + h*l)] = data[n-1-x + w*(y + h*l)];
  446. data[n-1-x + w*(y + h*l)] = data[n-1-y + w*(n-1-x + h*l)];
  447. data[n-1-y + w*(n-1-x + h*l)] = data[x + w*(n-1-y + h*l)];
  448. data[x + w*(n-1-y + h*l)] = temp;
  449. };
  450. };
  451. };
  452. };
  453. };
  454. void cortexImage::flip(void){
  455. uint16_t i, j, k;
  456. for(k = 0; k < c; ++k){
  457. for(i = 0; i < h; ++i){
  458. for(j = 0; j < w/2; ++j){
  459. int index = j + w*(i + h*(k));
  460. int flip = (w - j - 1) + w*(i + h*(k));
  461. float swap = data[flip];
  462. data[flip] = data[index];
  463. data[index] = swap;
  464. };
  465. };
  466. };
  467. };
  468. void cortexImage::distance(cortexImage *b){
  469. uint16_t i, j;
  470. for(i = 0; i < c; ++i){
  471. for(j = 0; j < h*w; ++j){
  472. data[j] += pow( data[i*h*w+j]-data[i*h*w+j], 2 );
  473. };
  474. };
  475. for(j = 0; j < h*w; ++j){
  476. data[j] = sqrt(data[j]);
  477. };
  478. };
  479. void cortexImage::ghost( cortexImage *source, uint32_t dx, uint32_t dy){
  480. uint16_t x, y, k;
  481. float max_dist = (float)sqrt((-source->w/2.f + .5)*(-source->w/2.f + .5));
  482. for(k = 0; k < source->c; ++k){
  483. for(y = 0; y < source->h; ++y){
  484. for(x = 0; x < source->w; ++x){
  485. float dist = (float)sqrt((x - source->w/2.f + .5f)*(x - source->w/2.f + .5f) + (y - source->h/2.f + .5f)*(y - source->h/2.f + .5f));
  486. float alpha = (1 - dist/max_dist);
  487. if(alpha < 0) alpha = 0;
  488. float v1 = source->getPixel(x,y,k);
  489. float v2 = getPixel(dx+x,dy+y,k);
  490. float val = alpha*v1 + (1-alpha)*v2;
  491. setPixel(dx+x, dy+y, k, val);
  492. };
  493. };
  494. };
  495. };
  496. void cortexImage::embed(cortexImage *source, uint32_t dx, uint32_t dy){
  497. uint32_t x, y, k;
  498. for(k = 0; k < source->c; ++k){
  499. for(y = 0; y < source->h; ++y){
  500. for(x = 0; x < source->w; ++x){
  501. float val = source->getPixel(x,y,k);
  502. setPixel(dx+x, dy+y, k, val);
  503. };
  504. };
  505. };
  506. };
  507. /*
  508. cortexImage *collapse_image_layers(cortexImage *source, uint32_t border){
  509. uint32_t h = source.h;
  510. h = (h+border)*source.c - border;
  511. image dest = make_image(source.w, h, 1);
  512. uint32_t i;
  513. for(i = 0; i < source.c; ++i){
  514. image layer = get_image_layer(source, i);
  515. uint32_t h_offset = i*(source.h + border);
  516. embed_image(layer, dest, 0, h_offset);
  517. free_image(layer);
  518. }
  519. return dest;
  520. };
  521. */
  522. cortexImage *cortexImage::getLayer(uint32_t l){
  523. cortexImage *out;
  524. out = new cortexImage();
  525. out->makeWithData(w, h, 1);// make_image(m.w, m.h, 1);
  526. int i;
  527. for (i = 0; i < h*w; ++i){
  528. out->data[i] = data[i + l*h*w];
  529. };
  530. return out;
  531. };
  532. void cortexImage::constrain(void){
  533. uint16_t i;
  534. for(i = 0; i < w*h*c; ++i){
  535. if(data[i] < 0) data[i] = 0;
  536. if(data[i] > 1) data[i] = 1;
  537. };
  538. };
  539. void cortexImage::normalize(void){
  540. uint16_t i;
  541. float min = 9999999.f;
  542. float max = -999999.f;
  543. for(i = 0; i < h*w*c; ++i){
  544. float v = data[i];
  545. if(v < min) min = v;
  546. if(v > max) max = v;
  547. }
  548. if(max - min < .000000001f){
  549. min = 0.f;
  550. max = 1.f;
  551. }
  552. for(i = 0; i < c*w*h; ++i){
  553. data[i] = (data[i] - min)/(max-min);
  554. };
  555. };
  556. void cortexImage::normalize2( void ){
  557. float *min = (float*)calloc(c, sizeof(float));
  558. float *max = (float*)calloc(c, sizeof(float));
  559. uint16_t i, j;
  560. for(i = 0; i < c; ++i) min[i] = max[i] = data[i*h*w];
  561. for(j = 0; j < c; ++j){
  562. for(i = 0; i < h*w; ++i){
  563. float v = data[i+j*h*w];
  564. if(v < min[j]) min[j] = v;
  565. if(v > max[j]) max[j] = v;
  566. };
  567. };
  568. for(i = 0; i < c; ++i){
  569. if(max[i] - min[i] < .000000001f){
  570. min[i] = 0;
  571. max[i] = 1;
  572. };
  573. };
  574. for(j = 0; j < c; ++j){
  575. for(i = 0; i < w*h; ++i){
  576. data[i+j*h*w] = (data[i+j*h*w] - min[j])/(max[j]-min[j]);
  577. };
  578. };
  579. free(min);
  580. free(max);
  581. };
  582. void cortexImage::copyInto(cortexImage *dest){
  583. if (dest->data != NULL) free(dest->data);
  584. dest->data = (float*)calloc(h*w*c, sizeof(float));
  585. dest->size = h*w*c;
  586. dest->step = w*c;
  587. if (dest->data != NULL){
  588. dest->h = h;
  589. dest->w = w;
  590. dest->c = c;
  591. memcpy(dest->data, data, h*w*c*sizeof(float));
  592. } else {
  593. dest->h = 0;
  594. dest->w = 0;
  595. dest->c = 0;
  596. dest->size = 0;
  597. dest->step = 0;
  598. };
  599. };
  600. void cortexImage::rgb_to_bgr(void){
  601. uint16_t i;
  602. for(i = 0; i < w*h; ++i){
  603. float swap = data[i];
  604. data[i] = data[i+w*h*2];
  605. data[i+w*h*2] = swap;
  606. };
  607. };
  608. bool cortexImage::toIpl(IplImage* dst){
  609. unsigned char *idata = (unsigned char *)dst->imageData;
  610. uint32_t hh = dst->height;
  611. uint32_t ww = dst->width;
  612. uint32_t cc = dst->nChannels;
  613. uint32_t sstep = dst->widthStep;
  614. uint32_t i, j, k;
  615. for (i = 0; i < hh; ++i){
  616. for (k = 0; k < cc; ++k){
  617. for (j = 0; j < ww; ++j){
  618. idata[i*sstep + j*cc + k] = ((int)( 255.f * data[k*w*h + i*w + j])) & 0xFF;
  619. };
  620. };
  621. };
  622. return true;
  623. };
  624. /*
  625. void flush_stream_buffer(CvCapture *cap, int n)
  626. {
  627. int i;
  628. for(i = 0; i < n; ++i) {
  629. cvQueryFrame(cap);
  630. }
  631. }
  632. image get_image_from_stream(CvCapture *cap)
  633. {
  634. IplImage* src = cvQueryFrame(cap);
  635. if (!src) return make_empty_image(0,0,0);
  636. image im = ipl_to_image(src);
  637. rgbgr_image(im);
  638. return im;
  639. }
  640. int fill_image_from_stream(CvCapture *cap, image im)
  641. {
  642. IplImage* src = cvQueryFrame(cap);
  643. if (!src) return 0;
  644. ipl_into_image(src, im);
  645. rgbgr_image(im);
  646. return 1
  647. }
  648. */
  649. /*
  650. void cortexImage::saveJPG(const char *name){
  651. char buff[256];
  652. uint32_t x, y, k;
  653. cortexImage *copy;
  654. copy = new cortexImage( this );
  655. if (c == 3) copy->rgb_to_bgr();
  656. sprintf(buff, "%s.jpg", name);
  657. IplImage *disp = cvCreateImage(cvSize(w,h), IPL_DEPTH_8U, c);
  658. int step = disp->widthStep;
  659. for(y = 0; y < h; ++y){
  660. for(x = 0; x < w; ++x){
  661. for(k= 0; k < c; ++k){
  662. disp->imageData[y*step + x*c + k] = (unsigned char)(copy->getPixel(x, y, k) * 255);
  663. };
  664. };
  665. };
  666. cvSaveImage( buff, disp, 0 );
  667. cvReleaseImage(&disp);
  668. delete(copy);
  669. };
  670. */
  671. void cortexImage::saveJPG(const char *name, int Quality ){
  672. uint16_t i, k;
  673. char buff[256];
  674. sprintf(buff, "%s.png", name);
  675. unsigned char *Zdata = (unsigned char*)calloc(w*h*c, sizeof(char));
  676. for (k = 0; k < c; ++k){
  677. for (i = 0; i < w*h; ++i){
  678. Zdata[i*c + k] = (unsigned char)(255 * data[i + k*w*h]);
  679. };
  680. };
  681. int success = stbi_write_jpg(buff, w, h, c, Zdata, Quality );
  682. free(Zdata);
  683. if (!success) fprintf(stderr, "Failed to write image %s\n", buff);
  684. };
  685. void cortexImage::savePNG(const char *name){
  686. uint16_t i, k;
  687. char buff[256];
  688. sprintf(buff, "%s.png", name);
  689. unsigned char *Zdata = (unsigned char*)calloc(w*h*c, sizeof(char));
  690. for(k = 0; k < c; ++k){
  691. for(i = 0; i < w*h; ++i){
  692. Zdata[i*c+k] = (unsigned char) (255*data[i + k*w*h]);
  693. };
  694. };
  695. int success = stbi_write_png( buff, w, h, c, Zdata, w*c );
  696. free(Zdata);
  697. if(!success) fprintf(stderr, "Failed to write image %s\n", buff );
  698. };
  699. void cortexImage::makeEmpty(uint32_t ww, uint32_t hh, uint32_t cc){
  700. if (data != NULL) free(data);
  701. h = hh;
  702. w = ww;
  703. c = cc;
  704. step = 0;
  705. size = 0;
  706. };
  707. void cortexImage::freeData(void){
  708. if (data != NULL) free(data);
  709. };
  710. void cortexImage::makeWithData(uint32_t ww, uint32_t hh, uint32_t cc){
  711. makeEmpty( ww, hh, cc );
  712. size = h*w*c;
  713. data = (float*)calloc(size, sizeof(float));
  714. };
  715. void cortexImage::fillRandom(void){
  716. freeData();
  717. data = (float*)calloc(size, sizeof(float));
  718. for (uint16_t i = 0; i < size; ++i){
  719. data[i] = (float)((rand_normal() * .25) + .5);
  720. };
  721. };
  722. void cortexImage::setData(uint32_t ww, uint32_t hh, uint32_t cc, float *ddata){
  723. freeData();
  724. w = ww;
  725. h = hh;
  726. c = cc;
  727. data = ddata;
  728. };
  729. void cortexImage::place(uint32_t ww, uint32_t hh, uint32_t dx, uint32_t dy, cortexImage *canvas){
  730. uint32_t x, y, cc;
  731. for(cc = 0; cc < c; ++cc){
  732. for(y = 0; y < hh; ++y){
  733. for(x = 0; x < ww; ++x){
  734. uint32_t rx = (uint32_t)((float)x / ww) * w;
  735. uint32_t ry = (uint32_t)((float)y / hh) * h;
  736. float val = bilinear_interpolate( (float)rx, (float)ry, cc );
  737. canvas->setPixel( x + dx, y + dy, cc, val);
  738. };
  739. };
  740. };
  741. };
  742. void cortexImage::centerCrop(uint32_t ww, uint32_t hh){
  743. int m = (w < h) ? w : h;
  744. crop((w - m) / 2, (h - m) / 2, m, m);
  745. resize(ww, hh);
  746. };
  747. void cortexImage::rotateCrop( float rad, float s, uint32_t ww, uint32_t hh, float dx, float dy, float aspect){
  748. uint32_t x, y, cc;
  749. float cx = (float)(w/2.);
  750. float cy = (float)(h/2.);
  751. cortexImage *rot;
  752. rot = new cortexImage();
  753. rot->makeWithData(ww, hh, c);
  754. for(cc = 0; cc < c; ++cc){
  755. for(y = 0; y < hh; ++y){
  756. for(x = 0; x < ww; ++x){
  757. float rx = (float)(cos(rad)*((x - ww/2.)/s*aspect + dx/s*aspect) - sin(rad)*((y - hh/2.)/s + dy/s) + cx);
  758. float ry = (float)(sin(rad)*((x - ww/2.)/s*aspect + dx/s*aspect) + cos(rad)*((y - hh/2.)/s + dy/s) + cy);
  759. float val = bilinear_interpolate( rx, ry, cc);
  760. rot->setPixel(x, y, cc, val);
  761. };
  762. };
  763. };
  764. setData(rot->w, rot->h, rot->c, rot->data);
  765. delete(rot);
  766. };
  767. void cortexImage::rotate(float rad){
  768. uint32_t x, y, cc;
  769. float cx = (float)(w / 2.f);
  770. float cy = (float)(h / 2.f);
  771. cortexImage *rot;
  772. rot = new cortexImage();
  773. rot->makeWithData(w, h, c );
  774. for (cc = 0; cc < c; ++c){
  775. for (y = 0; y < h; ++y){
  776. for (x = 0; x < w; ++x){
  777. float rx = (float)(cos(rad)*(x-cx) - sin(rad)*(y-cy) + cx);
  778. float ry = (float)(sin(rad)*(x-cx) + cos(rad)*(y-cy) + cy);
  779. float val = bilinear_interpolate( rx, ry, cc);
  780. rot->setPixel( x, y, cc, val);
  781. };
  782. };
  783. };
  784. setData(rot->w, rot->h, rot->c, rot->data);
  785. delete(rot);
  786. };
  787. void cortexImage::fill(float s){
  788. for (uint16_t i = 0; i < h*w*c; ++i) data[i] = s;
  789. };
  790. void cortexImage::translate(float s){
  791. for (uint16_t i = 0; i < h*w*c; ++i) data[i] += s;
  792. };
  793. void cortexImage::scale(float s){
  794. for (uint16_t i = 0; i < h*w*c; ++i) data[i] *= s;
  795. };
  796. void cortexImage::crop(uint32_t dx, uint32_t dy, uint32_t ww, uint32_t hh){
  797. cortexImage *cropped;
  798. cropped = new cortexImage();
  799. cropped->makeWithData(ww, hh, c );
  800. uint32_t i, j, k;
  801. for(k = 0; k < c; ++k){
  802. for(j = 0; j < hh; ++j){
  803. for(i = 0; i < ww; ++i){
  804. uint32_t r = j + dy;
  805. uint32_t cc = i + dx;
  806. float val = 0;
  807. r = constrain_int(r, 0, h-1);
  808. cc = constrain_int(cc, 0, w-1);
  809. val = getPixel( cc, r, k);
  810. cropped->setPixel(i, j, k, val);
  811. };
  812. };
  813. };
  814. setData(cropped->w, cropped->h, cropped->c, cropped->data);
  815. delete(cropped);
  816. };
  817. int best3dShiftR(cortexImage *a, cortexImage *b, int min, int max){
  818. if(min == max) return min;
  819. int mid = (int)floor((min + max) / 2.);
  820. cortexImage *c1;
  821. cortexImage *c2;
  822. c1 = new cortexImage(b);
  823. c2 = new cortexImage(b);
  824. c1->crop(0, mid, b->w, b->h);
  825. c2->crop(0, mid + 1, b->w, b->h);
  826. float d1 = dist_array(c1->data, a->data, a->w*a->h*a->c, 10);
  827. float d2 = dist_array(c2->data, a->data, a->w*a->h*a->c, 10);
  828. c1->freeData();
  829. c2->freeData();
  830. delete(c1);
  831. delete(c2);
  832. if( d1 < d2) return best3dShiftR(a, b, min, mid);
  833. else return best3dShiftR(a, b, mid+1, max);
  834. };
  835. int best3dShift(cortexImage *a, cortexImage *b, int min, int max){
  836. uint16_t i;
  837. int best = 0;
  838. float best_distance = FLT_MAX;
  839. for(i = min; i <= max; i += 2){
  840. cortexImage *c;
  841. c = new cortexImage(b);
  842. c->crop(0, i, b->w, b->h);
  843. float d = dist_array(c->data, a->data, a->w*a->h*a->c, 100);
  844. if(d < best_distance){
  845. best_distance = d;
  846. best = i;
  847. }
  848. printf("%d %f\n", i, d);
  849. c->freeData();
  850. delete(c);
  851. };
  852. return best;
  853. };
  854. void composite3d(char *f1, char *f2, char *out, int delta){
  855. if(!out) out = "out";
  856. cortexImage *a;
  857. a = new cortexImage();
  858. a->fillFromFile(f1, 0, 0, 3);
  859. cortexImage *b;
  860. b = new cortexImage();
  861. b->fillFromFile(f2, 0, 0, 3);
  862. int shift = best3dShiftR(a, b, -a->h/100, a->h/100);
  863. cortexImage *c1;
  864. c1 = new cortexImage(b);
  865. c1->crop( 10, shift, b->w, b->h);
  866. float d1 = dist_array(c1->data, a->data, a->w*a->h*a->c, 100);
  867. cortexImage *c2;
  868. c2 = new cortexImage(b);
  869. c2->crop(-10, shift, b->w, b->h);
  870. float d2 = dist_array(c2->data, a->data, a->w*a->h*a->c, 100);
  871. if(d2 < d1 && 0){
  872. cortexImage *swap = a;
  873. a = b;
  874. b = swap;
  875. shift = -shift;
  876. printf("swapped, %d\n", shift);
  877. } else{
  878. printf("%d\n", shift);
  879. }
  880. cortexImage *c;
  881. c = new cortexImage(b);
  882. c->crop( delta, shift, a->w, a->h);
  883. int i;
  884. for(i = 0; i < c->w*c->h; ++i){
  885. c->data[i] = a->data[i];
  886. };
  887. c->saveJPG(out);
  888. c->freeData();
  889. c1->freeData();
  890. c2->freeData();
  891. a->freeData();
  892. b->freeData();
  893. delete(c);
  894. delete(c1);
  895. delete(c2);
  896. delete(a);
  897. delete(b);
  898. };
  899. void cortexImage::letterboxInto(cortexImage *boxed){
  900. int new_w = w;
  901. int new_h = h;
  902. if (((float)boxed->w / w) < ((float)boxed->h / h)) {
  903. new_w = boxed->w;
  904. new_h = (h * boxed->w) / w;
  905. } else {
  906. new_h = boxed->h;
  907. new_w = (w * boxed->h) / h;
  908. };
  909. cortexImage *resized;
  910. resized = new cortexImage(this);
  911. resized->resize(new_w, new_h);
  912. boxed->embed(resized, (boxed->w-new_w)/2, (boxed->h-new_h)/2);
  913. resized->freeData();
  914. delete(resized);
  915. };
  916. void cortexImage::letterbox(uint32_t ww, uint32_t hh){
  917. int new_w = w;
  918. int new_h = h;
  919. if (((float)ww/w) < ((float)hh/h)) {
  920. new_w = ww;
  921. new_h = (h * ww)/w;
  922. } else {
  923. new_h = hh;
  924. new_w = (w * hh)/h;
  925. }
  926. resize( new_w, new_h);
  927. cortexImage *boxed;
  928. boxed = new cortexImage();
  929. boxed->makeWithData(ww, hh, c);
  930. boxed->fill(.5f);
  931. //int i;
  932. //for(i = 0; i < boxed.w*boxed.h*boxed.c; ++i) boxed.data[i] = 0;
  933. boxed->embed(this, (ww-new_w)/2, (hh-new_h)/2);
  934. setData(ww, hh, c, boxed->data);
  935. delete(boxed);
  936. };
  937. void cortexImage::resizeMax(uint32_t max){
  938. int ww = w;
  939. int hh = h;
  940. if(ww > hh){
  941. hh = (hh * max) / ww;
  942. ww = max;
  943. } else {
  944. ww = (ww * max) / hh;
  945. hh = max;
  946. }
  947. if(ww == w && hh == h) return;
  948. resize( ww, hh );
  949. };
  950. void cortexImage::resizeMin(uint32_t min){
  951. int ww = w;
  952. int hh = h;
  953. if(ww < hh){
  954. hh = (hh * min) / ww;
  955. ww = min;
  956. } else {
  957. ww = (ww * min) / hh;
  958. hh = min;
  959. };
  960. if(ww == w && hh == h) return;
  961. resize(ww, hh);
  962. };
  963. void cortexImage::randomCrop(uint32_t ww, uint32_t hh){
  964. int dx = rand_int(0, w - ww);
  965. int dy = rand_int(0, h - hh);
  966. crop(dx, dy, ww, hh);
  967. };
  968. cortexARGS cortexImage::randomAugmentArgs(float angle, float aspect, uint32_t low, uint32_t high, uint32_t ww, uint32_t hh){
  969. cortexARGS a = { 0 };
  970. aspect = rand_scale(aspect);
  971. uint32_t r = rand_int(low, high);
  972. uint32_t min = (int)((h < w*aspect) ? h : w*aspect);
  973. float scale = (float)r / min;
  974. float rad = (float)(rand_uniform(-angle, angle) * TWO_PI / 360.f);
  975. float dx = (float)((w*scale/aspect - ww) / 2.);
  976. float dy = (float)((h*scale - ww) / 2.);
  977. //if(dx < 0) dx = 0;
  978. //if(dy < 0) dy = 0;
  979. dx = rand_uniform(-dx, dx);
  980. dy = rand_uniform(-dy, dy);
  981. a.rad = rad;
  982. a.scale = scale;
  983. a.w = ww;
  984. a.h = hh;
  985. a.dx = dx;
  986. a.dy = dy;
  987. a.aspect = aspect;
  988. return a;
  989. }
  990. void cortexImage::randomAugment(float angle, float aspect, uint32_t low, uint32_t high, uint32_t w, uint32_t h){
  991. cortexARGS a = randomAugmentArgs(angle, aspect, low, high, w, h);
  992. rotateCrop( a.rad, a.scale, a.w, a.h, a.dx, a.dy, a.aspect);
  993. };
  994. float three_way_max(float a, float b, float c){
  995. return (a > b) ? ( (a > c) ? a : c) : ( (b > c) ? b : c) ;
  996. };
  997. float three_way_min(float a, float b, float c){
  998. return (a < b) ? ( (a < c) ? a : c) : ( (b < c) ? b : c) ;
  999. };
  1000. void cortexImage::yuv_to_rgb(void){
  1001. assert( c == 3);
  1002. float r, g, b;
  1003. float y, u, v;
  1004. for (uint16_t j = 0; j < h; ++j){
  1005. for (uint16_t i = 0; i < w; ++i){
  1006. y = getPixel( i , j, 0);
  1007. u = getPixel( i , j, 1);
  1008. v = getPixel( i , j, 2);
  1009. r = (float)(y + 1.13983*v);
  1010. g = (float)(y + -.39465*u + -.58060*v);
  1011. b = (float)(y + 2.03211*u);
  1012. setPixel( i, j, 0, r);
  1013. setPixel( i, j, 1, g);
  1014. setPixel( i, j, 2, b);
  1015. };
  1016. };
  1017. };
  1018. void cortexImage::rgb_to_yuv(void){
  1019. assert(c == 3);
  1020. float r, g, b;
  1021. float y, u, v;
  1022. for (uint16_t j = 0; j < h; ++j){
  1023. for (uint16_t i = 0; i < w; ++i){
  1024. r = getPixel( i , j, 0);
  1025. g = getPixel( i , j, 1);
  1026. b = getPixel( i , j, 2);
  1027. y = (float)(.299*r + .587*g + .114*b);
  1028. u = (float)(-.14713*r + -.28886*g + .436*b);
  1029. v = (float)(.615*r + -.51499*g + -.10001*b);
  1030. setPixel( i, j, 0, y);
  1031. setPixel( i, j, 1, u);
  1032. setPixel( i, j, 2, v);
  1033. };
  1034. };
  1035. };
  1036. // http://www.cs.rit.edu/~ncs/color/t_convert.html
  1037. void cortexImage::rgb_to_hsv(void){
  1038. assert( c == 3);
  1039. float r, g, b;
  1040. float hh, s, v;
  1041. for (uint16_t j = 0; j < h; ++j){
  1042. for (uint16_t i = 0; i < w; ++i){
  1043. r = getPixel( i , j, 0);
  1044. g = getPixel( i , j, 1);
  1045. b = getPixel( i , j, 2);
  1046. float max = three_way_max(r,g,b);
  1047. float min = three_way_min(r,g,b);
  1048. float delta = max - min;
  1049. v = max;
  1050. if(max == 0){
  1051. s = 0;
  1052. hh = 0;
  1053. }else{
  1054. s = delta/max;
  1055. if(r == max){
  1056. hh = (g - b) / delta;
  1057. } else if (g == max) {
  1058. hh = 2 + (b - r) / delta;
  1059. } else {
  1060. hh = 4 + (r - g) / delta;
  1061. }
  1062. if (hh < 0) hh += 6;
  1063. hh = (float)(hh/6.);
  1064. }
  1065. setPixel( i, j, 0, hh);
  1066. setPixel( i, j, 1, s);
  1067. setPixel( i, j, 2, v);
  1068. };
  1069. };
  1070. };
  1071. void cortexImage::hsv_to_rgb( void ){
  1072. assert(c == 3);
  1073. float r, g, b;
  1074. float hh, s, v;
  1075. float f, p, q, t;
  1076. for (uint16_t j = 0; j < h; ++j){
  1077. for (uint16_t i = 0; i < w; ++i){
  1078. hh = 6 * getPixel( i , j, 0);
  1079. s = getPixel( i , j, 1);
  1080. v = getPixel( i , j, 2);
  1081. if (s == 0) {
  1082. r = g = b = v;
  1083. } else {
  1084. int index = (int)floor(hh);
  1085. f = hh - index;
  1086. p = v*(1-s);
  1087. q = v*(1-s*f);
  1088. t = v*(1-s*(1-f));
  1089. if(index == 0){
  1090. r = v; g = t; b = p;
  1091. } else if(index == 1){
  1092. r = q; g = v; b = p;
  1093. } else if(index == 2){
  1094. r = p; g = v; b = t;
  1095. } else if(index == 3){
  1096. r = p; g = q; b = v;
  1097. } else if(index == 4){
  1098. r = t; g = p; b = v;
  1099. } else {
  1100. r = v; g = p; b = q;
  1101. };
  1102. };
  1103. setPixel( i, j, 0, r);
  1104. setPixel( i, j, 1, g);
  1105. setPixel( i, j, 2, b);
  1106. };
  1107. };
  1108. };
  1109. void cortexImage::grayscale_3C(void){
  1110. assert( c == 3);
  1111. float scale[] = {(float)0.299, (float)0.587, 0.114f};
  1112. for (uint16_t j = 0; j < h; ++j){
  1113. for (uint16_t i = 0; i < w; ++i){
  1114. float val = 0;
  1115. for (uint8_t k = 0; k < 3; ++k){
  1116. val += scale[k]*getPixel( i, j, k);
  1117. }
  1118. data[0*h*w + w*j + i] = val;
  1119. data[1*h*w + w*j + i] = val;
  1120. data[2*h*w + w*j + i] = val;
  1121. };
  1122. };
  1123. };
  1124. void cortexImage::grayscale(void){
  1125. assert(c == 3);
  1126. //image gray = make_image(im.w, im.h, 1);
  1127. float scale[] = {0.299f, 0.587f, 0.114f};
  1128. for (uint16_t k = 0; k < c; ++k){
  1129. for (uint16_t j = 0; j < h; ++j){
  1130. for (uint16_t i = 0; i < w; ++i){
  1131. data[i+w*j] += scale[k]*getPixel( i, j, k);
  1132. };
  1133. };
  1134. };
  1135. };
  1136. void cortexImage::threshold(float thresh){
  1137. for (uint16_t i = 0; i < w*h*c; ++i){
  1138. data[i] = (float)(data[i]>thresh ? 1 : 0);
  1139. };
  1140. };
  1141. void cortexImage::blendAsFront(cortexImage *back, float alpha){
  1142. assert(w == back->w && h == back->h && c == back->c);
  1143. //image blend = make_image(fore.w, fore.h, fore.c);
  1144. for (uint16_t k = 0; k < c; ++k){
  1145. for (uint16_t j = 0; j < h; ++j){
  1146. for (uint16_t i = 0; i < w; ++i){
  1147. float val = alpha * getPixel( i, j, k) +
  1148. (1 - alpha)* back->getPixel(i, j, k);
  1149. setPixel( i, j, k, val );
  1150. };
  1151. };
  1152. };
  1153. };
  1154. void cortexImage::blendAsBack(cortexImage *fore, float alpha){
  1155. assert(fore->w == w && fore->h == h && fore->c == c);
  1156. //image blend = make_image(fore.w, fore.h, fore.c);
  1157. for (uint16_t k = 0; k < fore->c; ++k){
  1158. for (uint16_t j = 0; j < fore->h; ++j){
  1159. for (uint16_t i = 0; i < fore->w; ++i){
  1160. float val = alpha * fore->getPixel(i, j, k) +
  1161. (1 - alpha)* getPixel(i, j, k);
  1162. setPixel( i, j, k, val );
  1163. };
  1164. };
  1165. };
  1166. };
  1167. void cortexImage::scale_channel(uint32_t cc, float v){
  1168. for (uint16_t j = 0; j < h; ++j){
  1169. for (uint16_t i = 0; i < w; ++i){
  1170. float pix = getPixel( i, j, cc);
  1171. pix = pix*v;
  1172. setPixel( i, j, cc, pix);
  1173. };
  1174. };
  1175. };
  1176. void cortexImage::translate_channel(uint32_t cc, float v){
  1177. for (uint16_t j = 0; j < h; ++j){
  1178. for (uint16_t i = 0; i < w; ++i){
  1179. float pix = getPixel( i, j, cc);
  1180. pix = pix+v;
  1181. setPixel( i, j, cc, pix);
  1182. };
  1183. };
  1184. };
  1185. void cortexImage::binarize(void){
  1186. for (uint16_t i = 0; i < w * h * c; ++i){
  1187. if(data[i] > .5) data[i] = 1; else data[i] = 0;
  1188. };
  1189. };
  1190. void cortexImage::saturate(float sat){
  1191. rgb_to_hsv();
  1192. scale_channel(1, sat);
  1193. hsv_to_rgb();
  1194. constrain();
  1195. };
  1196. void cortexImage::hue(float hue){
  1197. rgb_to_hsv();
  1198. for (uint16_t i = 0; i < w*h; ++i){
  1199. data[i] = data[i] + hue;
  1200. if (data[i] > 1) data[i] -= 1;
  1201. if (data[i] < 0) data[i] += 1;
  1202. }
  1203. hsv_to_rgb();
  1204. constrain();
  1205. };
  1206. void cortexImage::exposure(float sat){
  1207. rgb_to_hsv();
  1208. scale_channel( 2, sat);
  1209. hsv_to_rgb();
  1210. constrain();
  1211. };
  1212. void cortexImage::distort( float hue, float sat, float val ){
  1213. rgb_to_hsv();
  1214. scale_channel( 1, sat);
  1215. scale_channel( 2, val);
  1216. for (uint16_t i = 0; i < w*h; ++i){
  1217. data[i] = data[i] + hue;
  1218. if (data[i] > 1) data[i] -= 1;
  1219. if (data[i] < 0) data[i] += 1;
  1220. }
  1221. hsv_to_rgb();
  1222. constrain();
  1223. };
  1224. void cortexImage::randomDistort(float hue, float saturation, float exposure){
  1225. float dhue = rand_uniform(-hue, hue);
  1226. float dsat = rand_scale(saturation);
  1227. float dexp = rand_scale(exposure);
  1228. distort(dhue, dsat, dexp);
  1229. };
  1230. void cortexImage::saturateExposure(float sat, float exposure){
  1231. rgb_to_hsv();
  1232. scale_channel(1, sat);
  1233. scale_channel(2, exposure);
  1234. hsv_to_rgb();
  1235. constrain();
  1236. };
  1237. float cortexImage::bilinear_interpolate(float x, float y, uint32_t cc){
  1238. int ix = (int) floorf(x);
  1239. int iy = (int) floorf(y);
  1240. float dx = x - ix;
  1241. float dy = y - iy;
  1242. float val = (1-dy) * (1-dx) * getPixelEX(ix, iy, cc) +
  1243. dy * (1-dx) * getPixelEX(ix, iy+1, cc) +
  1244. (1-dy) * dx * getPixelEX(ix+1, iy, cc) +
  1245. dy * dx * getPixelEX(ix+1, iy+1, cc);
  1246. return val;
  1247. };
  1248. void cortexImage::resize(uint32_t ww, uint32_t hh){
  1249. cortexImage resized;
  1250. cortexImage part;
  1251. resized.makeEmpty(ww, hh, c);
  1252. part.makeEmpty(ww, h, c);
  1253. uint32_t r, cc, k;
  1254. float w_scale = (float)(w - 1) / (ww - 1);
  1255. float h_scale = (float)(h - 1) / (hh - 1);
  1256. for(k = 0; k < c; ++k){
  1257. for(r = 0; r < h; ++r){
  1258. for(cc = 0; cc < ww; ++cc){
  1259. float val = 0;
  1260. if(cc == ww-1 || w == 1){
  1261. val = getPixel(w-1, r, k);
  1262. } else {
  1263. float sx = cc*w_scale;
  1264. int ix = (int) sx;
  1265. float dx = sx - ix;
  1266. val = (1 - dx) * getPixel(ix, r, k) + dx * getPixel(ix+1, r, k);
  1267. };
  1268. part.setPixel(cc, r, k, val);
  1269. };
  1270. };
  1271. };
  1272. for(k = 0; k < c; ++k){
  1273. for(r = 0; r < hh; ++r){
  1274. float sy = r*h_scale;
  1275. int iy = (int) sy;
  1276. float dy = sy - iy;
  1277. for(cc = 0; cc < ww; ++cc){
  1278. float val = (1 - dy) * part.getPixel(cc, iy, k);
  1279. resized.setPixel(cc, r, k, val);
  1280. };
  1281. if(r == hh-1 || h == 1) continue;
  1282. for(cc = 0; cc < ww; ++cc){
  1283. float val = dy * part.getPixel(cc, iy + 1, k);
  1284. resized.addPixel(cc, r, k, val);
  1285. };
  1286. };
  1287. };
  1288. part.freeData();
  1289. setData( ww, hh, resized.c, resized.data );
  1290. };
  1291. bool cortexImage::fillFromFile(char* path, uint32_t x, uint32_t y, uint32_t channels){
  1292. int ww, hh, cc;
  1293. unsigned char *Zdata = stbi_load(path, &ww, &hh, &cc, channels);
  1294. if (!data) {
  1295. fprintf(stderr, "Cannot load image \"%s\"\nSTB Reason: %s\n", path, stbi_failure_reason());
  1296. return false;
  1297. };
  1298. if(channels) cc = channels;
  1299. int i,j,k;
  1300. makeWithData(ww, hh, cc);
  1301. for(k = 0; k < cc; ++k){
  1302. for(j = 0; j < hh; ++j){
  1303. for(i = 0; i < ww; ++i){
  1304. int dst_index = i + ww*j + ww*hh*k;
  1305. int src_index = k + cc*i + cc*ww*j;
  1306. data[dst_index] = (float)(Zdata[src_index]/255.);
  1307. };
  1308. };
  1309. };
  1310. free(Zdata);
  1311. return true;
  1312. };
  1313. float cortexImage::getPixel(uint32_t x, uint32_t y, uint32_t cc){
  1314. assert(x < w && y < h && cc < c);
  1315. return data[cc*h*w + y*w + x];
  1316. }
  1317. float cortexImage::getPixelEX(uint32_t x, uint32_t y, uint32_t cc){
  1318. if(x < 0 || x >= w || y < 0 || y >= h) return 0;
  1319. /*
  1320. if(x < 0) x = 0;
  1321. if(x >= m.w) x = m.w-1;
  1322. if(y < 0) y = 0;
  1323. if(y >= m.h) y = m.h-1;
  1324. */
  1325. if(cc < 0 || cc >= c) return 0;
  1326. return getPixel( x, y, cc);
  1327. };
  1328. void cortexImage::setPixel(uint32_t x, uint32_t y, uint32_t cc, float val){
  1329. if (x < 0 || y < 0 || c < 0 || x >= w || y >= h || cc >= c) return;
  1330. assert(x < w && y < h && cc < c);
  1331. data[cc*h*w + y*w + x] = val;
  1332. };
  1333. void cortexImage::addPixel(uint32_t x, uint32_t y, uint32_t cc, float val){
  1334. assert(x < w && y < h && cc < c);
  1335. data[cc*h*w + y*w + x] += val;
  1336. };
  1337. void cortexImage::collapseImagesVert(cortexImageList *imgList){
  1338. int color = 1;
  1339. int border = 1;
  1340. int hh,ww,cc,ch;
  1341. ww = (*imgList->begin())->w;
  1342. ch = ((*imgList->begin())->h + border);
  1343. hh = ch * imgList->size() - border;
  1344. cc = (*imgList->begin())->c;
  1345. if(cc != 3 || !color){
  1346. ww = (ww+border)*cc - border;
  1347. cc = 1;
  1348. };
  1349. makeWithData(ww, hh, cc);
  1350. int i,j;
  1351. i = 0;
  1352. for ( cortexImageList::iterator index = imgList->begin(); index < imgList->end(); index++ ){
  1353. int h_offset = i*ch;
  1354. //(*index)->normalize();
  1355. if( cc == 3 && color){
  1356. embed( (*index), 0, h_offset);
  1357. } else{
  1358. for (j = 0; j < (*index)->c; ++j){
  1359. int w_offset = j*((*index)->w + border);
  1360. cortexImage *layer;
  1361. layer = (*index)->getLayer(j);
  1362. embed(layer, w_offset, h_offset);
  1363. layer->freeData();
  1364. delete(layer);
  1365. };
  1366. };
  1367. };
  1368. };
  1369. void cortexImage::collapseImagesHorz(cortexImageList *imgList){
  1370. int color = 1;
  1371. int border = 1;
  1372. int hh,cw,ww,cc;
  1373. int size = (*imgList->begin())->h;
  1374. hh = size;
  1375. cw = ((*imgList->begin())->w + border);
  1376. ww = cw * imgList->size() - border;
  1377. cc = (*imgList->begin())->c;
  1378. if(cc != 3 || !color){
  1379. hh = (hh+border)*cc - border;
  1380. cc = 1;
  1381. };
  1382. makeWithData(ww, hh, cc);
  1383. int i,j;
  1384. i = 0;
  1385. for( cortexImageList::iterator index = imgList->begin(); index < imgList->end(); index++ ){
  1386. int w_offset = i*cw;
  1387. //(*index)->normalize();
  1388. if(cc == 3 && color){
  1389. embed((*index), w_offset, 0);
  1390. } else {
  1391. for (j = 0; j < (*index)->c; ++j){
  1392. int h_offset = j*(size+border);
  1393. cortexImage *layer;
  1394. layer = (*index)->getLayer(j);
  1395. embed(layer, w_offset, h_offset);
  1396. layer->freeData();
  1397. delete(layer);
  1398. };
  1399. };
  1400. };
  1401. };