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.

1538 lines
38 KiB

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