📄 class.c
字号:
/****************************************************************************
File Name : class.c
Purpose : provides algorithm for contour detection and classification
Release : Version 1.0
Date : Aug 31,1995
GSNAKE API is jointly developed by the Information Technology Institute (ITI), Singapore, and the School of Applied Science, Nanyang Technological
University (NTU), Singapore.
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.
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 :
asschan@ntu.ac.sg
kflai@iti.gov.sg
***************************************************************************/
#include "gsnake.h"
CLASSIFY::CLASSIFY(EEXTTYPE _Eexttype)
{
Eexttype = _Eexttype ;
numClass = 0 ;
templates = NULL ;
labels = NULL ;
Deform_Prob = Margin_Prob = Rigid_Match = Deform_Match = NULL ;
Qx = 3, Qy = 3, NR = Nrxy = Nt = Ndx = Ndy = 1 ;
QR = 0.25, Qrxy = 0.25, Qt = RADIAN(10), Qdx = 0.1, Qdy = 0.1 ;
R = 1 ; THETA = 0 ;
numSearchSegment = 5 ;
segmentSpacing = 5 ;
numLevel = 1 ;
verbose = 0 ;
nhoodTangent = 3, nhoodNormal = 3 ;
}
CLASSIFY::~CLASSIFY( void )
{
register short i;
for( i = 0; i< numClass; i++ ) {
if ( templates[i] ) delete templates[i];
if ( labels[i] ) _iMemFree(labels[i]) ;
}
_iMemFree( Margin_Prob ) ;
_iMemFree( Deform_Prob );
_iMemFree( Rigid_Match );
_iMemFree( Deform_Match);
_iMemFree( templates );
_iMemFree( labels ) ;
}
/*
* memory allocation
*/
int CLASSIFY::init( void )
{
templates = (CONTOUR **)_iMemRealloc( templates,
(numClass+1)*sizeof(CONTOUR *) );
labels = (char **)_iMemRealloc( labels,
(numClass+1)*sizeof(char *) );
Margin_Prob = (double *)_iMemRealloc( Margin_Prob,
(numClass+1)*sizeof(double));
Deform_Prob = (double *)_iMemRealloc( Deform_Prob,
(numClass+1)*sizeof(double));
Rigid_Match = (double *)_iMemRealloc( Rigid_Match,
(numClass+1)*sizeof(double) );
Deform_Match = (double *)_iMemRealloc( Deform_Match,
(numClass+1)*sizeof(double) );
if ( !templates || !labels || !Margin_Prob || !Deform_Prob ||
!Rigid_Match || !Deform_Match )
return MEMORYERROR;
templates[numClass] = NULL ; labels[numClass] = NULL ;
Margin_Prob[numClass] = Deform_Prob[numClass] =
Rigid_Match[numClass] = Deform_Match[numClass] = 0 ;
return NOERROR;
}
/*
* read a template from structure
*/
int CLASSIFY::read( CONTOUR *contour )
{
if ( init() == MEMORYERROR ) {
fprintf(stderr, "<CLASSIFY::init> : memory allocation error\n");
return MEMORYERROR;
}
templates[numClass] = contour->duplicate();
/* increase contour # */
numClass++;
return NOERROR;
}
/*
* read a template from file
*/
int CLASSIFY::read( char *filename )
{
if ( ( init() == MEMORYERROR ) ||
!( templates[numClass] = new CONTOUR ) ) {
fprintf(stderr, "<CLASSIFY::init> : memory allocation error\n");
return MEMORYERROR;
}
templates[numClass]->CONTOUR::read( filename );
labels[numClass] = strtok(strdup(filename), ".") ;
/* increase contour # */
numClass++;
return NOERROR;
}
/*
* perform classification
*/
int CLASSIFY::classify(IMAGE *testimg, CLASSTYPE ClassType)
{
GSNAKE gsnake(Eexttype, _LOCAL_LAMBDA) ;
register short i ;
double avg_sig_nu_sqr = 0 ;
for(i=0; i<numClass; i++) /* compute average sigma nu */
avg_sig_nu_sqr += templates[i]->getSigNuSqr() ;
avg_sig_nu_sqr /= numClass ;
gsnake.putRawImg(testimg) ;
gsnake.genPyramid(numLevel, verbose) ;
for(i=0; i<numClass; i++) {
templates[i]->duplicate(&gsnake) ;
gsnake.putSigNuSqr(avg_sig_nu_sqr) ;
gsnake.localize(Qx, Qy, NR, QR, Nrxy, Qrxy, Nt, Qt, Ndx, Qdx,
Ndy, Qdy, R, THETA) ;
gsnake.ESnake(0) ;
Rigid_Match[i] = -gsnake.getEext() ;
gsnake.minimize(segmentSpacing, numSearchSegment, 0, 1, verbose) ;
gsnake.ESnake(0) ;
Deform_Match[i] = -gsnake.getEext() ;
Deform_Prob[i] =
exp( (1 - gsnake.getEsnake()) * gsnake.getNumSnaxel() /
avg_sig_nu_sqr ) / gsnake.getZ() ;
Margin_Prob[i] = gsnake.marginalize(nhoodTangent, nhoodNormal) ;
}
return selectMax(ClassType) ;
}
/*
* find the maximum value for type
*/
int CLASSIFY::selectMax( CLASSTYPE ClassType )
{
short register i, max_index;
double max_score = -1000;
double *score;
switch( ClassType ) {
case _MARGIN_PROB :
score = Margin_Prob ;
break ;
case _DEFORM_PROB :
score = Deform_Prob;
break;
case _RIGID_MATCH :
score = Rigid_Match;
break;
case _DEFORM_MATCH :
score = Deform_Match;
}
/* find maximum score */
for ( i = 0; i < numClass; i++ ) {
if ( score[i] > max_score ) {
max_index = i;
max_score = score[i];
}
}
return max_index;
}
/*
* get the desire template score
*/
double CLASSIFY::getScore( int template_id, /* template identifier */
CLASSTYPE ClassType ) /* classmethod */
{
double score;
switch( ClassType ) {
case _MARGIN_PROB :
score = Margin_Prob[template_id];
break;
case _DEFORM_PROB :
score = Deform_Prob[template_id];
break;
case _RIGID_MATCH :
score = Rigid_Match[template_id];
break;
case _DEFORM_MATCH :
score = Deform_Match[template_id];
break;
}
return score;
}
char *CLASSIFY::getLabel(short class_id)
{
if(labels && labels[class_id]) return labels[class_id] ;
}
void CLASSIFY::dump(char *imgname, FILE *stream)
{
register int i ;
fprintf(stream, "\nClassification Results for Image <%s> :\n", imgname);
fprintf(stream, "Rigid Match : \n\t");
for(i=0; i<numClass; i++)
fprintf(stream, "%s : %e\t", getLabel(i), getScore(i, _RIGID_MATCH));
fprintf(stream, "\n\tResults : %s\n",
getLabel(selectMax(_RIGID_MATCH)));
fprintf(stream, "Deform Match : \n\t");
for(i=0; i<numClass; i++)
fprintf(stream, "%s : %e\t", getLabel(i), getScore(i, _DEFORM_MATCH));
fprintf(stream, "\n\tResults : %s\n",
getLabel(selectMax(_DEFORM_MATCH)));
fprintf(stream, "Deform Prob : \n\t");
for(i=0; i<numClass; i++)
fprintf(stream, "%s : %e\t", getLabel(i), getScore(i, _DEFORM_PROB));
fprintf(stream, "\n\tResults : %s\n",
getLabel(selectMax(_DEFORM_PROB)));
fprintf(stream, "Marginalized : \n\t");
for(i=0; i<numClass; i++)
fprintf(stream, "%s : %e\t", getLabel(i), getScore(i, _MARGIN_PROB));
fprintf(stream, "\n\tResults : %s\n",
getLabel(selectMax(_MARGIN_PROB)));
fprintf(stream, "\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -