PageRenderTime 552ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/VideoLoad/MYmaska.cpp

https://github.com/straiki/VisionCup-Deep-Team
C++ | 322 lines | 213 code | 67 blank | 42 comment | 34 complexity | 5aea29310fa677281b10d890c9ceed61 MD5 | raw file
  1. #include "MYmaska.h"
  2. #include "MYoblicej.h"
  3. #include "stdhead.h"
  4. #include "MYvideo.h"
  5. using namespace std;
  6. //konstruktor
  7. MYmaska::MYmaska(IplImage *frame){
  8. cout << "vytvorena maska" << endl;
  9. this->frame = frame;
  10. }
  11. //destruktor
  12. MYmaska::~MYmaska(){
  13. cout << "znicena maska" << endl;
  14. if(this->source) cvReleaseImage(&this->source);
  15. if(this->edited) cvReleaseImage(&this->edited);
  16. if(this->rotated) cvReleaseImage(&this->rotated);
  17. if(this->mask) cvReleaseImage(&this->mask);
  18. if(this->mask2) cvReleaseImage(&this->mask2);
  19. if(this->mask3) cvReleaseImage(&this->mask3);
  20. }
  21. void MYmaska::open(string name){
  22. this->source = cvLoadImage(name.c_str());
  23. }
  24. void MYmaska::open_mask(string name){
  25. this->mask = cvLoadImage(name.c_str());
  26. }
  27. void MYmaska::changeSize(float size){
  28. cout << size << endl;
  29. //zmena originalu
  30. this->edited = cvCreateImage(cvSize((int)(this->source->width*size),
  31. (int)(this->source->height*size)),
  32. source->depth,
  33. source->nChannels);
  34. cvResize(this->source, this->edited);
  35. //zmena masky
  36. this->mask2 = cvCreateImage(cvSize((int)(this->mask->width*size),
  37. (int)(this->mask->height*size)),
  38. IPL_DEPTH_8U,
  39. mask->nChannels);
  40. cvResize(this->mask, this->mask2);
  41. }
  42. // Rotate the image clockwise (or counter-clockwise if negative).
  43. // Remember to free the returned image.
  44. void MYmaska::rotateImage(float angleRadians)
  45. {
  46. // Create a map_matrix, where the left 2x2 matrix
  47. // is the transform and the right 2x1 is the dimensions.
  48. float m[6];
  49. CvMat M = cvMat(2, 3, CV_32F, m);
  50. int w = this->edited->width;
  51. int h = this->edited->height;
  52. m[0] = (float)( cos(angleRadians) );
  53. m[1] = (float)( sin(angleRadians) );
  54. m[3] = -m[1];
  55. m[4] = m[0];
  56. m[2] = w*0.5f;
  57. m[5] = h*0.5f;
  58. // Make a spare image for the result
  59. CvSize sizeRotated;
  60. sizeRotated.width = cvRound(MAX(w,h));
  61. sizeRotated.height = cvRound(MAX(w,h));
  62. // Rotate
  63. this->rotated = cvCreateImage(sizeRotated,
  64. this->edited->depth,
  65. this->edited->nChannels );
  66. this->mask3 = cvCreateImage(sizeRotated,
  67. this->mask2->depth,
  68. this->mask2->nChannels );
  69. // Transform the image
  70. cvGetQuadrangleSubPix( this->edited, this->rotated, &M);
  71. cvGetQuadrangleSubPix( this->mask2, this->mask3, &M);
  72. }
  73. void MYmaska::vytvorKnirek(MYoblicej *oblicej){
  74. this->oblicej = oblicej;
  75. this->open("../masks/mustache.png");
  76. this->open_mask("../masks/mustache_mask.png");
  77. float velikost = ((oblicej->sirka)*0.9)/(source->width*1.0);
  78. this->changeSize(velikost);
  79. this->rotateImage(this->oblicej->uhel);
  80. }
  81. void MYmaska::vytvorKaju(MYoblicej *oblicej){
  82. this->oblicej = oblicej;
  83. this->open("../masks/gott.png");
  84. this->open_mask("../masks/gott_mask2.png");
  85. float velikost = ((oblicej->sirka)*1.5)/(source->width*1.0);
  86. this->changeSize(velikost);
  87. this->rotateImage(this->oblicej->uhel);
  88. }
  89. void MYmaska::vytvorKlobouk(MYoblicej *oblicej){
  90. this->oblicej = oblicej;
  91. this->open("../masks/klobouk.png");
  92. this->open_mask("../masks/klobouk_mask.png");
  93. float velikost = ((oblicej->sirka)*1.5)/(source->width*1.0);
  94. this->changeSize(velikost);
  95. this->rotateImage(this->oblicej->uhel);
  96. }
  97. void MYmaska::vytvorPusu(MYoblicej * oblic){
  98. static int i = 1;
  99. this->oblicej = oblic;
  100. MYvideo *prd = new MYvideo();
  101. prd->open("../masks/pusa.avi");
  102. this->open_mask("../masks/pusa_mask.png");
  103. IplImage * akt;
  104. for(int j = 0; j < i ; j++){
  105. akt = prd->next_frame();
  106. if( i >= 100) i = 1;
  107. }
  108. this->source = cvCreateImage(cvGetSize(akt),
  109. akt->depth,
  110. akt->nChannels);
  111. cvCopy(akt,this->source);
  112. cout << "ok" << endl;
  113. i+=5;
  114. float velikost = ((oblicej->sirka)*0.9)/(source->width*1.0);
  115. this->changeSize(velikost);
  116. this->rotateImage(this->oblicej->uhel);
  117. delete prd;
  118. //cvReleaseImage(&akt);
  119. }
  120. uchar MYmaska::interpolate(int barva2, int barva, int krok, int pocet_kroku){
  121. // return (krok % 2) ? (barva) : (barva2);
  122. //if(barva < barva2){
  123. return (uchar)(((float)(barva2 - barva)*(1 - krok/(float)pocet_kroku)) + (float)barva);
  124. //}else{
  125. // return ((barva - barva2)*((krok/(float)pocet_kroku))) + barva2;
  126. //}
  127. }
  128. void MYmaska::skryjOci(MYoblicej *oblicej){
  129. int prodluz = 30;
  130. this->oblicej = oblicej;
  131. this->open("../masks/oko.png"); //this->source = cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U, 3);
  132. this->open_mask("../masks/oko_mask.png");
  133. this->changeSize(0.1);
  134. this->rotateImage(this->oblicej->uhel);
  135. //this->mask = cvCreateImage(cvSize(img->width,img->height),IPL_DEPTH_8U, 3);
  136. //cvCopy(img, this->mask);
  137. // naalokovan prostor
  138. // CvRect rect = cvRect(oblicej->leve_oko_x + oblicej->sour_x - sirka/2, oblicej->leve_oko_y + oblicej->sour_y - vyska/2, sirka, vyska);
  139. CvRect rect = this->oblicej->Leye;
  140. rect.x -= prodluz/2;
  141. rect.y -= prodluz/2;
  142. rect.width = this->oblicej->Leye.width+prodluz;
  143. rect.height = this->oblicej->Leye.height+prodluz;
  144. cvSetImageROI(this->frame, rect);//jsem na levem oku
  145. int k;
  146. for(int i = rect.x; i < (rect.x + rect.width); i++ ){
  147. int bod1B = CV_IMAGE_ELEM( this->frame, uchar, rect.y, (i)*3);//+rect.height-1
  148. int bod1G = CV_IMAGE_ELEM( this->frame, uchar, rect.y, (i)*3+1);
  149. int bod1R = CV_IMAGE_ELEM( this->frame, uchar, rect.y, (i)*3+2);
  150. int bod2B = CV_IMAGE_ELEM( this->frame, uchar, rect.y+rect.height-1, (i)*3);//
  151. int bod2G = CV_IMAGE_ELEM( this->frame, uchar, rect.y+rect.height-1, (i)*3+1);
  152. int bod2R = CV_IMAGE_ELEM( this->frame, uchar, rect.y+rect.height-1, (i)*3+2);
  153. k=0;
  154. for(int j = rect.y; j < (rect.y + rect.height); j++){
  155. CV_IMAGE_ELEM( this->frame, uchar, j, (i)*3) = this->interpolate(bod1B,bod2B,k,rect.height); //b
  156. CV_IMAGE_ELEM( this->frame, uchar, j, (i)*3+1) = this->interpolate(bod1G,bod2G,k,rect.height); //g
  157. CV_IMAGE_ELEM( this->frame, uchar, j, (i)*3+2) = this->interpolate(bod1R,bod2R,k,rect.height); //r
  158. k++;
  159. }
  160. }
  161. cvResetImageROI(this->frame);
  162. rect = this->oblicej->Reye;
  163. rect.x -= prodluz/2;
  164. rect.y -= prodluz/2;
  165. rect.width = this->oblicej->Reye.width+prodluz;
  166. rect.height = this->oblicej->Reye.height+prodluz;
  167. cvSetImageROI(this->frame, rect);//jsem na levem oku
  168. for(int i = rect.x; i < (rect.x + rect.width); i++ ){
  169. int bod1B = CV_IMAGE_ELEM( this->frame, uchar, rect.y, (i)*3);//+rect.height-1
  170. int bod1G = CV_IMAGE_ELEM( this->frame, uchar, rect.y, (i)*3+1);
  171. int bod1R = CV_IMAGE_ELEM( this->frame, uchar, rect.y, (i)*3+2);
  172. int bod2B = CV_IMAGE_ELEM( this->frame, uchar, rect.y+rect.height-1, (i)*3);//
  173. int bod2G = CV_IMAGE_ELEM( this->frame, uchar, rect.y+rect.height-1, (i)*3+1);
  174. int bod2R = CV_IMAGE_ELEM( this->frame, uchar, rect.y+rect.height-1, (i)*3+2);
  175. k=0;
  176. for(int j = rect.y; j < (rect.y + rect.height); j++){
  177. CV_IMAGE_ELEM( this->frame, uchar, j, (i)*3) = this->interpolate(bod1B,bod2B,k,rect.height); //b
  178. CV_IMAGE_ELEM( this->frame, uchar, j, (i)*3+1) = this->interpolate(bod1G,bod2G,k,rect.height); //g
  179. CV_IMAGE_ELEM( this->frame, uchar, j, (i)*3+2) = this->interpolate(bod1R,bod2R,k,rect.height); //r
  180. k++;
  181. }
  182. }
  183. cvResetImageROI(this->frame);
  184. }
  185. IplImage* MYmaska::addMask(IplImage *frame,int typ){
  186. // cvSetImageROI(frame, cvRect(mezi_oci_x - mask->rotated->width/2,
  187. // mezi_oci_y - mask->rotated->height/2));
  188. int start_y = 0;
  189. int start_x = 0;
  190. const int Thres = 100;
  191. const int HT = 50;
  192. int x,y,i,j;
  193. if(typ == KNIR){
  194. // start_y = this->oblicej->sour_y + this->oblicej->knirek_y - this->rotated->height/2;
  195. // start_x = this->oblicej->sour_x + this->oblicej->knirek_x - this->rotated->width/2;
  196. start_x = this->oblicej->Pmoust.x - this->rotated->height/2;
  197. start_y = this->oblicej->Pmoust.y - this->rotated->height/2;
  198. }
  199. else if(typ == PUSA){
  200. // start_y = this->oblicej->sour_y + this->oblicej->pusa_y - this->rotated->height/2;
  201. // start_x = this->oblicej->sour_x + this->oblicej->pusa_x - this->rotated->width/2;
  202. start_x = this->oblicej->Pmoust.x - this->rotated->height/2;
  203. start_y = this->oblicej->Pmoust.y - this->rotated->height/2;
  204. }
  205. else if(typ == KAJA){
  206. start_y = this->oblicej->Pkaja.y - this->rotated->height/2;
  207. start_x = this->oblicej->Pkaja.x - this->rotated->width/2;
  208. }
  209. else if(typ == KLOBOUK){
  210. // start_y = this->oblicej->sour_y - 45 - this->rotated->height/2;
  211. // start_x = this->oblicej->sour_x -- this->rotated->height/2;
  212. start_x = this->oblicej->Phead.x - this->rotated->height/2;
  213. start_y = this->oblicej->Phead.y - this->rotated->height/2;
  214. }
  215. else if(typ == OCI){
  216. start_x = this->oblicej->Peyes.x - this->rotated->height/2;
  217. start_y = this->oblicej->Peyes.y - this->rotated->height/2;
  218. }
  219. for (i = start_y; i < start_y + this->rotated->height; i++) {
  220. for (j = start_x; j < start_x + this->rotated->width; j++) {
  221. float vaha_r = 1.0-(CV_IMAGE_ELEM( this->mask3, uchar, i - start_y, (j - start_x)*3+0)/255.0);
  222. float vaha_g = 1.0-(CV_IMAGE_ELEM( this->mask3, uchar, i - start_y, (j - start_x)*3+1)/255.0);
  223. float vaha_b = 1.0-(CV_IMAGE_ELEM( this->mask3, uchar, i - start_y, (j - start_x)*3+2)/255.0);
  224. //cout << "vaha: " << vaha_r << endl;
  225. int barva_r = 0;
  226. int barva_g = 0;
  227. int barva_b = 0;
  228. barva_r = vaha_r*CV_IMAGE_ELEM( this->rotated, uchar, i - start_y, (j - start_x)*3+0) + (1.0-vaha_r)*CV_IMAGE_ELEM( frame, uchar, i, j*3+0);
  229. barva_g = vaha_g*CV_IMAGE_ELEM( this->rotated, uchar, i - start_y, (j - start_x)*3+1) + (1.0-vaha_g)*CV_IMAGE_ELEM( frame, uchar, i, j*3+1);
  230. barva_b = vaha_b*CV_IMAGE_ELEM( this->rotated, uchar, i - start_y, (j - start_x)*3+2) + (1.0-vaha_b)*CV_IMAGE_ELEM( frame, uchar, i, j*3+2);
  231. if(barva_r>255) barva_r = 255;
  232. if(barva_g>255) barva_g = 255;
  233. if(barva_b>255) barva_b = 255;
  234. if(barva_r<0) barva_r = 0;
  235. if(barva_g<0) barva_g = 0;
  236. if(barva_b<0) barva_b = 0;
  237. ((uchar*)(frame->imageData + i * frame->widthStep))[j*3+0] = (uchar)barva_r;
  238. ((uchar*)(frame->imageData + i * frame->widthStep))[j*3+1] = (uchar)barva_g;
  239. ((uchar*)(frame->imageData + i * frame->widthStep))[j*3+2] = (uchar)barva_b;
  240. // if(CV_IMAGE_ELEM( mask->mask3, uchar, i - start_y, (j - start_x)*3) < Thres &&
  241. // CV_IMAGE_ELEM( mask->mask3, uchar, i - start_y, (j - start_x)*3+1) > 255-HT &&
  242. // CV_IMAGE_ELEM( mask->mask3, uchar, i - start_y, (j - start_x)*3+2) < Thres) {
  243. //
  244. // continue;
  245. // }
  246. // else{
  247. // ((uchar*)(frame->imageData + i * frame->widthStep))[j*3] = CV_IMAGE_ELEM( mask->rotated, uchar, i - start_y, (j - start_x)*3); //b
  248. // ((uchar*)(frame->imageData + i * frame->widthStep))[j*3+1] = CV_IMAGE_ELEM( mask->rotated, uchar, i - start_y, (j - start_x)*3+1); //g
  249. // ((uchar*)(frame->imageData + i * frame->widthStep))[j*3+2] = CV_IMAGE_ELEM( mask->rotated, uchar, i - start_y, (j - start_x)*3+2); //r
  250. //
  251. // }
  252. }
  253. }
  254. return frame;
  255. }