class.c
Upload User: shhuayu888
Upload Date: 2013-03-28
Package Size: 1788k
Code Size: 7k
Category:

Windows Develop

Development Platform:

Unix_Linux

  1. /****************************************************************************
  2.  File Name  : class.c
  3.  Purpose    : provides algorithm for contour detection and classification
  4.  Release    : Version 1.0
  5.  Date     : Aug 31,1995
  6. GSNAKE API is jointly developed by the Information Technology Institute (ITI), Singapore, and the School of Applied Science, Nanyang Technological
  7. University (NTU), Singapore. 
  8. These software programs are available to the user without any license or royalty fees. Permission is hereby granted to use, copy, modify, and distribute this software and its documentation for any purpose. ITI and NTU gives no warranty, express, implied, or statuary for the software and/or documentation provided, including, without limitation, waranty of merchantibility and warranty of fitness for a particular purpose. The software provided hereunder is on an "as is"  basis, and ITI and NTU has no obligation to provide maintenance, support, updates, enhancements, or modifications.
  9. GSNAKE API is available for any UNIX compatible system. User feedback, bugs, or software and manual suggestions should be sent via electronic mail to one of the following, who may or may not act on them as he/she desires :
  10. asschan@ntu.ac.sg
  11. kflai@iti.gov.sg
  12. ***************************************************************************/
  13. #include "gsnake.h"
  14. CLASSIFY::CLASSIFY(EEXTTYPE _Eexttype)
  15. {
  16. Eexttype = _Eexttype ;
  17. numClass = 0 ;
  18. templates = NULL ;
  19. labels = NULL ;
  20. Deform_Prob = Margin_Prob = Rigid_Match = Deform_Match = NULL ;
  21. Qx = 3, Qy = 3, NR = Nrxy = Nt = Ndx = Ndy = 1 ;
  22. QR = 0.25, Qrxy = 0.25, Qt = RADIAN(10), Qdx = 0.1, Qdy = 0.1 ;
  23. R = 1 ; THETA = 0 ;
  24. numSearchSegment = 5 ;
  25. segmentSpacing = 5 ;
  26. numLevel = 1 ;
  27. verbose = 0 ;
  28. nhoodTangent = 3, nhoodNormal = 3 ;
  29. }
  30. CLASSIFY::~CLASSIFY( void )
  31. {
  32. register short i;
  33. for( i = 0; i< numClass; i++ ) {
  34. if ( templates[i] ) delete templates[i];
  35. if ( labels[i] ) _iMemFree(labels[i]) ;
  36. }
  37. _iMemFree( Margin_Prob ) ;
  38. _iMemFree( Deform_Prob );
  39. _iMemFree( Rigid_Match );
  40. _iMemFree( Deform_Match);
  41. _iMemFree( templates );
  42. _iMemFree( labels ) ;
  43. }
  44. /*
  45.  * memory allocation
  46.  */
  47. int CLASSIFY::init( void )
  48. {
  49. templates = (CONTOUR **)_iMemRealloc( templates, 
  50.    (numClass+1)*sizeof(CONTOUR *) );
  51. labels = (char **)_iMemRealloc( labels, 
  52.    (numClass+1)*sizeof(char *) );
  53. Margin_Prob = (double *)_iMemRealloc( Margin_Prob, 
  54. (numClass+1)*sizeof(double));
  55. Deform_Prob  = (double *)_iMemRealloc( Deform_Prob, 
  56. (numClass+1)*sizeof(double));
  57. Rigid_Match = (double *)_iMemRealloc( Rigid_Match, 
  58. (numClass+1)*sizeof(double) );
  59. Deform_Match = (double *)_iMemRealloc( Deform_Match, 
  60. (numClass+1)*sizeof(double) );
  61. if ( !templates || !labels || !Margin_Prob || !Deform_Prob || 
  62.      !Rigid_Match || !Deform_Match )
  63. return MEMORYERROR;
  64. templates[numClass] = NULL ; labels[numClass] = NULL ;
  65. Margin_Prob[numClass] = Deform_Prob[numClass] = 
  66. Rigid_Match[numClass] = Deform_Match[numClass] = 0 ;
  67. return NOERROR;
  68. }
  69.  
  70. /*
  71.  * read a template from structure
  72.  */
  73. int CLASSIFY::read( CONTOUR *contour )
  74. {
  75. if ( init() == MEMORYERROR ) {
  76. fprintf(stderr, "<CLASSIFY::init> : memory allocation errorn");
  77.                 return MEMORYERROR;
  78.         }
  79. templates[numClass] = contour->duplicate();
  80. /* increase contour # */
  81. numClass++;
  82. return NOERROR;
  83. }
  84. /*
  85.  * read a template from file
  86.  */
  87. int CLASSIFY::read( char *filename )
  88. {
  89. if ( ( init() == MEMORYERROR ) ||
  90.     !( templates[numClass] = new CONTOUR ) ) {
  91. fprintf(stderr, "<CLASSIFY::init> : memory allocation errorn");
  92.                 return MEMORYERROR;
  93.         }
  94. templates[numClass]->CONTOUR::read( filename );
  95. labels[numClass] = strtok(strdup(filename), ".") ;
  96. /* increase contour # */
  97. numClass++;
  98. return NOERROR;
  99. }
  100.  
  101. /*
  102.  * perform classification
  103.  */
  104. int CLASSIFY::classify(IMAGE *testimg, CLASSTYPE ClassType)
  105. {
  106. GSNAKE gsnake(Eexttype, _LOCAL_LAMBDA) ;
  107. register short i ;
  108. double avg_sig_nu_sqr = 0 ;
  109. for(i=0; i<numClass; i++) /* compute average sigma nu */
  110. avg_sig_nu_sqr += templates[i]->getSigNuSqr() ;
  111. avg_sig_nu_sqr /= numClass ;
  112. gsnake.putRawImg(testimg) ;
  113. gsnake.genPyramid(numLevel, verbose) ;
  114. for(i=0; i<numClass; i++) {
  115.     templates[i]->duplicate(&gsnake) ;
  116.     
  117.     gsnake.putSigNuSqr(avg_sig_nu_sqr) ;
  118.           
  119.     gsnake.localize(Qx, Qy, NR, QR, Nrxy, Qrxy, Nt, Qt, Ndx, Qdx,
  120.      Ndy, Qdy, R, THETA) ; 
  121.     gsnake.ESnake(0) ;
  122.     Rigid_Match[i] = -gsnake.getEext() ; 
  123.     gsnake.minimize(segmentSpacing, numSearchSegment, 0, 1, verbose) ;
  124.     gsnake.ESnake(0) ;
  125.     Deform_Match[i] = -gsnake.getEext() ;
  126.     
  127.     Deform_Prob[i] = 
  128.  exp( (1 - gsnake.getEsnake()) * gsnake.getNumSnaxel() /
  129.       avg_sig_nu_sqr ) / gsnake.getZ() ;
  130.     Margin_Prob[i] = gsnake.marginalize(nhoodTangent, nhoodNormal) ;
  131. }
  132. return selectMax(ClassType) ;
  133. }
  134. /*
  135.  * find the maximum value for type 
  136.  */
  137. int CLASSIFY::selectMax( CLASSTYPE ClassType )
  138. {
  139. short register i, max_index;
  140. double max_score = -1000;
  141. double *score;
  142. switch( ClassType ) {
  143. case _MARGIN_PROB :
  144. score = Margin_Prob ;
  145. break ;
  146. case _DEFORM_PROB :
  147. score = Deform_Prob;
  148. break;
  149. case _RIGID_MATCH :
  150. score = Rigid_Match;
  151. break;
  152. case _DEFORM_MATCH :
  153. score = Deform_Match;
  154. }
  155. /* find maximum score */
  156. for ( i = 0; i < numClass; i++ ) {
  157. if ( score[i] > max_score ) {
  158. max_index = i;
  159. max_score = score[i];
  160. }
  161. }
  162. return max_index;
  163. }
  164. /*
  165.  * get the desire template score
  166.  */
  167. double CLASSIFY::getScore( int template_id, /* template identifier */
  168.   CLASSTYPE ClassType ) /* classmethod */
  169. {
  170. double score;
  171. switch( ClassType ) {
  172. case _MARGIN_PROB :
  173. score = Margin_Prob[template_id];
  174. break;
  175. case _DEFORM_PROB :
  176. score = Deform_Prob[template_id];
  177. break;
  178. case _RIGID_MATCH :
  179. score = Rigid_Match[template_id];
  180. break;
  181. case _DEFORM_MATCH :
  182. score = Deform_Match[template_id];
  183. break;
  184. }
  185. return score;
  186. }
  187. char *CLASSIFY::getLabel(short class_id)
  188. {
  189. if(labels && labels[class_id]) return labels[class_id] ;
  190. }
  191. void CLASSIFY::dump(char *imgname, FILE *stream)
  192. {
  193. register int i ;
  194. fprintf(stream, "nClassification Results for Image <%s> :n", imgname);
  195. fprintf(stream, "Rigid Match : nt");
  196. for(i=0; i<numClass; i++) 
  197.   fprintf(stream, "%s : %et", getLabel(i), getScore(i, _RIGID_MATCH));
  198. fprintf(stream, "ntResults : %sn", 
  199. getLabel(selectMax(_RIGID_MATCH)));
  200. fprintf(stream, "Deform Match : nt");
  201. for(i=0; i<numClass; i++) 
  202.   fprintf(stream, "%s : %et", getLabel(i), getScore(i, _DEFORM_MATCH));
  203. fprintf(stream, "ntResults : %sn", 
  204. getLabel(selectMax(_DEFORM_MATCH)));
  205. fprintf(stream, "Deform Prob : nt");
  206. for(i=0; i<numClass; i++) 
  207.    fprintf(stream, "%s : %et", getLabel(i), getScore(i, _DEFORM_PROB));
  208. fprintf(stream, "ntResults : %sn", 
  209. getLabel(selectMax(_DEFORM_PROB)));
  210. fprintf(stream, "Marginalized : nt");
  211. for(i=0; i<numClass; i++) 
  212.    fprintf(stream, "%s : %et", getLabel(i), getScore(i, _MARGIN_PROB));
  213. fprintf(stream, "ntResults : %sn", 
  214. getLabel(selectMax(_MARGIN_PROB)));
  215. fprintf(stream, "n");
  216. }