Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
learn.c
Package: gsnake.rar [view]
Upload User: shhuayu888
Upload Date: 2013-03-28
Package Size: 1788k
Code Size: 6k
Category:
Windows Develop
Development Platform:
Unix_Linux
- /***************************************************************************
- File Name : learn.c
- Purpose : provides routines to learn shape matrix and deformation
- variance.
- 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 :
- kflai@iti.gov.sg
- asschan@ntu.ac.sg
- ***************************************************************************/
- #include "gsnake.h"
- /*
- * constructor
- */
- MODEL::MODEL(void)
- {
- deformSample = 0;
- shapeSample = 0;
- }
- /*
- * initialize template if first snake is trained
- */
- int MODEL::init(GSNAKE *sample)
- {
- /* if model exists */
- if ( deformSample || shapeSample )
- return NOERROR;
- if ( ( initSnaxel( sample->getNumSnaxel() ) == MEMORYERROR ) )
- return MEMORYERROR;
- return NOERROR;
- }
- /*
- * initialize shape matrix parameters if 1st snake is trained
- */
- int MODEL::initShape(GSNAKE *sample)
- {
- SNAXEL *model;
- /* allocate memory */
- if ( init(sample) == MEMORYERROR ) {
- fprintf(stderr,
- "<MODEL::LearnShape> : Memory allocation errorn");
- return MEMORYERROR;
- }
- Eext = 0.0; /* edge force */
- sig_nu_sqr = 0.0; /* white noise variance */
- mode = sample->getMode(); /* opened / close mode */
- EextType = sample->getEextType();
- global_lambda = sample->getGLambda();
- for ( model = getHead(); model ; model = model->getNext() ) {
- model->putAlpha( 0 );
- model->putBeta( 0 );
- model->putRow( 0 );
- model->putCol( 0 );
- model->putLambda( 0 );
- }
- return NOERROR;
- }
- /*
- * initialize deform parameters if 1st snake is trained
- */
- int MODEL::initDeform(GSNAKE *sample)
- {
- SNAXEL *model;
- /* allocate memory */
- if ( init(sample) == MEMORYERROR ) {
- fprintf(stderr,
- "<MODEL::LearnDeform> : Memory allocation errorn");
- return MEMORYERROR;
- }
- Eext = 0.0; /* edge force */
- sig_nu_sqr = 0.0;/* white noise variance */
- global_lambda = _LOCAL_LAMBDA;
- for ( model = getHead(); model ; model = model->getNext() ) {
- model->putEext( 0 );
- model->putLambda( 0 );
- }
- return NOERROR;
- }
- /*
- * learn Shape Matrix A from sample
- */
- int MODEL::LearnShape( GSNAKE *sample )
- {
- SNAXEL *model, *samp;
- /* first learning sample */
- if (shapeSample == 0)
- if ( initShape( sample ) == MEMORYERROR )
- return MEMORYERROR;
- /* learn shape matrix A */
- sample->computeShape();
- /* learn from sample */
- for ( model = getHead(), samp = sample->getHead();
- model && samp; model = model->getNext(),
- samp = samp->getNext() ) {
- model->putAlpha( ( shapeSample * model->getAlpha() +
- samp->getAlpha() ) / ( 1 + shapeSample ) );
- model->putBeta( ( shapeSample * model->getBeta() +
- samp->getBeta() ) / ( 1 + shapeSample ) );
- model->putRow( ( shapeSample * model->getRow() +
- samp->getRow() ) / ( 1 + shapeSample ) );
- model->putCol( ( shapeSample * model->getCol() +
- samp->getCol() ) / ( 1 + shapeSample ) );
- }
- computeAvgLength(); /* compute average length */
- shapeSample++; /* increase number of shape sample */
- return NOERROR;
- }
- /*
- * learn gsnake local lambda
- */
- int MODEL::LearnDeform( GSNAKE *sample ) /* gsnake sample */
- {
- register short m, n;
- double sigma; /* white noise variance */
- double data_x; /* gaussian pyramid data */
- SNAXEL *model; /* model */
- SNAXEL *samp; /* sample */
- double Emodel; /* model energy */
- double lambda; /* local lambda */
- short level; /* gaussian pyramid level */
- BOOLEAN snakept;
- /* first learning sample */
- if (deformSample == 0)
- if ( initDeform( sample ) == MEMORYERROR )
- return MEMORYERROR;
- /* get edge map and gaussian image */
- GaussImg = sample->getGauss( 0 );
- EdgeMap = sample->getEdge( 0 );
- short row = GaussImg->getRow();
- short col = GaussImg->getCol();
- /* compute sample energy */
- sample->ESnake( 0, 0 );
- /* learn deformation from highest level of the pyramid */
- sigma = 0; /* white noise variance */
- sample->computeAvgLength(); /* average length */
- /* learn noise variance */
- for ( m = 0; m < row; m++ )
- for ( n = 0; n < col; n++ ) {
- data_x = (sample->getEextType() == _INTENSITY) ?
- GaussImg->get(m,n) : EdgeMap->getMag(m,n);
- /* check if data_x is a snaxel */
- snakept = _FALSE;
- for (samp=sample->getHead(); samp; samp=samp->getNext())
- if ( (samp->getRow() == m) && (samp->getCol() == n) ) {
- sigma += SQR(data_x - sample->getEext());
- snakept = _TRUE;
- break;
- }
- /* otherwise add the noise variance */
- if (snakept == _FALSE)
- sigma += SQR(data_x);
- }
- sigma /= (double)(m*n); /* compute average noise variance */
- /* learn lambda */
- samp = sample->getHead();
- model = getHead();
- for ( ; samp && model ; samp=samp->getNext(), model=model->getNext() ) {
- model->putEext( (deformSample*model->getEext() +
- samp->getEext()) / (deformSample+1) );
- lambda = sigma / (sigma + samp->getEint());
- model->putLambda( (deformSample*model->getLambda() +
- lambda) / (deformSample+1) );
- }
- Eext = (deformSample*Eext + sample->getEext()) / (deformSample+1);
- sig_nu_sqr = (deformSample*sig_nu_sqr + sigma) / (deformSample+1);
- deformSample++;
- return NOERROR;
- }