PageRenderTime 30ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/0.2.1/featureLBPHist.c

http://pam-face-authentication.googlecode.com/
C | 272 lines | 174 code | 16 blank | 82 comment | 18 complexity | 776f0644ad8ae0b7ba1dd47d7442c6d0 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
  1. #include "cv.h"
  2. #include "cvaux.h"
  3. #include "highgui.h"
  4. #include <math.h>
  5. int checkBit(int i)
  6. {
  7. // Well Removing patterns with bits changing consecutively more than twice
  8. int j=i;
  9. int bit8=(i%2);
  10. int bit7=((i/2)%2);
  11. int bit6=((i/4)%2);
  12. int bit5=((i/8)%2);
  13. int bit4=((i/16)%2);
  14. int bit3=((i/32)%2);
  15. int bit2=((i/64)%2);
  16. int bit1=((i/128)%2);
  17. int bitVector[9] = {bit1,bit8,bit7, bit6, bit5,bit4, bit3,bit2,bit1};
  18. int current=bitVector[0];
  19. int count=0;
  20. for (i=0;i<9;i++)
  21. {
  22. if (current!=bitVector[i])
  23. count++;
  24. current=bitVector[i];
  25. }
  26. if (count>2)
  27. return -1;
  28. else
  29. return 0;
  30. }
  31. double getBIT(IplImage* img,double px,double py,double threshold)
  32. {
  33. if (px<0 || py<0 || px>=img->width || py>=img->height)
  34. return 0;
  35. else
  36. {
  37. CvScalar s;
  38. s=cvGet2D(img,py,px);
  39. if (s.val[0]>=threshold)
  40. return 1;
  41. else
  42. return 0;
  43. }
  44. }
  45. void featureLBPSum(IplImage * img, double * val)
  46. {
  47. double sum=0;
  48. IplImage* imgLBP=cvCreateImage( cvSize(img->width,img->height), 8, img->nChannels );
  49. int i,j=0;
  50. cvZero(imgLBP);
  51. for (i=0;i<img->height;i++)
  52. {
  53. for (j=0;j<img->width;j++)
  54. {
  55. int p1x,p2x,p3x,p4x,p5x,p6x,p7x,p8x;
  56. int p1y,p2y,p3y,p4y,p5y,p6y,p7y,p8y;
  57. p1x=j-1;
  58. p1y=i-1;
  59. p2x=j;
  60. p2y=i-1;
  61. p3x=j+1;
  62. p3y=i-1;
  63. p4x=j+1;
  64. p4y=i;
  65. p5x=j+1;
  66. p5y=i+1;
  67. p6x=j;
  68. p6y=i+1;
  69. p7x=j-1;
  70. p7y=i+1;
  71. p8x=j-1;
  72. p8y=i;
  73. CvScalar s;
  74. s=cvGet2D(img,i,j);
  75. double bit1=128*getBIT(img,p1x,p1y,s.val[0]);
  76. double bit2=64*getBIT(img,p2x,p2y,s.val[0]);
  77. double bit3=32*getBIT(img,p3x,p3y,s.val[0]);
  78. double bit4=16*getBIT(img,p4x,p4y,s.val[0]);
  79. double bit5=8*getBIT(img,p5x,p5y,s.val[0]);
  80. double bit6=4*getBIT(img,p6x,p6y,s.val[0]);
  81. double bit7=2*getBIT(img,p7x,p7y,s.val[0]);
  82. double bit8=1*getBIT(img,p8x,p8y,s.val[0]);
  83. CvScalar s1;
  84. s1.val[0]=bit1+bit2+bit3+bit4+bit5+bit6+bit7+bit8;
  85. s1.val[1]=0;
  86. s1.val[2]=0;
  87. cvSet2D(imgLBP,i,j,s1);
  88. // printf("%e \n",s1.val[0]);
  89. sum+=s1.val[0];
  90. }
  91. }
  92. // printf("%e SUM \n ",sum);
  93. *val=sum;
  94. }
  95. void featureLBPHist(IplImage * img,float *features_final)
  96. {
  97. int lbpArry[256];
  98. IplImage* imgLBP=cvCreateImage( cvSize(img->width,img->height), 8, img->nChannels );
  99. int Nx = floor((img->width )/30);
  100. int Ny= floor((img->height)/20);
  101. int i,j=0;
  102. cvZero(imgLBP);
  103. for (i=0;i<img->height;i++)
  104. {
  105. for (j=0;j<img->width;j++)
  106. {
  107. int p1x,p2x,p3x,p4x,p5x,p6x,p7x,p8x;
  108. int p1y,p2y,p3y,p4y,p5y,p6y,p7y,p8y;
  109. p1x=j-1;
  110. p1y=i-1;
  111. p2x=j;
  112. p2y=i-1;
  113. p3x=j+1;
  114. p3y=i-1;
  115. p4x=j+1;
  116. p4y=i;
  117. p5x=j+1;
  118. p5y=i+1;
  119. p6x=j;
  120. p6y=i+1;
  121. p7x=j-1;
  122. p7y=i+1;
  123. p8x=j-1;
  124. p8y=i;
  125. CvScalar s;
  126. s=cvGet2D(img,i,j);
  127. double bit1=128*getBIT(img,p1x,p1y,s.val[0]);
  128. double bit2=64*getBIT(img,p2x,p2y,s.val[0]);
  129. double bit3=32*getBIT(img,p3x,p3y,s.val[0]);
  130. double bit4=16*getBIT(img,p4x,p4y,s.val[0]);
  131. double bit5=8*getBIT(img,p5x,p5y,s.val[0]);
  132. double bit6=4*getBIT(img,p6x,p6y,s.val[0]);
  133. double bit7=2*getBIT(img,p7x,p7y,s.val[0]);
  134. double bit8=1*getBIT(img,p8x,p8y,s.val[0]);
  135. CvScalar s1;
  136. s1.val[0]=bit1+bit2+bit3+bit4+bit5+bit6+bit7+bit8;
  137. s1.val[1]=0;
  138. s1.val[2]=0;
  139. cvSet2D(imgLBP,i,j,s1);
  140. }
  141. }
  142. int l,m,k=0;
  143. for (i=0;i<Ny;i++)
  144. {
  145. for (j=0;j<Nx;j++)
  146. {
  147. int startX=30*j;
  148. int startY=20*i;
  149. int count=0;
  150. for (k=0;k<256;k++)
  151. {
  152. if(checkBit(k)==0)
  153. {
  154. lbpArry[k]=count;
  155. features_final[i*Nx*59 + j*59 +count]=0;
  156. count++;
  157. }
  158. else
  159. {
  160. lbpArry[k]=58;
  161. features_final[i*Nx*59 + j*59 +58]=0;
  162. }
  163. }
  164. //printf("%d \n",count);
  165. for (l=0;l<20;l++)
  166. {
  167. for (m=0;m<30;m++)
  168. {
  169. CvScalar s;
  170. s=cvGet2D(imgLBP,startY+l,startX+m);
  171. int val=s.val[0];
  172. features_final[i*Nx*59 + j*59 +lbpArry[val]]++;
  173. }
  174. }
  175. }
  176. }
  177. cvReleaseImage( &imgLBP);
  178. }
  179. /*
  180. void createLBP(char * fullpath1,char * fullpath2)
  181. {
  182. printf("%s %s \n",fullpath1,fullpath2);
  183. IplImage* img=cvLoadImage(fullpath1,0);
  184. IplImage* imgLBP=cvCreateImage( cvSize(img->width,img->height), 8, img->nChannels );
  185. int Nx = floor((img->width - 10)/10);
  186. int Ny= floor((img->height - 10)/10);
  187. int i,j=0;
  188. cvZero(imgLBP);
  189. for (i=0;i<img->height;i++)
  190. {
  191. for (j=0;j<img->width;j++)
  192. {
  193. int p1x,p2x,p3x,p4x,p5x,p6x,p7x,p8x;
  194. int p1y,p2y,p3y,p4y,p5y,p6y,p7y,p8y;
  195. p1x=j-1;
  196. p1y=i-1;
  197. p2x=j;
  198. p2y=i-1;
  199. p3x=j+1;
  200. p3y=i-1;
  201. p4x=j+1;
  202. p4y=i;
  203. p5x=j+1;
  204. p5y=i+1;
  205. p6x=j;
  206. p6y=i+1;
  207. p7x=j-1;
  208. p7y=i+1;
  209. p8x=j-1;
  210. p8y=i;
  211. CvScalar s;
  212. s=cvGet2D(img,i,j);
  213. double bit1=128*getBIT(img,p1x,p1y,s.val[0]);
  214. double bit2=64*getBIT(img,p2x,p2y,s.val[0]);
  215. double bit3=32*getBIT(img,p3x,p3y,s.val[0]);
  216. double bit4=16*getBIT(img,p4x,p4y,s.val[0]);
  217. double bit5=8*getBIT(img,p5x,p5y,s.val[0]);
  218. double bit6=4*getBIT(img,p6x,p6y,s.val[0]);
  219. double bit7=2*getBIT(img,p7x,p7y,s.val[0]);
  220. double bit8=1*getBIT(img,p8x,p8y,s.val[0]);
  221. CvScalar s1;
  222. s1.val[0]=bit1+bit2+bit3+bit4+bit5+bit6+bit7+bit8;
  223. s1.val[1]=0;
  224. s1.val[2]=0;
  225. cvSet2D(imgLBP,i,j,s1);
  226. }
  227. }
  228. cvSaveImage(fullpath2,imgLBP);
  229. cvReleaseImage( &img);
  230. cvReleaseImage( &imgLBP);
  231. }
  232. /*
  233. void main()
  234. {
  235. IplImage * img = cvLoadImage("abc.jpg",0);
  236. int Nx = floor((img->width - 10)/10);
  237. int Ny= floor((img->height - 10)/10);
  238. float* features = (float*)malloc( Nx*Ny * 59 * sizeof(float) );
  239. featureLBPHist(img,features);
  240. //cvNamedWindow( "Source", 1 );
  241. //cvShowImage( "Source", img );
  242. cvWaitKey(0);
  243. }
  244. */