2010-10-12 14:35:04 +02:00
# include "precomp.hpp"
2010-10-09 13:36:06 +02:00
# include "_latentsvm.h"
2010-10-12 14:35:04 +02:00
# include "_lsvm_resizeimg.h"
2010-10-09 13:36:06 +02:00
# ifndef max
# define max(a,b) (((a) > (b)) ? (a) : (b))
# endif
# ifndef min
# define min(a,b) (((a) < (b)) ? (a) : (b))
# endif
/*
2011-04-18 21:34:51 +02:00
// Getting feature map for the selected subimage
2010-10-09 13:36:06 +02:00
//
// API
// int getFeatureMaps(const IplImage * image, const int k, featureMap **map);
// INPUT
// image - selected subimage
// k - size of cells
// OUTPUT
// map - feature map
// RESULT
// Error status
*/
2011-07-12 20:43:41 +02:00
int getFeatureMaps ( const IplImage * image , const int k , CvLSVMFeatureMap * * map )
2010-10-09 13:36:06 +02:00
{
int sizeX , sizeY ;
2011-07-12 20:43:41 +02:00
int p , px , stringSize ;
int height , width , numChannels ;
2010-10-09 13:36:06 +02:00
int i , j , kk , c , ii , jj , d ;
float * datadx , * datady ;
2011-07-12 20:43:41 +02:00
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD>
int ch ;
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
float magnitude , x , y , tx , ty ;
2010-10-09 13:36:06 +02:00
IplImage * dx , * dy ;
2011-07-12 20:43:41 +02:00
int * nearest ;
2010-10-09 13:36:06 +02:00
float * w , a_x , b_x ;
2011-07-12 20:43:41 +02:00
// <20> <> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <20> <> <EFBFBD> <EFBFBD> x <20> y
float kernel [ 3 ] = { - 1.f , 0.f , 1.f } ;
2010-10-09 13:36:06 +02:00
CvMat kernel_dx = cvMat ( 1 , 3 , CV_32F , kernel ) ;
CvMat kernel_dy = cvMat ( 3 , 1 , CV_32F , kernel ) ;
2011-07-12 20:43:41 +02:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2010-10-09 13:36:06 +02:00
float * r ;
2011-07-12 20:43:41 +02:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
// <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
int * alfa ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
float boundary_x [ NUM_SECTOR + 1 ] ;
float boundary_y [ NUM_SECTOR + 1 ] ;
float max , dotProd ;
int maxi ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
height = image - > height ;
width = image - > width ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
numChannels = image - > nChannels ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
dx = cvCreateImage ( cvSize ( image - > width , image - > height ) ,
IPL_DEPTH_32F , 3 ) ;
dy = cvCreateImage ( cvSize ( image - > width , image - > height ) ,
IPL_DEPTH_32F , 3 ) ;
2010-10-09 13:36:06 +02:00
sizeX = width / k ;
sizeY = height / k ;
2011-07-12 20:43:41 +02:00
px = 3 * NUM_SECTOR ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2010-10-09 13:36:06 +02:00
p = px ;
2011-07-12 20:43:41 +02:00
stringSize = sizeX * p ;
allocFeatureMapObject ( map , sizeX , sizeY , p ) ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
cvFilter2D ( image , dx , & kernel_dx , cvPoint ( - 1 , 0 ) ) ;
cvFilter2D ( image , dy , & kernel_dy , cvPoint ( 0 , - 1 ) ) ;
float arg_vector ;
for ( i = 0 ; i < = NUM_SECTOR ; i + + )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
arg_vector = ( ( float ) i ) * ( ( float ) ( PI ) / ( float ) ( NUM_SECTOR ) ) ;
boundary_x [ i ] = cosf ( arg_vector ) ;
boundary_y [ i ] = sinf ( arg_vector ) ;
} /*for(i = 0; i <= NUM_SECTOR; i++) */
2010-10-09 13:36:06 +02:00
r = ( float * ) malloc ( sizeof ( float ) * ( width * height ) ) ;
alfa = ( int * ) malloc ( sizeof ( int ) * ( width * height * 2 ) ) ;
2011-07-12 20:43:41 +02:00
for ( j = 1 ; j < height - 1 ; j + + )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
datadx = ( float * ) ( dx - > imageData + dx - > widthStep * j ) ;
datady = ( float * ) ( dy - > imageData + dy - > widthStep * j ) ;
for ( i = 1 ; i < width - 1 ; i + + )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
c = 0 ;
x = ( datadx [ i * numChannels + c ] ) ;
y = ( datady [ i * numChannels + c ] ) ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
r [ j * width + i ] = sqrtf ( x * x + y * y ) ;
for ( ch = 1 ; ch < numChannels ; ch + + )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
tx = ( datadx [ i * numChannels + ch ] ) ;
ty = ( datady [ i * numChannels + ch ] ) ;
magnitude = sqrtf ( tx * tx + ty * ty ) ;
if ( magnitude > r [ j * width + i ] )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
r [ j * width + i ] = magnitude ;
c = ch ;
2010-10-09 13:36:06 +02:00
x = tx ;
y = ty ;
}
2011-07-12 20:43:41 +02:00
} /*for(ch = 1; ch < numChannels; ch++)*/
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
max = boundary_x [ 0 ] * x + boundary_y [ 0 ] * y ;
2010-10-09 13:36:06 +02:00
maxi = 0 ;
2011-07-12 20:43:41 +02:00
for ( kk = 0 ; kk < NUM_SECTOR ; kk + + )
{
dotProd = boundary_x [ kk ] * x + boundary_y [ kk ] * y ;
if ( dotProd > max )
{
max = dotProd ;
2010-10-09 13:36:06 +02:00
maxi = kk ;
2011-07-12 20:43:41 +02:00
}
else
{
if ( - dotProd > max )
{
max = - dotProd ;
maxi = kk + NUM_SECTOR ;
}
2010-10-09 13:36:06 +02:00
}
}
2011-07-12 20:43:41 +02:00
alfa [ j * width * 2 + i * 2 ] = maxi % NUM_SECTOR ;
2010-10-09 13:36:06 +02:00
alfa [ j * width * 2 + i * 2 + 1 ] = maxi ;
} /*for(i = 0; i < width; i++)*/
} /*for(j = 0; j < height; j++)*/
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <20> <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2011-07-12 20:43:41 +02:00
nearest = ( int * ) malloc ( sizeof ( int ) * k ) ;
w = ( float * ) malloc ( sizeof ( float ) * ( k * 2 ) ) ;
2010-10-09 13:36:06 +02:00
for ( i = 0 ; i < k / 2 ; i + + )
{
2011-07-12 20:43:41 +02:00
nearest [ i ] = - 1 ;
2010-10-09 13:36:06 +02:00
} /*for(i = 0; i < k / 2; i++)*/
for ( i = k / 2 ; i < k ; i + + )
{
2011-07-12 20:43:41 +02:00
nearest [ i ] = 1 ;
2010-10-09 13:36:06 +02:00
} /*for(i = k / 2; i < k; i++)*/
for ( j = 0 ; j < k / 2 ; j + + )
{
b_x = k / 2 + j + 0.5f ;
a_x = k / 2 - j - 0.5f ;
w [ j * 2 ] = 1.0f / a_x * ( ( a_x * b_x ) / ( a_x + b_x ) ) ;
w [ j * 2 + 1 ] = 1.0f / b_x * ( ( a_x * b_x ) / ( a_x + b_x ) ) ;
} /*for(j = 0; j < k / 2; j++)*/
for ( j = k / 2 ; j < k ; j + + )
{
a_x = j - k / 2 + 0.5f ;
b_x = - j + k / 2 - 0.5f + k ;
w [ j * 2 ] = 1.0f / a_x * ( ( a_x * b_x ) / ( a_x + b_x ) ) ;
w [ j * 2 + 1 ] = 1.0f / b_x * ( ( a_x * b_x ) / ( a_x + b_x ) ) ;
} /*for(j = k / 2; j < k; j++)*/
//<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
for ( i = 0 ; i < sizeY ; i + + )
{
2011-07-12 20:43:41 +02:00
for ( j = 0 ; j < sizeX ; j + + )
{
for ( ii = 0 ; ii < k ; ii + + )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
for ( jj = 0 ; jj < k ; jj + + )
{
if ( ( i * k + ii > 0 ) & &
( i * k + ii < height - 1 ) & &
( j * k + jj > 0 ) & &
( j * k + jj < width - 1 ) )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
d = ( k * i + ii ) * width + ( j * k + jj ) ;
( * map ) - > map [ i * stringSize + j * ( * map ) - > numFeatures + alfa [ d * 2 ] ] + =
r [ d ] * w [ ii * 2 ] * w [ jj * 2 ] ;
( * map ) - > map [ i * stringSize + j * ( * map ) - > numFeatures + alfa [ d * 2 + 1 ] + NUM_SECTOR ] + =
r [ d ] * w [ ii * 2 ] * w [ jj * 2 ] ;
if ( ( i + nearest [ ii ] > = 0 ) & &
( i + nearest [ ii ] < = sizeY - 1 ) )
{
( * map ) - > map [ ( i + nearest [ ii ] ) * stringSize + j * ( * map ) - > numFeatures + alfa [ d * 2 ] ] + =
r [ d ] * w [ ii * 2 + 1 ] * w [ jj * 2 ] ;
( * map ) - > map [ ( i + nearest [ ii ] ) * stringSize + j * ( * map ) - > numFeatures + alfa [ d * 2 + 1 ] + NUM_SECTOR ] + =
r [ d ] * w [ ii * 2 + 1 ] * w [ jj * 2 ] ;
}
if ( ( j + nearest [ jj ] > = 0 ) & &
( j + nearest [ jj ] < = sizeX - 1 ) )
{
( * map ) - > map [ i * stringSize + ( j + nearest [ jj ] ) * ( * map ) - > numFeatures + alfa [ d * 2 ] ] + =
r [ d ] * w [ ii * 2 ] * w [ jj * 2 + 1 ] ;
( * map ) - > map [ i * stringSize + ( j + nearest [ jj ] ) * ( * map ) - > numFeatures + alfa [ d * 2 + 1 ] + NUM_SECTOR ] + =
r [ d ] * w [ ii * 2 ] * w [ jj * 2 + 1 ] ;
}
if ( ( i + nearest [ ii ] > = 0 ) & &
( i + nearest [ ii ] < = sizeY - 1 ) & &
( j + nearest [ jj ] > = 0 ) & &
( j + nearest [ jj ] < = sizeX - 1 ) )
{
( * map ) - > map [ ( i + nearest [ ii ] ) * stringSize + ( j + nearest [ jj ] ) * ( * map ) - > numFeatures + alfa [ d * 2 ] ] + =
r [ d ] * w [ ii * 2 + 1 ] * w [ jj * 2 + 1 ] ;
( * map ) - > map [ ( i + nearest [ ii ] ) * stringSize + ( j + nearest [ jj ] ) * ( * map ) - > numFeatures + alfa [ d * 2 + 1 ] + NUM_SECTOR ] + =
r [ d ] * w [ ii * 2 + 1 ] * w [ jj * 2 + 1 ] ;
}
}
} /*for(jj = 0; jj < k; jj++)*/
} /*for(ii = 0; ii < k; ii++)*/
} /*for(j = 1; j < sizeX - 1; j++)*/
2010-10-09 13:36:06 +02:00
} /*for(i = 1; i < sizeY - 1; i++)*/
cvReleaseImage ( & dx ) ;
cvReleaseImage ( & dy ) ;
free ( w ) ;
2011-07-12 20:43:41 +02:00
free ( nearest ) ;
2010-10-09 13:36:06 +02:00
free ( r ) ;
free ( alfa ) ;
return LATENT_SVM_OK ;
}
/*
// Feature map Normalization and Truncation
//
// API
2011-07-12 20:43:41 +02:00
// int normalizeAndTruncate(featureMap *map, const float alfa);
2010-10-09 13:36:06 +02:00
// INPUT
// map - feature map
// alfa - truncation threshold
// OUTPUT
// map - truncated and normalized feature map
// RESULT
// Error status
*/
2011-07-12 20:43:41 +02:00
int normalizeAndTruncate ( CvLSVMFeatureMap * map , const float alfa )
2010-10-09 13:36:06 +02:00
{
int i , j , ii ;
int sizeX , sizeY , p , pos , pp , xp , pos1 , pos2 ;
2011-07-12 20:43:41 +02:00
float * partOfNorm ; // norm of C(i, j)
float * newData ;
float valOfNorm ;
2010-10-09 13:36:06 +02:00
sizeX = map - > sizeX ;
sizeY = map - > sizeY ;
2011-07-12 20:43:41 +02:00
partOfNorm = ( float * ) malloc ( sizeof ( float ) * ( sizeX * sizeY ) ) ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
p = NUM_SECTOR ;
xp = NUM_SECTOR * 3 ;
pp = NUM_SECTOR * 12 ;
2010-10-09 13:36:06 +02:00
for ( i = 0 ; i < sizeX * sizeY ; i + + )
{
2011-07-12 20:43:41 +02:00
valOfNorm = 0.0f ;
pos = i * map - > numFeatures ;
2010-10-09 13:36:06 +02:00
for ( j = 0 ; j < p ; j + + )
{
2011-07-12 20:43:41 +02:00
valOfNorm + = map - > map [ pos + j ] * map - > map [ pos + j ] ;
2010-10-09 13:36:06 +02:00
} /*for(j = 0; j < p; j++)*/
2011-07-12 20:43:41 +02:00
partOfNorm [ i ] = valOfNorm ;
2010-10-09 13:36:06 +02:00
} /*for(i = 0; i < sizeX * sizeY; i++)*/
2011-07-12 20:43:41 +02:00
2010-10-09 13:36:06 +02:00
sizeX - = 2 ;
sizeY - = 2 ;
2011-07-12 20:43:41 +02:00
newData = ( float * ) malloc ( sizeof ( float ) * ( sizeX * sizeY * pp ) ) ;
2010-10-09 13:36:06 +02:00
//normalization
for ( i = 1 ; i < = sizeY ; i + + )
{
for ( j = 1 ; j < = sizeX ; j + + )
{
2011-07-12 20:43:41 +02:00
valOfNorm = sqrtf (
partOfNorm [ ( i ) * ( sizeX + 2 ) + ( j ) ] +
partOfNorm [ ( i ) * ( sizeX + 2 ) + ( j + 1 ) ] +
partOfNorm [ ( i + 1 ) * ( sizeX + 2 ) + ( j ) ] +
partOfNorm [ ( i + 1 ) * ( sizeX + 2 ) + ( j + 1 ) ] ) ;
2010-10-09 13:36:06 +02:00
pos1 = ( i ) * ( sizeX + 2 ) * xp + ( j ) * xp ;
pos2 = ( i - 1 ) * ( sizeX ) * pp + ( j - 1 ) * pp ;
for ( ii = 0 ; ii < p ; ii + + )
{
2011-07-12 20:43:41 +02:00
newData [ pos2 + ii ] = map - > map [ pos1 + ii ] / valOfNorm ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < p; ii++)*/
for ( ii = 0 ; ii < 2 * p ; ii + + )
{
2011-07-12 20:43:41 +02:00
newData [ pos2 + ii + p * 4 ] = map - > map [ pos1 + ii + p ] / valOfNorm ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < 2 * p; ii++)*/
2011-07-12 20:43:41 +02:00
valOfNorm = sqrtf (
partOfNorm [ ( i ) * ( sizeX + 2 ) + ( j ) ] +
partOfNorm [ ( i ) * ( sizeX + 2 ) + ( j + 1 ) ] +
partOfNorm [ ( i - 1 ) * ( sizeX + 2 ) + ( j ) ] +
partOfNorm [ ( i - 1 ) * ( sizeX + 2 ) + ( j + 1 ) ] ) ;
2010-10-09 13:36:06 +02:00
for ( ii = 0 ; ii < p ; ii + + )
{
2011-07-12 20:43:41 +02:00
newData [ pos2 + ii + p ] = map - > map [ pos1 + ii ] / valOfNorm ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < p; ii++)*/
for ( ii = 0 ; ii < 2 * p ; ii + + )
{
2011-07-12 20:43:41 +02:00
newData [ pos2 + ii + p * 6 ] = map - > map [ pos1 + ii + p ] / valOfNorm ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < 2 * p; ii++)*/
2011-07-12 20:43:41 +02:00
valOfNorm = sqrtf (
partOfNorm [ ( i ) * ( sizeX + 2 ) + ( j ) ] +
partOfNorm [ ( i ) * ( sizeX + 2 ) + ( j - 1 ) ] +
partOfNorm [ ( i + 1 ) * ( sizeX + 2 ) + ( j ) ] +
partOfNorm [ ( i + 1 ) * ( sizeX + 2 ) + ( j - 1 ) ] ) ;
2010-10-09 13:36:06 +02:00
for ( ii = 0 ; ii < p ; ii + + )
{
2011-07-12 20:43:41 +02:00
newData [ pos2 + ii + p * 2 ] = map - > map [ pos1 + ii ] / valOfNorm ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < p; ii++)*/
for ( ii = 0 ; ii < 2 * p ; ii + + )
{
2011-07-12 20:43:41 +02:00
newData [ pos2 + ii + p * 8 ] = map - > map [ pos1 + ii + p ] / valOfNorm ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < 2 * p; ii++)*/
2011-07-12 20:43:41 +02:00
valOfNorm = sqrtf (
partOfNorm [ ( i ) * ( sizeX + 2 ) + ( j ) ] +
partOfNorm [ ( i ) * ( sizeX + 2 ) + ( j - 1 ) ] +
partOfNorm [ ( i - 1 ) * ( sizeX + 2 ) + ( j ) ] +
partOfNorm [ ( i - 1 ) * ( sizeX + 2 ) + ( j - 1 ) ] ) ;
2010-10-09 13:36:06 +02:00
for ( ii = 0 ; ii < p ; ii + + )
{
2011-07-12 20:43:41 +02:00
newData [ pos2 + ii + p * 3 ] = map - > map [ pos1 + ii ] / valOfNorm ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < p; ii++)*/
for ( ii = 0 ; ii < 2 * p ; ii + + )
{
2011-07-12 20:43:41 +02:00
newData [ pos2 + ii + p * 10 ] = map - > map [ pos1 + ii + p ] / valOfNorm ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < 2 * p; ii++)*/
} /*for(j = 1; j <= sizeX; j++)*/
} /*for(i = 1; i <= sizeY; i++)*/
//truncation
for ( i = 0 ; i < sizeX * sizeY * pp ; i + + )
{
2011-07-12 20:43:41 +02:00
if ( newData [ i ] > alfa ) newData [ i ] = alfa ;
2010-10-09 13:36:06 +02:00
} /*for(i = 0; i < sizeX * sizeY * pp; i++)*/
//swop data
2011-07-12 20:43:41 +02:00
map - > numFeatures = pp ;
2010-10-09 13:36:06 +02:00
map - > sizeX = sizeX ;
map - > sizeY = sizeY ;
2011-07-12 20:43:41 +02:00
free ( map - > map ) ;
free ( partOfNorm ) ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
map - > map = newData ;
2010-10-09 13:36:06 +02:00
return LATENT_SVM_OK ;
}
/*
// Feature map reduction
// In each cell we reduce dimension of the feature vector
// according to original paper special procedure
//
// API
// int PCAFeatureMaps(featureMap *map)
// INPUT
// map - feature map
// OUTPUT
// map - feature map
// RESULT
// Error status
*/
2010-10-18 10:49:18 +02:00
int PCAFeatureMaps ( CvLSVMFeatureMap * map )
2010-10-09 13:36:06 +02:00
{
int i , j , ii , jj , k ;
int sizeX , sizeY , p , pp , xp , yp , pos1 , pos2 ;
2011-07-12 20:43:41 +02:00
float * newData ;
2010-10-09 13:36:06 +02:00
float val ;
float nx , ny ;
sizeX = map - > sizeX ;
sizeY = map - > sizeY ;
2011-07-12 20:43:41 +02:00
p = map - > numFeatures ;
pp = NUM_SECTOR * 3 + 4 ;
2010-10-09 13:36:06 +02:00
yp = 4 ;
2011-07-12 20:43:41 +02:00
xp = NUM_SECTOR ;
2010-10-09 13:36:06 +02:00
nx = 1.0f / sqrtf ( ( float ) ( xp * 2 ) ) ;
ny = 1.0f / sqrtf ( ( float ) ( yp ) ) ;
2011-07-12 20:43:41 +02:00
newData = ( float * ) malloc ( sizeof ( float ) * ( sizeX * sizeY * pp ) ) ;
2010-10-09 13:36:06 +02:00
for ( i = 0 ; i < sizeY ; i + + )
{
for ( j = 0 ; j < sizeX ; j + + )
{
pos1 = ( ( i ) * sizeX + j ) * p ;
pos2 = ( ( i ) * sizeX + j ) * pp ;
k = 0 ;
for ( jj = 0 ; jj < xp * 2 ; jj + + )
{
val = 0 ;
for ( ii = 0 ; ii < yp ; ii + + )
{
2011-07-12 20:43:41 +02:00
val + = map - > map [ pos1 + yp * xp + ii * xp * 2 + jj ] ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < yp; ii++)*/
2011-07-12 20:43:41 +02:00
newData [ pos2 + k ] = val * ny ;
2010-10-09 13:36:06 +02:00
k + + ;
} /*for(jj = 0; jj < xp * 2; jj++)*/
for ( jj = 0 ; jj < xp ; jj + + )
{
val = 0 ;
for ( ii = 0 ; ii < yp ; ii + + )
{
2011-07-12 20:43:41 +02:00
val + = map - > map [ pos1 + ii * xp + jj ] ;
2010-10-09 13:36:06 +02:00
} /*for(ii = 0; ii < yp; ii++)*/
2011-07-12 20:43:41 +02:00
newData [ pos2 + k ] = val * ny ;
2010-10-09 13:36:06 +02:00
k + + ;
} /*for(jj = 0; jj < xp; jj++)*/
for ( ii = 0 ; ii < yp ; ii + + )
{
val = 0 ;
for ( jj = 0 ; jj < 2 * xp ; jj + + )
{
2011-07-12 20:43:41 +02:00
val + = map - > map [ pos1 + yp * xp + ii * xp * 2 + jj ] ;
2010-10-09 13:36:06 +02:00
} /*for(jj = 0; jj < xp; jj++)*/
2011-07-12 20:43:41 +02:00
newData [ pos2 + k ] = val * nx ;
2010-10-09 13:36:06 +02:00
k + + ;
} /*for(ii = 0; ii < yp; ii++)*/
} /*for(j = 0; j < sizeX; j++)*/
} /*for(i = 0; i < sizeY; i++)*/
//swop data
2011-07-12 20:43:41 +02:00
map - > numFeatures = pp ;
free ( map - > map ) ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
map - > map = newData ;
return LATENT_SVM_OK ;
}
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
int getPathOfFeaturePyramid ( IplImage * image ,
float step , int numStep , int startIndex ,
int sideLength , CvLSVMFeaturePyramid * * maps )
{
CvLSVMFeatureMap * map ;
IplImage * scaleTmp ;
float scale ;
int i , err ;
for ( i = 0 ; i < numStep ; i + + )
{
scale = 1.0f / powf ( step , ( float ) i ) ;
scaleTmp = resize_opencv ( image , scale ) ;
err = getFeatureMaps ( scaleTmp , sideLength , & map ) ;
err = normalizeAndTruncate ( map , VAL_OF_TRUNCATE ) ;
err = PCAFeatureMaps ( map ) ;
( * maps ) - > pyramid [ startIndex + i ] = map ;
cvReleaseImage ( & scaleTmp ) ;
} /*for(i = 0; i < numStep; i++)*/
2010-10-09 13:36:06 +02:00
return LATENT_SVM_OK ;
}
/*
// Getting feature pyramid
//
// API
// int getFeaturePyramid(IplImage * image, const filterObject **all_F,
const int n_f ,
const int lambda , const int k ,
const int startX , const int startY ,
const int W , const int H , featurePyramid * * maps ) ;
// INPUT
// image - image
// OUTPUT
// maps - feature maps for all levels
// RESULT
// Error status
*/
2011-07-12 20:43:41 +02:00
int getFeaturePyramid ( IplImage * image , CvLSVMFeaturePyramid * * maps )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
IplImage * imgResize ;
float step ;
int numStep ;
int maxNumCells ;
int W , H ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
if ( image - > depth = = IPL_DEPTH_32F )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
imgResize = image ;
2010-10-09 13:36:06 +02:00
}
else
{
2011-07-12 20:43:41 +02:00
imgResize = cvCreateImage ( cvSize ( image - > width , image - > height ) ,
IPL_DEPTH_32F , 3 ) ;
cvConvert ( image , imgResize ) ;
2010-10-09 13:36:06 +02:00
}
2011-07-12 20:43:41 +02:00
W = imgResize - > width ;
H = imgResize - > height ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
step = powf ( 2.0f , 1.0f / ( ( float ) LAMBDA ) ) ;
maxNumCells = W / SIDE_LENGTH ;
if ( maxNumCells > H / SIDE_LENGTH )
2010-10-09 13:36:06 +02:00
{
2011-07-12 20:43:41 +02:00
maxNumCells = H / SIDE_LENGTH ;
2010-10-09 13:36:06 +02:00
}
2011-07-12 20:43:41 +02:00
numStep = ( int ) ( logf ( ( float ) maxNumCells / ( 5.0f ) ) / logf ( step ) ) + 1 ;
allocFeaturePyramidObject ( maps , numStep + LAMBDA ) ;
2010-10-09 13:36:06 +02:00
2011-07-12 20:43:41 +02:00
getPathOfFeaturePyramid ( imgResize , step , LAMBDA , 0 ,
SIDE_LENGTH / 2 , maps ) ;
getPathOfFeaturePyramid ( imgResize , step , numStep , LAMBDA ,
SIDE_LENGTH , maps ) ;
if ( image - > depth ! = IPL_DEPTH_32F )
2010-10-09 13:36:06 +02:00
{
cvReleaseImage ( & imgResize ) ;
}
return LATENT_SVM_OK ;
2011-07-12 20:43:41 +02:00
}