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.

1519 lines
36 KiB

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