refactoring latentSVM

This commit is contained in:
Evgeniy Kozinov 2011-07-12 18:43:41 +00:00
parent ea8e27961e
commit 07eed8c489
10 changed files with 354 additions and 409 deletions

View File

@ -160,9 +160,9 @@ CVAPI(int) cvRunHaarClassifierCascade( const CvHaarClassifierCascade* cascade,
// (x, y) - coordinate in level l // (x, y) - coordinate in level l
typedef struct typedef struct
{ {
unsigned int x; int x;
unsigned int y; int y;
unsigned int l; int l;
} CvLSVMFilterPosition; } CvLSVMFilterPosition;
// DataType: STRUCT filterObject // DataType: STRUCT filterObject
@ -179,16 +179,12 @@ typedef struct
// used formula H[(j * sizeX + i) * p + k], where // used formula H[(j * sizeX + i) * p + k], where
// k - component of feature vector in cell (i, j) // k - component of feature vector in cell (i, j)
// END OF FILTER DESCRIPTION // END OF FILTER DESCRIPTION
// xp - auxillary parameter for internal use
// size of row in feature vectors
// (yp = (int) (p / xp); p = xp * yp)
typedef struct{ typedef struct{
CvLSVMFilterPosition V; CvLSVMFilterPosition V;
float fineFunction[4]; float fineFunction[4];
unsigned int sizeX; int sizeX;
unsigned int sizeY; int sizeY;
unsigned int p; int numFeatures;
unsigned int xp;
float *H; float *H;
} CvLSVMFilterObject; } CvLSVMFilterObject;

View File

@ -37,10 +37,7 @@
// RESULT // RESULT
// Error status // Error status
*/ */
int getFeaturePyramid(IplImage * image, int getFeaturePyramid(IplImage * image, CvLSVMFeaturePyramid **maps);
const int lambda, const int k,
const int startX, const int startY,
const int W, const int H, CvLSVMFeaturePyramid **maps);
/* /*
// Getting feature map for the selected subimage // Getting feature map for the selected subimage
@ -55,7 +52,7 @@ int getFeaturePyramid(IplImage * image,
// RESULT // RESULT
// Error status // Error status
*/ */
int getFeatureMaps_dp(const IplImage * image, const int k, CvLSVMFeatureMap **map); int getFeatureMaps(const IplImage * image, const int k, CvLSVMFeatureMap **map);
/* /*
@ -71,7 +68,7 @@ int getFeatureMaps_dp(const IplImage * image, const int k, CvLSVMFeatureMap **ma
// RESULT // RESULT
// Error status // Error status
*/ */
int normalizationAndTruncationFeatureMaps(CvLSVMFeatureMap *map, const float alfa); int normalizeAndTruncate(CvLSVMFeatureMap *map, const float alfa);
/* /*
// Feature map reduction // Feature map reduction

View File

@ -2,6 +2,7 @@
#define LSVM_ERROR #define LSVM_ERROR
#define LATENT_SVM_OK 0 #define LATENT_SVM_OK 0
#define LATENT_SVM_MEM_NULL 2
#define DISTANCE_TRANSFORM_OK 1 #define DISTANCE_TRANSFORM_OK 1
#define DISTANCE_TRANSFORM_GET_INTERSECTION_ERROR -1 #define DISTANCE_TRANSFORM_GET_INTERSECTION_ERROR -1
#define DISTANCE_TRANSFORM_ERROR -2 #define DISTANCE_TRANSFORM_ERROR -2

View File

@ -13,18 +13,18 @@
// Error status is return value // Error status is return value
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
int allocFilterObject(CvLSVMFilterObject **obj, const int sizeX, const int sizeY, int allocFilterObject(CvLSVMFilterObject **obj, const int sizeX, const int sizeY,
const int p, const int xp); const int p);
int freeFilterObject (CvLSVMFilterObject **obj); int freeFilterObject (CvLSVMFilterObject **obj);
int allocFeatureMapObject(CvLSVMFeatureMap **obj, const int sizeX, const int sizeY, int allocFeatureMapObject(CvLSVMFeatureMap **obj, const int sizeX, const int sizeY,
const int p, const int xp); const int p);
int freeFeatureMapObject (CvLSVMFeatureMap **obj); int freeFeatureMapObject (CvLSVMFeatureMap **obj);
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
#endif #endif
int allocFeaturePyramidObject(CvLSVMFeaturePyramid **obj, int allocFeaturePyramidObject(CvLSVMFeaturePyramid **obj,
const int lambda, const int countLevel); const int countLevel);
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"

View File

@ -14,7 +14,7 @@
// The number of elements in bin // The number of elements in bin
// The number of sectors in gradient histogram building // The number of sectors in gradient histogram building
#define CNTPARTION 9 #define NUM_SECTOR 9
// The number of levels in image resize procedure // The number of levels in image resize procedure
// We need Lambda levels to resize image twice // We need Lambda levels to resize image twice
@ -23,6 +23,8 @@
// Block size. Used in feature pyramid building procedure // Block size. Used in feature pyramid building procedure
#define SIDE_LENGTH 8 #define SIDE_LENGTH 8
#define VAL_OF_TRUNCATE 0.2f
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
// main data structures // // main data structures //
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
@ -30,31 +32,24 @@
// DataType: STRUCT featureMap // DataType: STRUCT featureMap
// FEATURE MAP DESCRIPTION // FEATURE MAP DESCRIPTION
// Rectangular map (sizeX x sizeY), // Rectangular map (sizeX x sizeY),
// every cell stores feature vector (dimension = p) // every cell stores feature vector (dimension = numFeatures)
// H - matrix of feature vectors // map - matrix of feature vectors
// to set and get feature vectors (i,j) // to set and get feature vectors (i,j)
// used formula Map[(j * sizeX + i) * p + k], where // used formula map[(j * sizeX + i) * p + k], where
// k - component of feature vector in cell (i, j) // k - component of feature vector in cell (i, j)
// END OF FEATURE MAP DESCRIPTION
// xp - auxillary parameter for internal use
// size of row in feature vectors
// (yp = (int) (p / xp); p = xp * yp)
typedef struct{ typedef struct{
int sizeX; int sizeX;
int sizeY; int sizeY;
int p; int numFeatures;
int xp; float *map;
float *Map;
} CvLSVMFeatureMap; } CvLSVMFeatureMap;
// DataType: STRUCT featurePyramid // DataType: STRUCT featurePyramid
// //
// countLevel - number of levels in the feature pyramid // numLevels - number of levels in the feature pyramid
// lambda - resize scale coefficient
// pyramid - array of pointers to feature map at different levels // pyramid - array of pointers to feature map at different levels
typedef struct{ typedef struct{
int countLevel; int numLevels;
int lambda;
CvLSVMFeatureMap **pyramid; CvLSVMFeatureMap **pyramid;
} CvLSVMFeaturePyramid; } CvLSVMFeaturePyramid;
@ -74,14 +69,14 @@ typedef struct{
// DataType: STRUCT fftImage // DataType: STRUCT fftImage
// The structure stores FFT image // The structure stores FFT image
// //
// p - number of channels // numFeatures - number of channels
// x - array of FFT images for 2d signals // x - array of FFT images for 2d signals
// n - number of rows // n - number of rows
// m - number of collums // m - number of collums
typedef struct{ typedef struct{
unsigned int p; int numFeatures;
unsigned int dimX; int dimX;
unsigned int dimY; int dimY;
float **channels; float **channels;
} CvLSVMFftImage; } CvLSVMFftImage;

View File

@ -10,13 +10,6 @@
#define min(a,b) (((a) < (b)) ? (a) : (b)) #define min(a,b) (((a) < (b)) ? (a) : (b))
#endif #endif
static inline int sign(float r)
{
if(r > 0.0001f) return 1;
if(r < -0.0001f) return -1;
return 0;
}
/* /*
// Getting feature map for the selected subimage // Getting feature map for the selected subimage
// //
@ -30,115 +23,132 @@ static inline int sign(float r)
// RESULT // RESULT
// Error status // Error status
*/ */
int getFeatureMaps_dp(const IplImage* image,const int k, CvLSVMFeatureMap **map) int getFeatureMaps(const IplImage* image, const int k, CvLSVMFeatureMap **map)
{ {
int sizeX, sizeY; int sizeX, sizeY;
int p, px, strsz; int p, px, stringSize;
int height, width, channels; int height, width, numChannels;
int i, j, kk, c, ii, jj, d; int i, j, kk, c, ii, jj, d;
float * datadx, * datady; float * datadx, * datady;
float tmp, x, y, tx, ty;
//íîìåð êàíàëà â öèêëå
int ch;
//ïåðåìåííûå âû÷èñëåíèÿ ìàãíèòóäû
float magnitude, x, y, tx, ty;
IplImage * dx, * dy; IplImage * dx, * dy;
int *nearest_x, *nearest_y; int *nearest;
float *w, a_x, b_x; float *w, a_x, b_x;
float kernel[3] = {-1.f, 0.f, 1.f}; // ÿäðî äëÿ âû÷èñëåíèÿ ãðàäèåíòîâ èçîáðàæåíèå ïî îñÿì x è y
float kernel[3] = {-1.f, 0.f, 1.f};
CvMat kernel_dx = cvMat(1, 3, CV_32F, kernel); CvMat kernel_dx = cvMat(1, 3, CV_32F, kernel);
CvMat kernel_dy = cvMat(3, 1, CV_32F, kernel); CvMat kernel_dy = cvMat(3, 1, CV_32F, kernel);
// ãðà÷åíèå ãðàäèåíòà
float * r; float * r;
int * alfa; // íîâåð ñåêòîðà êóäà ïîïàëî çíà÷åíèå ãðàäèåíòà
// ÷åòíûå èííåêñû íå êîíòðàñòíîå èçîáðàæåíèå
// íå ÷åòíûå èííåêñû êîíòðàñòíîå èçîáðàæåíèå
int * alfa;
float boundary_x[CNTPARTION+1]; // âåêòîðû ãðàíèö ñåêòîðîâ
float boundary_y[CNTPARTION+1]; float boundary_x[NUM_SECTOR + 1];
float max, tmp_scal; float boundary_y[NUM_SECTOR + 1];
int maxi; float max, dotProd;
int maxi;
height = image->height; height = image->height;
width = image->width ; width = image->width ;
channels = image->nChannels; numChannels = image->nChannels;
dx = cvCreateImage(cvSize(image->width , image->height) , IPL_DEPTH_32F , 3); dx = cvCreateImage(cvSize(image->width, image->height),
dy = cvCreateImage(cvSize(image->width , image->height) , IPL_DEPTH_32F , 3); IPL_DEPTH_32F, 3);
dy = cvCreateImage(cvSize(image->width, image->height),
IPL_DEPTH_32F, 3);
sizeX = width / k; sizeX = width / k;
sizeY = height / k; sizeY = height / k;
px = CNTPARTION + 2 * CNTPARTION; // êîíòðàñòíîå è íå êîíòðàñòíîå èçîáðàæåíèå px = 3 * NUM_SECTOR; // êîíòðàñòíîå è íå êîíòðàñòíîå èçîáðàæåíèå
p = px; p = px;
strsz = sizeX * p; stringSize = sizeX * p;
allocFeatureMapObject(map, sizeX, sizeY, p, px); allocFeatureMapObject(map, sizeX, sizeY, p);
cvFilter2D(image, dx, &kernel_dx, cvPoint(-1, 0)); cvFilter2D(image, dx, &kernel_dx, cvPoint(-1, 0));
cvFilter2D(image, dy, &kernel_dy, cvPoint(0, -1)); cvFilter2D(image, dy, &kernel_dy, cvPoint(0, -1));
for(i = 0; i <= CNTPARTION; i++) float arg_vector;
for(i = 0; i <= NUM_SECTOR; i++)
{ {
boundary_x[i] = cosf((((float)i) * (((float)PI) / (float) (CNTPARTION)))); arg_vector = ( (float) i ) * ( (float)(PI) / (float)(NUM_SECTOR) );
boundary_y[i] = sinf((((float)i) * (((float)PI) / (float) (CNTPARTION)))); boundary_x[i] = cosf(arg_vector);
}/*for(i = 0; i <= CNTPARTION; i++) */ boundary_y[i] = sinf(arg_vector);
}/*for(i = 0; i <= NUM_SECTOR; i++) */
r = (float *)malloc( sizeof(float) * (width * height)); r = (float *)malloc( sizeof(float) * (width * height));
alfa = (int *)malloc( sizeof(int ) * (width * height * 2)); alfa = (int *)malloc( sizeof(int ) * (width * height * 2));
for(j = 1; j < height-1; j++) for(j = 1; j < height - 1; j++)
{ {
datadx = (float*)(dx->imageData + dx->widthStep *j); datadx = (float*)(dx->imageData + dx->widthStep * j);
datady = (float*)(dy->imageData + dy->widthStep *j); datady = (float*)(dy->imageData + dy->widthStep * j);
for(i = 1; i < width-1; i++) for(i = 1; i < width - 1; i++)
{ {
c = 0; c = 0;
x = (datadx[i*channels+c]); x = (datadx[i * numChannels + c]);
y = (datady[i*channels+c]); y = (datady[i * numChannels + c]);
r[j * width + i] =sqrtf(x*x + y*y); r[j * width + i] =sqrtf(x * x + y * y);
for(kk = 1; kk < channels; kk++) for(ch = 1; ch < numChannels; ch++)
{ {
tx = (datadx[i*channels+kk]); tx = (datadx[i * numChannels + ch]);
ty = (datady[i*channels+kk]); ty = (datady[i * numChannels + ch]);
tmp =sqrtf(tx*tx + ty*ty); magnitude = sqrtf(tx * tx + ty * ty);
if(tmp > r[j * width + i]) if(magnitude > r[j * width + i])
{ {
r[j * width + i] = tmp; r[j * width + i] = magnitude;
c = kk; c = ch;
x = tx; x = tx;
y = ty; y = ty;
} }
}/*for(kk = 1; kk < channels; kk++)*/ }/*for(ch = 1; ch < numChannels; ch++)*/
max = boundary_x[0] * x + boundary_y[0] * y;
max = boundary_x[0]*x + boundary_y[0]*y;
maxi = 0; maxi = 0;
for (kk = 0; kk < CNTPARTION; kk++) { for (kk = 0; kk < NUM_SECTOR; kk++)
tmp_scal = boundary_x[kk]*x + boundary_y[kk]*y; {
if (tmp_scal> max) { dotProd = boundary_x[kk] * x + boundary_y[kk] * y;
max = tmp_scal; if (dotProd > max)
{
max = dotProd;
maxi = kk; maxi = kk;
}else if (-tmp_scal> max) { }
max = -tmp_scal; else
maxi = kk + CNTPARTION; {
if (-dotProd > max)
{
max = -dotProd;
maxi = kk + NUM_SECTOR;
}
} }
} }
alfa[j * width * 2 + i * 2 ] = maxi % CNTPARTION; alfa[j * width * 2 + i * 2 ] = maxi % NUM_SECTOR;
alfa[j * width * 2 + i * 2 + 1] = maxi; alfa[j * width * 2 + i * 2 + 1] = maxi;
}/*for(i = 0; i < width; i++)*/ }/*for(i = 0; i < width; i++)*/
}/*for(j = 0; j < height; j++)*/ }/*for(j = 0; j < height; j++)*/
//ïîäñ÷åò âåñîâ è ñìåùåíèé //ïîäñ÷åò âåñîâ è ñìåùåíèé
nearest_x = (int *)malloc(sizeof(int) * k); nearest = (int *)malloc(sizeof(int ) * k);
nearest_y = (int *)malloc(sizeof(int) * k); w = (float*)malloc(sizeof(float) * (k * 2));
w = (float*)malloc(sizeof(float) * (k * 2));
for(i = 0; i < k / 2; i++) for(i = 0; i < k / 2; i++)
{ {
nearest_x[i] = -1; nearest[i] = -1;
nearest_y[i] = -1;
}/*for(i = 0; i < k / 2; i++)*/ }/*for(i = 0; i < k / 2; i++)*/
for(i = k / 2; i < k; i++) for(i = k / 2; i < k; i++)
{ {
nearest_x[i] = 1; nearest[i] = 1;
nearest_y[i] = 1;
}/*for(i = k / 2; i < k; i++)*/ }/*for(i = k / 2; i < k; i++)*/
for(j = 0; j < k / 2; j++) for(j = 0; j < k / 2; j++)
@ -160,44 +170,52 @@ int getFeatureMaps_dp(const IplImage* image,const int k, CvLSVMFeatureMap **map)
//èíòåðïîëÿöèÿ //èíòåðïîëÿöèÿ
for(i = 0; i < sizeY; i++) for(i = 0; i < sizeY; i++)
{ {
for(j = 0; j < sizeX; j++) for(j = 0; j < sizeX; j++)
{
for(ii = 0; ii < k; ii++)
{ {
for(ii = 0; ii < k; ii++) 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))
{ {
for(jj = 0; jj < k; jj++) d = (k * i + ii) * width + (j * k + jj);
{ (*map)->map[ i * stringSize + j * (*map)->numFeatures + alfa[d * 2 ]] +=
if ((i * k + ii > 0) && (i * k + ii < height - 1) && (j * k + jj > 0) && (j * k + jj < width - 1)) r[d] * w[ii * 2] * w[jj * 2];
{ (*map)->map[ i * stringSize + j * (*map)->numFeatures + alfa[d * 2 + 1] + NUM_SECTOR] +=
d = (k*i + ii)* width + (j*k + jj); r[d] * w[ii * 2] * w[jj * 2];
(*map)->Map[(i ) * strsz + (j ) * (*map)->p + alfa[d * 2 ] ] += if ((i + nearest[ii] >= 0) &&
r[d] * w[ii * 2 ] * w[jj * 2 ]; (i + nearest[ii] <= sizeY - 1))
(*map)->Map[(i ) * strsz + (j ) * (*map)->p + alfa[d * 2 + 1] + CNTPARTION] += {
r[d] * w[ii * 2 ] * w[jj * 2 ]; (*map)->map[(i + nearest[ii]) * stringSize + j * (*map)->numFeatures + alfa[d * 2 ] ] +=
if ((i + nearest_y[ii] >= 0) && (i + nearest_y[ii] <= sizeY - 1)) r[d] * w[ii * 2 + 1] * w[jj * 2 ];
{ (*map)->map[(i + nearest[ii]) * stringSize + j * (*map)->numFeatures + alfa[d * 2 + 1] + NUM_SECTOR] +=
(*map)->Map[(i + nearest_y[ii]) * strsz + (j ) * (*map)->p + alfa[d * 2 ] ] += r[d] * w[ii * 2 + 1] * w[jj * 2 ];
r[d] * w[ii * 2 + 1] * w[jj * 2 ]; }
(*map)->Map[(i + nearest_y[ii]) * strsz + (j ) * (*map)->p + alfa[d * 2 + 1] + CNTPARTION] += if ((j + nearest[jj] >= 0) &&
r[d] * w[ii * 2 + 1] * w[jj * 2 ]; (j + nearest[jj] <= sizeX - 1))
} {
if ((j + nearest_x[jj] >= 0) && (j + nearest_x[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 ) * strsz + (j + nearest_x[jj]) * (*map)->p + alfa[d * 2 ] ] += (*map)->map[i * stringSize + (j + nearest[jj]) * (*map)->numFeatures + alfa[d * 2 + 1] + NUM_SECTOR] +=
r[d] * w[ii * 2 ] * w[jj * 2 + 1]; r[d] * w[ii * 2] * w[jj * 2 + 1];
(*map)->Map[(i ) * strsz + (j + nearest_x[jj]) * (*map)->p + alfa[d * 2 + 1] + CNTPARTION] += }
r[d] * w[ii * 2 ] * w[jj * 2 + 1]; if ((i + nearest[ii] >= 0) &&
} (i + nearest[ii] <= sizeY - 1) &&
if ((i + nearest_y[ii] >= 0) && (i + nearest_y[ii] <= sizeY - 1) && (j + nearest_x[jj] >= 0) && (j + nearest_x[jj] <= sizeX - 1)) (j + nearest[jj] >= 0) &&
{ (j + nearest[jj] <= sizeX - 1))
(*map)->Map[(i + nearest_y[ii]) * strsz + (j + nearest_x[jj]) * (*map)->p + 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 ] ] +=
(*map)->Map[(i + nearest_y[ii]) * strsz + (j + nearest_x[jj]) * (*map)->p + alfa[d * 2 + 1] + CNTPARTION] += r[d] * w[ii * 2 + 1] * w[jj * 2 + 1];
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(jj = 0; jj < k; jj++)*/
}/*for(j = 1; j < sizeX - 1; j++)*/ }/*for(ii = 0; ii < k; ii++)*/
}/*for(j = 1; j < sizeX - 1; j++)*/
}/*for(i = 1; i < sizeY - 1; i++)*/ }/*for(i = 1; i < sizeY - 1; i++)*/
cvReleaseImage(&dx); cvReleaseImage(&dx);
@ -205,9 +223,8 @@ int getFeatureMaps_dp(const IplImage* image,const int k, CvLSVMFeatureMap **map)
free(w); free(w);
free(nearest_x); free(nearest);
free(nearest_y);
free(r); free(r);
free(alfa); free(alfa);
@ -218,7 +235,7 @@ int getFeatureMaps_dp(const IplImage* image,const int k, CvLSVMFeatureMap **map)
// Feature map Normalization and Truncation // Feature map Normalization and Truncation
// //
// API // API
// int normalizationAndTruncationFeatureMaps(featureMap *map, const float alfa); // int normalizeAndTruncate(featureMap *map, const float alfa);
// INPUT // INPUT
// map - feature map // map - feature map
// alfa - truncation threshold // alfa - truncation threshold
@ -227,114 +244,113 @@ int getFeatureMaps_dp(const IplImage* image,const int k, CvLSVMFeatureMap **map)
// RESULT // RESULT
// Error status // Error status
*/ */
int normalizationAndTruncationFeatureMaps(CvLSVMFeatureMap *map, const float alfa) int normalizeAndTruncate(CvLSVMFeatureMap *map, const float alfa)
{ {
int i,j, ii; int i,j, ii;
int sizeX, sizeY, p, pos, pp, xp, pos1, pos2; int sizeX, sizeY, p, pos, pp, xp, pos1, pos2;
float * part_noma; // norm of C(i, j) float * partOfNorm; // norm of C(i, j)
float * new_data; float * newData;
float norm_val; float valOfNorm;
sizeX = map->sizeX; sizeX = map->sizeX;
sizeY = map->sizeY; sizeY = map->sizeY;
part_noma = (float *)malloc (sizeof(float) * (sizeX * sizeY)); partOfNorm = (float *)malloc (sizeof(float) * (sizeX * sizeY));
p = map->xp / 3; p = NUM_SECTOR;
xp = NUM_SECTOR * 3;
pp = NUM_SECTOR * 12;
for(i = 0; i < sizeX * sizeY; i++) for(i = 0; i < sizeX * sizeY; i++)
{ {
norm_val = 0.0; valOfNorm = 0.0f;
pos = i * map->p; pos = i * map->numFeatures;
for(j = 0; j < p; j++) for(j = 0; j < p; j++)
{ {
norm_val += map->Map[pos + j] * map->Map[pos + j]; valOfNorm += map->map[pos + j] * map->map[pos + j];
}/*for(j = 0; j < p; j++)*/ }/*for(j = 0; j < p; j++)*/
part_noma[i] = norm_val; partOfNorm[i] = valOfNorm;
}/*for(i = 0; i < sizeX * sizeY; i++)*/ }/*for(i = 0; i < sizeX * sizeY; i++)*/
xp = map->xp;
pp = xp * 4;
sizeX -= 2; sizeX -= 2;
sizeY -= 2; sizeY -= 2;
new_data = (float *)malloc (sizeof(float) * (sizeX * sizeY * pp)); newData = (float *)malloc (sizeof(float) * (sizeX * sizeY * pp));
//normalization //normalization
for(i = 1; i <= sizeY; i++) for(i = 1; i <= sizeY; i++)
{ {
for(j = 1; j <= sizeX; j++) for(j = 1; j <= sizeX; j++)
{ {
norm_val = sqrtf( valOfNorm = sqrtf(
part_noma[(i )*(sizeX + 2) + (j )] + partOfNorm[(i )*(sizeX + 2) + (j )] +
part_noma[(i )*(sizeX + 2) + (j + 1)] + partOfNorm[(i )*(sizeX + 2) + (j + 1)] +
part_noma[(i + 1)*(sizeX + 2) + (j )] + partOfNorm[(i + 1)*(sizeX + 2) + (j )] +
part_noma[(i + 1)*(sizeX + 2) + (j + 1)]); partOfNorm[(i + 1)*(sizeX + 2) + (j + 1)]);
pos1 = (i ) * (sizeX + 2) * xp + (j ) * xp; pos1 = (i ) * (sizeX + 2) * xp + (j ) * xp;
pos2 = (i-1) * (sizeX ) * pp + (j-1) * pp; pos2 = (i-1) * (sizeX ) * pp + (j-1) * pp;
for(ii = 0; ii < p; ii++) for(ii = 0; ii < p; ii++)
{ {
new_data[pos2 + ii ] = map->Map[pos1 + ii ] / norm_val; newData[pos2 + ii ] = map->map[pos1 + ii ] / valOfNorm;
}/*for(ii = 0; ii < p; ii++)*/ }/*for(ii = 0; ii < p; ii++)*/
for(ii = 0; ii < 2 * p; ii++) for(ii = 0; ii < 2 * p; ii++)
{ {
new_data[pos2 + ii + p * 4] = map->Map[pos1 + ii + p] / norm_val; newData[pos2 + ii + p * 4] = map->map[pos1 + ii + p] / valOfNorm;
}/*for(ii = 0; ii < 2 * p; ii++)*/ }/*for(ii = 0; ii < 2 * p; ii++)*/
norm_val = sqrtf( valOfNorm = sqrtf(
part_noma[(i )*(sizeX + 2) + (j )] + partOfNorm[(i )*(sizeX + 2) + (j )] +
part_noma[(i )*(sizeX + 2) + (j + 1)] + partOfNorm[(i )*(sizeX + 2) + (j + 1)] +
part_noma[(i - 1)*(sizeX + 2) + (j )] + partOfNorm[(i - 1)*(sizeX + 2) + (j )] +
part_noma[(i - 1)*(sizeX + 2) + (j + 1)]); partOfNorm[(i - 1)*(sizeX + 2) + (j + 1)]);
for(ii = 0; ii < p; ii++) for(ii = 0; ii < p; ii++)
{ {
new_data[pos2 + ii + p ] = map->Map[pos1 + ii ] / norm_val; newData[pos2 + ii + p ] = map->map[pos1 + ii ] / valOfNorm;
}/*for(ii = 0; ii < p; ii++)*/ }/*for(ii = 0; ii < p; ii++)*/
for(ii = 0; ii < 2 * p; ii++) for(ii = 0; ii < 2 * p; ii++)
{ {
new_data[pos2 + ii + p * 6] = map->Map[pos1 + ii + p] / norm_val; newData[pos2 + ii + p * 6] = map->map[pos1 + ii + p] / valOfNorm;
}/*for(ii = 0; ii < 2 * p; ii++)*/ }/*for(ii = 0; ii < 2 * p; ii++)*/
norm_val = sqrtf( valOfNorm = sqrtf(
part_noma[(i )*(sizeX + 2) + (j )] + partOfNorm[(i )*(sizeX + 2) + (j )] +
part_noma[(i )*(sizeX + 2) + (j - 1)] + partOfNorm[(i )*(sizeX + 2) + (j - 1)] +
part_noma[(i + 1)*(sizeX + 2) + (j )] + partOfNorm[(i + 1)*(sizeX + 2) + (j )] +
part_noma[(i + 1)*(sizeX + 2) + (j - 1)]); partOfNorm[(i + 1)*(sizeX + 2) + (j - 1)]);
for(ii = 0; ii < p; ii++) for(ii = 0; ii < p; ii++)
{ {
new_data[pos2 + ii + p * 2] = map->Map[pos1 + ii ] / norm_val; newData[pos2 + ii + p * 2] = map->map[pos1 + ii ] / valOfNorm;
}/*for(ii = 0; ii < p; ii++)*/ }/*for(ii = 0; ii < p; ii++)*/
for(ii = 0; ii < 2 * p; ii++) for(ii = 0; ii < 2 * p; ii++)
{ {
new_data[pos2 + ii + p * 8] = map->Map[pos1 + ii + p] / norm_val; newData[pos2 + ii + p * 8] = map->map[pos1 + ii + p] / valOfNorm;
}/*for(ii = 0; ii < 2 * p; ii++)*/ }/*for(ii = 0; ii < 2 * p; ii++)*/
norm_val = sqrtf( valOfNorm = sqrtf(
part_noma[(i )*(sizeX + 2) + (j )] + partOfNorm[(i )*(sizeX + 2) + (j )] +
part_noma[(i )*(sizeX + 2) + (j - 1)] + partOfNorm[(i )*(sizeX + 2) + (j - 1)] +
part_noma[(i - 1)*(sizeX + 2) + (j )] + partOfNorm[(i - 1)*(sizeX + 2) + (j )] +
part_noma[(i - 1)*(sizeX + 2) + (j - 1)]); partOfNorm[(i - 1)*(sizeX + 2) + (j - 1)]);
for(ii = 0; ii < p; ii++) for(ii = 0; ii < p; ii++)
{ {
new_data[pos2 + ii + p * 3 ] = map->Map[pos1 + ii ] / norm_val; newData[pos2 + ii + p * 3 ] = map->map[pos1 + ii ] / valOfNorm;
}/*for(ii = 0; ii < p; ii++)*/ }/*for(ii = 0; ii < p; ii++)*/
for(ii = 0; ii < 2 * p; ii++) for(ii = 0; ii < 2 * p; ii++)
{ {
new_data[pos2 + ii + p * 10] = map->Map[pos1 + ii + p] / norm_val; newData[pos2 + ii + p * 10] = map->map[pos1 + ii + p] / valOfNorm;
}/*for(ii = 0; ii < 2 * p; ii++)*/ }/*for(ii = 0; ii < 2 * p; ii++)*/
}/*for(j = 1; j <= sizeX; j++)*/ }/*for(j = 1; j <= sizeX; j++)*/
}/*for(i = 1; i <= sizeY; i++)*/ }/*for(i = 1; i <= sizeY; i++)*/
//truncation //truncation
for(i = 0; i < sizeX * sizeY * pp; i++) for(i = 0; i < sizeX * sizeY * pp; i++)
{ {
if(new_data [i] > alfa) new_data [i] = alfa; if(newData [i] > alfa) newData [i] = alfa;
}/*for(i = 0; i < sizeX * sizeY * pp; i++)*/ }/*for(i = 0; i < sizeX * sizeY * pp; i++)*/
//swop data //swop data
map->p = pp; map->numFeatures = pp;
map->xp = xp;
map->sizeX = sizeX; map->sizeX = sizeX;
map->sizeY = sizeY; map->sizeY = sizeY;
free (map->Map); free (map->map);
free (part_noma); free (partOfNorm);
map->Map = new_data; map->map = newData;
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
@ -356,21 +372,21 @@ int PCAFeatureMaps(CvLSVMFeatureMap *map)
{ {
int i,j, ii, jj, k; int i,j, ii, jj, k;
int sizeX, sizeY, p, pp, xp, yp, pos1, pos2; int sizeX, sizeY, p, pp, xp, yp, pos1, pos2;
float * new_data; float * newData;
float val; float val;
float nx, ny; float nx, ny;
sizeX = map->sizeX; sizeX = map->sizeX;
sizeY = map->sizeY; sizeY = map->sizeY;
p = map->p; p = map->numFeatures;
pp = map->xp + 4; pp = NUM_SECTOR * 3 + 4;
yp = 4; yp = 4;
xp = (map->xp / 3); xp = NUM_SECTOR;
nx = 1.0f / sqrtf((float)(xp * 2)); nx = 1.0f / sqrtf((float)(xp * 2));
ny = 1.0f / sqrtf((float)(yp )); ny = 1.0f / sqrtf((float)(yp ));
new_data = (float *)malloc (sizeof(float) * (sizeX * sizeY * pp)); newData = (float *)malloc (sizeof(float) * (sizeX * sizeY * pp));
for(i = 0; i < sizeY; i++) for(i = 0; i < sizeY; i++)
{ {
@ -384,9 +400,9 @@ int PCAFeatureMaps(CvLSVMFeatureMap *map)
val = 0; val = 0;
for(ii = 0; ii < yp; ii++) for(ii = 0; ii < yp; ii++)
{ {
val += map->Map[pos1 + yp * xp + ii * xp * 2 + jj]; val += map->map[pos1 + yp * xp + ii * xp * 2 + jj];
}/*for(ii = 0; ii < yp; ii++)*/ }/*for(ii = 0; ii < yp; ii++)*/
new_data[pos2 + k] = val * ny; newData[pos2 + k] = val * ny;
k++; k++;
}/*for(jj = 0; jj < xp * 2; jj++)*/ }/*for(jj = 0; jj < xp * 2; jj++)*/
for(jj = 0; jj < xp; jj++) for(jj = 0; jj < xp; jj++)
@ -394,9 +410,9 @@ int PCAFeatureMaps(CvLSVMFeatureMap *map)
val = 0; val = 0;
for(ii = 0; ii < yp; ii++) for(ii = 0; ii < yp; ii++)
{ {
val += map->Map[pos1 + ii * xp + jj]; val += map->map[pos1 + ii * xp + jj];
}/*for(ii = 0; ii < yp; ii++)*/ }/*for(ii = 0; ii < yp; ii++)*/
new_data[pos2 + k] = val * ny; newData[pos2 + k] = val * ny;
k++; k++;
}/*for(jj = 0; jj < xp; jj++)*/ }/*for(jj = 0; jj < xp; jj++)*/
for(ii = 0; ii < yp; ii++) for(ii = 0; ii < yp; ii++)
@ -404,25 +420,47 @@ int PCAFeatureMaps(CvLSVMFeatureMap *map)
val = 0; val = 0;
for(jj = 0; jj < 2 * xp; jj++) for(jj = 0; jj < 2 * xp; jj++)
{ {
val += map->Map[pos1 + yp * xp + ii * xp * 2 + jj]; val += map->map[pos1 + yp * xp + ii * xp * 2 + jj];
}/*for(jj = 0; jj < xp; jj++)*/ }/*for(jj = 0; jj < xp; jj++)*/
new_data[pos2 + k] = val * nx; newData[pos2 + k] = val * nx;
k++; k++;
} /*for(ii = 0; ii < yp; ii++)*/ } /*for(ii = 0; ii < yp; ii++)*/
}/*for(j = 0; j < sizeX; j++)*/ }/*for(j = 0; j < sizeX; j++)*/
}/*for(i = 0; i < sizeY; i++)*/ }/*for(i = 0; i < sizeY; i++)*/
//swop data //swop data
map->p = pp; map->numFeatures = pp;
map->xp = pp;
free (map->Map); free (map->map);
map->Map = new_data; map->map = newData;
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
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++)*/
return LATENT_SVM_OK;
}
/* /*
// Getting feature pyramid // Getting feature pyramid
// //
@ -434,145 +472,52 @@ int PCAFeatureMaps(CvLSVMFeatureMap *map)
const int W, const int H, featurePyramid **maps); const int W, const int H, featurePyramid **maps);
// INPUT // INPUT
// image - image // image - image
// lambda - resize scale
// k - size of cells
// startX - X coordinate of the image rectangle to search
// startY - Y coordinate of the image rectangle to search
// W - width of the image rectangle to search
// H - height of the image rectangle to search
// OUTPUT // OUTPUT
// maps - feature maps for all levels // maps - feature maps for all levels
// RESULT // RESULT
// Error status // Error status
*/ */
int getFeaturePyramid(IplImage * image, int getFeaturePyramid(IplImage * image, CvLSVMFeaturePyramid **maps)
const int lambda, const int k,
const int startX, const int startY,
const int W, const int H, CvLSVMFeaturePyramid **maps)
{ {
IplImage *img2, *imgTmp, *imgResize; IplImage *imgResize;
float step, tmp; float step;
int cntStep; int numStep;
int maxcall; int maxNumCells;
int i; int W, H;
int err;
CvLSVMFeatureMap *map;
//geting subimage if(image->depth == IPL_DEPTH_32F)
cvSetImageROI(image, cvRect(startX, startY, W, H));
img2 = cvCreateImage(cvGetSize(image), image->depth, image->nChannels);
cvCopy(image, img2, NULL);
cvResetImageROI(image);
if(img2->depth != IPL_DEPTH_32F)
{ {
imgResize = cvCreateImage(cvSize(img2->width , img2->height) , IPL_DEPTH_32F , 3); imgResize = image;
cvConvert(img2, imgResize);
} }
else else
{ {
imgResize = img2; imgResize = cvCreateImage(cvSize(image->width , image->height) ,
IPL_DEPTH_32F , 3);
cvConvert(image, imgResize);
} }
step = powf(2.0f, 1.0f/ ((float)lambda)); W = imgResize->width;
maxcall = W/k; H = imgResize->height;
if( maxcall > H/k )
step = powf(2.0f, 1.0f / ((float)LAMBDA));
maxNumCells = W / SIDE_LENGTH;
if( maxNumCells > H / SIDE_LENGTH )
{ {
maxcall = H/k; maxNumCells = H / SIDE_LENGTH;
} }
cntStep = (int)(logf((float)maxcall/(5.0f))/logf(step)) + 1; numStep = (int)(logf((float) maxNumCells / (5.0f)) / logf( step )) + 1;
//printf("Count step: %f %d\n", step, cntStep);
allocFeaturePyramidObject(maps, numStep + LAMBDA);
allocFeaturePyramidObject(maps, lambda, cntStep + lambda); getPathOfFeaturePyramid(imgResize, step , LAMBDA, 0,
SIDE_LENGTH / 2, maps);
for(i = 0; i < lambda; i++) getPathOfFeaturePyramid(imgResize, step, numStep, LAMBDA,
{ SIDE_LENGTH , maps);
tmp = 1.0f / powf(step, (float)i);
imgTmp = resize_opencv (imgResize, tmp); if(image->depth != IPL_DEPTH_32F)
//imgTmp = resize_article_dp(img2, tmp, 4);
err = getFeatureMaps_dp(imgTmp, 4, &map);
err = normalizationAndTruncationFeatureMaps(map, 0.2f);
err = PCAFeatureMaps(map);
(*maps)->pyramid[i] = map;
//printf("%d, %d\n", map->sizeY, map->sizeX);
cvReleaseImage(&imgTmp);
}
/**********************************one**************/
for(i = 0; i < cntStep; i++)
{
tmp = 1.0f / powf(step, (float)i);
imgTmp = resize_opencv (imgResize, tmp);
//imgTmp = resize_article_dp(imgResize, tmp, 8);
err = getFeatureMaps_dp(imgTmp, 8, &map);
err = normalizationAndTruncationFeatureMaps(map, 0.2f);
err = PCAFeatureMaps(map);
(*maps)->pyramid[i + lambda] = map;
//printf("%d, %d\n", map->sizeY, map->sizeX);
cvReleaseImage(&imgTmp);
}/*for(i = 0; i < cntStep; i++)*/
if(img2->depth != IPL_DEPTH_32F)
{ {
cvReleaseImage(&imgResize); cvReleaseImage(&imgResize);
} }
cvReleaseImage(&img2);
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
/*
// add zero border to feature map
//
// API
// int addBordersToFeatureMaps(featureMap *map, const int bX, const int bY);
// INPUT
// map - feature map
// bX - border size in x
// bY - border size in y
// OUTPUT
// map - feature map
// RESULT
// Error status
*/
int addBordersToFeatureMaps(CvLSVMFeatureMap *map, const int bX, const int bY){
int i,j, jj;
int sizeX, sizeY, p, pos1, pos2;
float * new_data;
sizeX = map->sizeX;
sizeY = map->sizeY;
p = map->p;
new_data = (float *)malloc (sizeof(float) * ((sizeX + 2 * bX) * (sizeY + 2 * bY) * p));
for(i = 0; i < ((sizeX + 2 * bX) * (sizeY + 2 * bY) * p); i++)
{
new_data[i] = (float)0;
}/*for(i = 0; i < ((sizeX + 2 * bX) * (sizeY + 2 * bY) * p); i++)*/
for(i = 0; i < sizeY; i++)
{
for(j = 0; j < sizeX; j++)
{
pos1 = ((i )*sizeX + (j )) * p;
pos2 = ((i + bY)*(sizeX + 2 * bX) + (j + bX)) * p;
for(jj = 0; jj < p; jj++)
{
new_data[pos2 + jj] = map->Map[pos1 + jj];
}/*for(jj = 0; jj < p; jj++)*/
}/*for(j = 0; j < sizeX; j++)*/
}/*for(i = 0; i < sizeY; i++)*/
//swop data
map->sizeX = sizeX + 2 * bX;
map->sizeY = sizeY + 2 * bY;
free (map->Map);
map->Map = new_data;
return LATENT_SVM_OK;
}

View File

@ -127,8 +127,7 @@ CvLSVMFeaturePyramid* createFeaturePyramidWithBorder(IplImage *image,
CvLSVMFeaturePyramid *H; CvLSVMFeaturePyramid *H;
// Obtaining feature pyramid // Obtaining feature pyramid
opResult = getFeaturePyramid(image, LAMBDA, SIDE_LENGTH, 0, 0, opResult = getFeaturePyramid(image, &H);
image->width, image->height, &H);
if (opResult != LATENT_SVM_OK) if (opResult != LATENT_SVM_OK)
{ {
@ -139,7 +138,7 @@ CvLSVMFeaturePyramid* createFeaturePyramidWithBorder(IplImage *image,
// Addition nullable border for each feature map // Addition nullable border for each feature map
// the size of the border for root filters // the size of the border for root filters
computeBorderSize(maxXBorder, maxYBorder, &bx, &by); computeBorderSize(maxXBorder, maxYBorder, &bx, &by);
for (level = 0; level < H->countLevel; level++) for (level = 0; level < H->numLevels; level++)
{ {
addNullableBorder(H->pyramid[level], bx, by); addNullableBorder(H->pyramid[level], bx, by);
} }
@ -196,7 +195,7 @@ int searchObject(const CvLSVMFeaturePyramid *H, const CvLSVMFilterObject **all_F
// Transformation filter displacement from the block space // Transformation filter displacement from the block space
// to the space of pixels at the initial image // to the space of pixels at the initial image
// that settles at the level number LAMBDA // that settles at the level number LAMBDA
convertPoints(H->countLevel, H->lambda, LAMBDA, (*points), convertPoints(H->numLevels, LAMBDA, LAMBDA, (*points),
(*levels), (*partsDisplacement), (*kPoints), n, (*levels), (*partsDisplacement), (*kPoints), n,
maxXBorder, maxYBorder); maxXBorder, maxYBorder);
@ -305,7 +304,7 @@ int searchObjectThreshold(const CvLSVMFeaturePyramid *H,
// Transformation filter displacement from the block space // Transformation filter displacement from the block space
// to the space of pixels at the initial image // to the space of pixels at the initial image
// that settles at the level number LAMBDA // that settles at the level number LAMBDA
convertPoints(H->countLevel, H->lambda, LAMBDA, (*points), convertPoints(H->numLevels, LAMBDA, LAMBDA, (*points),
(*levels), (*partsDisplacement), (*kPoints), n, (*levels), (*partsDisplacement), (*kPoints), n,
maxXBorder, maxYBorder); maxXBorder, maxYBorder);

View File

@ -658,8 +658,7 @@ void parserModel(FILE * xmlf, CvLSVMFilterObject *** model, int *last, int *max,
if(tagVal == EMODEL){ if(tagVal == EMODEL){
//printf("</Model>\n"); //printf("</Model>\n");
for(ii = 0; ii <= *last; ii++){ for(ii = 0; ii <= *last; ii++){
(*model)[ii]->p = p; (*model)[ii]->numFeatures = p;
(*model)[ii]->xp = 9;
} }
* count = N_comp; * count = N_comp;
return; return;

View File

@ -33,7 +33,7 @@ int convolution(const CvLSVMFilterObject *Fi, const CvLSVMFeatureMap *map, float
m1 = map->sizeX; m1 = map->sizeX;
n2 = Fi->sizeY; n2 = Fi->sizeY;
m2 = Fi->sizeX; m2 = Fi->sizeX;
p = map->p; p = map->numFeatures;
diff1 = n1 - n2 + 1; diff1 = n1 - n2 + 1;
diff2 = m1 - m2 + 1; diff2 = m1 - m2 + 1;
@ -51,7 +51,7 @@ int convolution(const CvLSVMFilterObject *Fi, const CvLSVMFeatureMap *map, float
{ {
for (j2 = 0; j2 < m2; j2++) for (j2 = 0; j2 < m2; j2++)
{ {
pMap = map->Map + (i1 + i2) * m1 * p + (j1 + j2) * p;//sm2 pMap = map->map + (i1 + i2) * m1 * p + (j1 + j2) * p;//sm2
pH = Fi->H + (i2 * m2 + j2) * p;//sm2 pH = Fi->H + (i2 * m2 + j2) * p;//sm2
for (k = 0; k < p/4; k++) for (k = 0; k < p/4; k++)
{ {
@ -210,14 +210,14 @@ int getFFTImageFilterObject(const CvLSVMFilterObject *filter,
mapSize = mapDimX * mapDimY; mapSize = mapDimX * mapDimY;
newFilter = (float *)malloc(sizeof(float) * (2 * mapSize)); newFilter = (float *)malloc(sizeof(float) * (2 * mapSize));
rot2PIFilter = (float *)malloc(sizeof(float) * filterSize); rot2PIFilter = (float *)malloc(sizeof(float) * filterSize);
res = allocFFTImage(image, filter->p, mapDimX, mapDimY); res = allocFFTImage(image, filter->numFeatures, mapDimX, mapDimY);
if (res != LATENT_SVM_OK) if (res != LATENT_SVM_OK)
{ {
return res; return res;
} }
for (i = 0; i < filter->p; i++) for (i = 0; i < filter->numFeatures; i++)
{ {
rot2PI(filter->H, filter->sizeX, filter->sizeY, rot2PIFilter, filter->p, i); rot2PI(filter->H, filter->sizeX, filter->sizeY, rot2PIFilter, filter->numFeatures, i);
addNullableBars(rot2PIFilter, filter->sizeX, filter->sizeY, addNullableBars(rot2PIFilter, filter->sizeX, filter->sizeY,
newFilter, mapDimX, mapDimY); newFilter, mapDimX, mapDimY);
fft2d(newFilter, (*image)->channels[i], mapDimY, mapDimX); fft2d(newFilter, (*image)->channels[i], mapDimY, mapDimX);
@ -241,14 +241,14 @@ int getFFTImageFeatureMap(const CvLSVMFeatureMap *map, CvLSVMFftImage **image)
{ {
int i, j, size; int i, j, size;
float *buf; float *buf;
allocFFTImage(image, map->p, map->sizeX, map->sizeY); allocFFTImage(image, map->numFeatures, map->sizeX, map->sizeY);
size = map->sizeX * map->sizeY; size = map->sizeX * map->sizeY;
buf = (float *)malloc(sizeof(float) * (2 * size)); buf = (float *)malloc(sizeof(float) * (2 * size));
for (i = 0; i < map->p; i++) for (i = 0; i < map->numFeatures; i++)
{ {
for (j = 0; j < size; j++) for (j = 0; j < size; j++)
{ {
buf[2 * j] = map->Map[j * map->p + i]; buf[2 * j] = map->map[j * map->numFeatures + i];
buf[2 * j + 1] = 0.0; buf[2 * j + 1] = 0.0;
} }
fft2d(buf, (*image)->channels[i], map->sizeY, map->sizeX); fft2d(buf, (*image)->channels[i], map->sizeY, map->sizeX);
@ -282,7 +282,7 @@ int convFFTConv2d(const CvLSVMFftImage *featMapImage, const CvLSVMFftImage *filt
imagesMultRes = (float *)malloc(sizeof(float) * size); imagesMultRes = (float *)malloc(sizeof(float) * size);
fftImagesMulti(featMapImage->channels[0], filterImage->channels[0], fftImagesMulti(featMapImage->channels[0], filterImage->channels[0],
featMapImage->dimY, featMapImage->dimX, imagesMultRes); featMapImage->dimY, featMapImage->dimX, imagesMultRes);
for (i = 1; (i < (int)featMapImage->p) && (i < (int)filterImage->p); i++) for (i = 1; (i < (int)featMapImage->numFeatures) && (i < (int)filterImage->numFeatures); i++)
{ {
fftImagesMulti(featMapImage->channels[i],filterImage->channels[i], fftImagesMulti(featMapImage->channels[i],filterImage->channels[i],
featMapImage->dimY, featMapImage->dimX, imagesMult); featMapImage->dimY, featMapImage->dimX, imagesMult);
@ -343,7 +343,7 @@ int filterDispositionLevel(const CvLSVMFilterObject *Fi, const CvLSVMFeatureMap
m1 = pyramid->sizeX; m1 = pyramid->sizeX;
n2 = Fi->sizeY; n2 = Fi->sizeY;
m2 = Fi->sizeX; m2 = Fi->sizeX;
p = pyramid->p; p = pyramid->numFeatures;
(*scoreFi) = NULL; (*scoreFi) = NULL;
(*pointsX) = NULL; (*pointsX) = NULL;
(*pointsY) = NULL; (*pointsY) = NULL;
@ -429,7 +429,7 @@ int filterDispositionLevelFFT(const CvLSVMFilterObject *Fi, const CvLSVMFftImage
m1 = featMapImage->dimX; m1 = featMapImage->dimX;
n2 = Fi->sizeY; n2 = Fi->sizeY;
m2 = Fi->sizeX; m2 = Fi->sizeX;
p = featMapImage->p; p = featMapImage->numFeatures;
(*scoreFi) = NULL; (*scoreFi) = NULL;
(*pointsX) = NULL; (*pointsX) = NULL;
(*pointsY) = NULL; (*pointsY) = NULL;
@ -525,8 +525,8 @@ int addNullableBorder(CvLSVMFeatureMap *map, int bx, int by)
float *new_map; float *new_map;
sizeX = map->sizeX + 2 * bx; sizeX = map->sizeX + 2 * bx;
sizeY = map->sizeY + 2 * by; sizeY = map->sizeY + 2 * by;
new_map = (float *)malloc(sizeof(float) * sizeX * sizeY * map->p); new_map = (float *)malloc(sizeof(float) * sizeX * sizeY * map->numFeatures);
for (i = 0; i < sizeX * sizeY * map->p; i++) for (i = 0; i < sizeX * sizeY * map->numFeatures; i++)
{ {
new_map[i] = 0.0; new_map[i] = 0.0;
} }
@ -534,17 +534,17 @@ int addNullableBorder(CvLSVMFeatureMap *map, int bx, int by)
{ {
for (j = bx; j < map->sizeX + bx; j++) for (j = bx; j < map->sizeX + bx; j++)
{ {
for (k = 0; k < map->p; k++) for (k = 0; k < map->numFeatures; k++)
{ {
new_map[(i * sizeX + j) * map->p + k] = new_map[(i * sizeX + j) * map->numFeatures + k] =
map->Map[((i - by) * map->sizeX + j - bx) * map->p + k]; map->map[((i - by) * map->sizeX + j - bx) * map->numFeatures + k];
} }
} }
} }
map->sizeX = sizeX; map->sizeX = sizeX;
map->sizeY = sizeY; map->sizeY = sizeY;
free(map->Map); free(map->map);
map->Map = new_map; map->map = new_map;
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
@ -558,19 +558,19 @@ CvLSVMFeatureMap* featureMapBorderPartFilter(CvLSVMFeatureMap *map,
computeBorderSize(maxXBorder, maxYBorder, &bx, &by); computeBorderSize(maxXBorder, maxYBorder, &bx, &by);
sizeX = map->sizeX + 2 * bx; sizeX = map->sizeX + 2 * bx;
sizeY = map->sizeY + 2 * by; sizeY = map->sizeY + 2 * by;
allocFeatureMapObject(&new_map, sizeX, sizeY, map->p, map->xp); allocFeatureMapObject(&new_map, sizeX, sizeY, map->numFeatures);
for (i = 0; i < sizeX * sizeY * map->p; i++) for (i = 0; i < sizeX * sizeY * map->numFeatures; i++)
{ {
new_map->Map[i] = 0.0; new_map->map[i] = 0.0f;
} }
for (i = by; i < map->sizeY + by; i++) for (i = by; i < map->sizeY + by; i++)
{ {
for (j = bx; j < map->sizeX + bx; j++) for (j = bx; j < map->sizeX + bx; j++)
{ {
for (k = 0; k < map->p; k++) for (k = 0; k < map->numFeatures; k++)
{ {
new_map->Map[(i * sizeX + j) * map->p + k] = new_map->map[(i * sizeX + j) * map->numFeatures + k] =
map->Map[((i - by) * map->sizeX + j - bx) * map->p + k]; map->map[((i - by) * map->sizeX + j - bx) * map->numFeatures + k];
} }
} }
} }
@ -640,7 +640,7 @@ int maxFunctionalScoreFixedLevel(const CvLSVMFilterObject **all_F, int n,
dimY = H->pyramid[level]->sizeY; dimY = H->pyramid[level]->sizeY;
// Number of features // Number of features
p = H->pyramid[level]->p; p = H->pyramid[level]->numFeatures;
// Getting dimension of root filter // Getting dimension of root filter
nF0 = all_F[0]->sizeY; nF0 = all_F[0]->sizeY;
@ -888,7 +888,7 @@ int thresholdFunctionalScoreFixedLevel(const CvLSVMFilterObject **all_F, int n,
dimY = H->pyramid[level]->sizeY; dimY = H->pyramid[level]->sizeY;
// Number of features // Number of features
p = H->pyramid[level]->p; p = H->pyramid[level]->numFeatures;
// Getting dimension of root filter // Getting dimension of root filter
nF0 = all_F[0]->sizeY; nF0 = all_F[0]->sizeY;
@ -1109,7 +1109,7 @@ int maxFunctionalScore(const CvLSVMFilterObject **all_F, int n,
// Computation the number of levels for seaching object, // Computation the number of levels for seaching object,
// first lambda-levels are used for computation values // first lambda-levels are used for computation values
// of score function for each position of root filter // of score function for each position of root filter
numLevels = H->countLevel - H->lambda; numLevels = H->numLevels - LAMBDA;
// Allocation memory for maximum value of score function for each level // Allocation memory for maximum value of score function for each level
tmpScore = (float *)malloc(sizeof(float) * numLevels); tmpScore = (float *)malloc(sizeof(float) * numLevels);
@ -1135,7 +1135,7 @@ int maxFunctionalScore(const CvLSVMFilterObject **all_F, int n,
} }
// Set current value of the maximum of score function // Set current value of the maximum of score function
res = maxFunctionalScoreFixedLevel(all_F, n, H, H->lambda, b, res = maxFunctionalScoreFixedLevel(all_F, n, H, LAMBDA, b,
maxXBorder, maxYBorder, maxXBorder, maxYBorder,
&(tmpScore[0]), &(tmpScore[0]),
tmpPoints[0], tmpPoints[0],
@ -1150,9 +1150,9 @@ int maxFunctionalScore(const CvLSVMFilterObject **all_F, int n,
file = fopen("maxScore.csv", "w+"); file = fopen("maxScore.csv", "w+");
fprintf(file, "%i;%lf;\n", H->lambda, tmpScore[0]); fprintf(file, "%i;%lf;\n", H->lambda, tmpScore[0]);
//*/ //*/
for (l = H->lambda + 1; l < H->countLevel; l++) for (l = LAMBDA + 1; l < H->numLevels; l++)
{ {
k = l - H->lambda; k = l - LAMBDA;
res = maxFunctionalScoreFixedLevel(all_F, n, H, l, b, res = maxFunctionalScoreFixedLevel(all_F, n, H, l, b,
maxXBorder, maxYBorder, maxXBorder, maxYBorder,
&(tmpScore[k]), &(tmpScore[k]),
@ -1191,7 +1191,7 @@ int maxFunctionalScore(const CvLSVMFilterObject **all_F, int n,
if ((tmpScore[i] - maxScore) * (tmpScore[i] - maxScore) <= EPS) if ((tmpScore[i] - maxScore) * (tmpScore[i] - maxScore) <= EPS)
{ {
// Computation the number of level // Computation the number of level
level = i + H->lambda; level = i + LAMBDA;
// Addition a set of points // Addition a set of points
f += tmpKPoints[i]; f += tmpKPoints[i];
@ -1272,7 +1272,7 @@ int thresholdFunctionalScore(const CvLSVMFilterObject **all_F, int n,
// Computation the number of levels for seaching object, // Computation the number of levels for seaching object,
// first lambda-levels are used for computation values // first lambda-levels are used for computation values
// of score function for each position of root filter // of score function for each position of root filter
numLevels = H->countLevel - H->lambda; numLevels = H->numLevels - LAMBDA;
// Allocation memory for values of score function for each level // Allocation memory for values of score function for each level
// that exceed threshold // that exceed threshold
@ -1305,9 +1305,9 @@ int thresholdFunctionalScore(const CvLSVMFilterObject **all_F, int n,
fprintf(file, "%i;%lf;\n", H->lambda, tmpScore[0]); fprintf(file, "%i;%lf;\n", H->lambda, tmpScore[0]);
//*/ //*/
(*kPoints) = 0; (*kPoints) = 0;
for (l = H->lambda; l < H->countLevel; l++) for (l = LAMBDA; l < H->numLevels; l++)
{ {
k = l - H->lambda; k = l - LAMBDA;
//printf("Score at the level %i\n", l); //printf("Score at the level %i\n", l);
res = thresholdFunctionalScoreFixedLevel(all_F, n, H, l, b, res = thresholdFunctionalScoreFixedLevel(all_F, n, H, l, b,
maxXBorder, maxYBorder, scoreThreshold, maxXBorder, maxYBorder, scoreThreshold,
@ -1339,7 +1339,7 @@ int thresholdFunctionalScore(const CvLSVMFilterObject **all_F, int n,
for (i = 0; i < numLevels; i++) for (i = 0; i < numLevels; i++)
{ {
// Computation the number of level // Computation the number of level
level = i + H->lambda; level = i + LAMBDA;
// Addition a set of points // Addition a set of points
f += tmpKPoints[i]; f += tmpKPoints[i];
@ -1411,7 +1411,7 @@ int createSchedule(const CvLSVMFeaturePyramid *H, const CvLSVMFilterObject **all
sumPartFiltersDim += all_F[i]->sizeX * all_F[i]->sizeY; sumPartFiltersDim += all_F[i]->sizeX * all_F[i]->sizeY;
} }
// Number of levels which are used for computation of score function // Number of levels which are used for computation of score function
numLevels = H->countLevel - H->lambda; numLevels = H->numLevels - LAMBDA;
// Allocation memory for saving number of dot products that will be // Allocation memory for saving number of dot products that will be
// computed for each level of feature pyramid // computed for each level of feature pyramid
dotProd = (int *)malloc(sizeof(int) * numLevels); dotProd = (int *)malloc(sizeof(int) * numLevels);
@ -1421,7 +1421,7 @@ int createSchedule(const CvLSVMFeaturePyramid *H, const CvLSVMFilterObject **all
dby = 2 * by; dby = 2 * by;
// Total number of dot products for all levels // Total number of dot products for all levels
numDotProducts = 0; numDotProducts = 0;
lambda = H->lambda; lambda = LAMBDA;
for (i = 0; i < numLevels; i++) for (i = 0; i < numLevels; i++)
{ {
dotProd[i] = H->pyramid[i + lambda]->sizeX * dotProd[i] = H->pyramid[i + lambda]->sizeX *

View File

@ -1,13 +1,14 @@
#include "precomp.hpp" #include "precomp.hpp"
#include "_lsvm_routine.h" #include "_lsvm_routine.h"
int allocFilterObject(CvLSVMFilterObject **obj, const int sizeX, const int sizeY, const int p, const int xp){ int allocFilterObject(CvLSVMFilterObject **obj, const int sizeX,
const int sizeY, const int numFeatures)
{
int i; int i;
(*obj) = (CvLSVMFilterObject *)malloc(sizeof(CvLSVMFilterObject)); (*obj) = (CvLSVMFilterObject *)malloc(sizeof(CvLSVMFilterObject));
(*obj)->sizeX = sizeX; (*obj)->sizeX = sizeX;
(*obj)->sizeY = sizeY; (*obj)->sizeY = sizeY;
(*obj)->p = p ; (*obj)->numFeatures = numFeatures;
(*obj)->xp = xp ;
(*obj)->fineFunction[0] = 0.0f; (*obj)->fineFunction[0] = 0.0f;
(*obj)->fineFunction[1] = 0.0f; (*obj)->fineFunction[1] = 0.0f;
(*obj)->fineFunction[2] = 0.0f; (*obj)->fineFunction[2] = 0.0f;
@ -15,75 +16,87 @@ int allocFilterObject(CvLSVMFilterObject **obj, const int sizeX, const int sizeY
(*obj)->V.x = 0; (*obj)->V.x = 0;
(*obj)->V.y = 0; (*obj)->V.y = 0;
(*obj)->V.l = 0; (*obj)->V.l = 0;
(*obj)->H = (float *) malloc(sizeof (float) * (sizeX * sizeY * p)); (*obj)->H = (float *) malloc(sizeof (float) *
for(i = 0; i < sizeX * sizeY * p; i++){ (sizeX * sizeY * numFeatures));
for(i = 0; i < sizeX * sizeY * numFeatures; i++)
{
(*obj)->H[i] = 0.0f; (*obj)->H[i] = 0.0f;
} }
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
int freeFilterObject (CvLSVMFilterObject **obj){ int freeFilterObject (CvLSVMFilterObject **obj)
if(*obj == NULL) return 0; {
if(*obj == NULL) return LATENT_SVM_MEM_NULL;
free((*obj)->H); free((*obj)->H);
free(*obj); free(*obj);
(*obj) = NULL; (*obj) = NULL;
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
int allocFeatureMapObject(CvLSVMFeatureMap **obj, const int sizeX, const int sizeY, const int p, const int xp){ int allocFeatureMapObject(CvLSVMFeatureMap **obj, const int sizeX,
const int sizeY, const int numFeatures)
{
int i; int i;
(*obj) = (CvLSVMFeatureMap *)malloc(sizeof(CvLSVMFeatureMap)); (*obj) = (CvLSVMFeatureMap *)malloc(sizeof(CvLSVMFeatureMap));
(*obj)->sizeX = sizeX; (*obj)->sizeX = sizeX;
(*obj)->sizeY = sizeY; (*obj)->sizeY = sizeY;
(*obj)->p = p ; (*obj)->numFeatures = numFeatures;
(*obj)->xp = xp ; (*obj)->map = (float *) malloc(sizeof (float) *
(*obj)->Map = (float *) malloc(sizeof (float) * (sizeX * sizeY * p)); (sizeX * sizeY * numFeatures));
for(i = 0; i < sizeX * sizeY * p; i++){ for(i = 0; i < sizeX * sizeY * numFeatures; i++)
(*obj)->Map[i] = 0.0; {
(*obj)->map[i] = 0.0f;
} }
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
int freeFeatureMapObject (CvLSVMFeatureMap **obj){ int freeFeatureMapObject (CvLSVMFeatureMap **obj)
if(*obj == NULL) return 0; {
free((*obj)->Map); if(*obj == NULL) return LATENT_SVM_MEM_NULL;
free((*obj)->map);
free(*obj); free(*obj);
(*obj) = NULL; (*obj) = NULL;
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
int allocFeaturePyramidObject(CvLSVMFeaturePyramid **obj, const int lambda, const int countLevel){ int allocFeaturePyramidObject(CvLSVMFeaturePyramid **obj,
const int numLevels)
{
(*obj) = (CvLSVMFeaturePyramid *)malloc(sizeof(CvLSVMFeaturePyramid)); (*obj) = (CvLSVMFeaturePyramid *)malloc(sizeof(CvLSVMFeaturePyramid));
(*obj)->countLevel = countLevel; (*obj)->numLevels = numLevels;
(*obj)->pyramid = (CvLSVMFeatureMap **)malloc(sizeof(CvLSVMFeatureMap *) * countLevel); (*obj)->pyramid = (CvLSVMFeatureMap **)malloc(
(*obj)->lambda = lambda; sizeof(CvLSVMFeatureMap *) * numLevels);
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
int freeFeaturePyramidObject (CvLSVMFeaturePyramid **obj){ int freeFeaturePyramidObject (CvLSVMFeaturePyramid **obj)
{
int i; int i;
if(*obj == NULL) return 0; if(*obj == NULL) return LATENT_SVM_MEM_NULL;
for(i = 0; i < (*obj)->countLevel; i++) for(i = 0; i < (*obj)->numLevels; i++)
{
freeFeatureMapObject(&((*obj)->pyramid[i])); freeFeatureMapObject(&((*obj)->pyramid[i]));
}
free((*obj)->pyramid); free((*obj)->pyramid);
free(*obj); free(*obj);
(*obj) = NULL; (*obj) = NULL;
return LATENT_SVM_OK; return LATENT_SVM_OK;
} }
int allocFFTImage(CvLSVMFftImage **image, int p, int dimX, int dimY) int allocFFTImage(CvLSVMFftImage **image, int numFeatures, int dimX, int dimY)
{ {
int i, j, size; int i, j, size;
*image = (CvLSVMFftImage *)malloc(sizeof(CvLSVMFftImage)); *image = (CvLSVMFftImage *)malloc(sizeof(CvLSVMFftImage));
(*image)->p = p; (*image)->numFeatures = numFeatures;
(*image)->dimX = dimX; (*image)->dimX = dimX;
(*image)->dimY = dimY; (*image)->dimY = dimY;
(*image)->channels = (float **)malloc(sizeof(float *) * p); (*image)->channels = (float **)malloc(sizeof(float *) * numFeatures);
size = 2 * dimX * dimY; size = 2 * dimX * dimY;
for (i = 0; i < p; i++) for (i = 0; i < numFeatures; i++)
{ {
(*image)->channels[i] = (float *)malloc(sizeof(float) * size); (*image)->channels[i] = (float *)malloc(sizeof(float) * size);
for (j = 0; j < size; j++) for (j = 0; j < size; j++)
{ {
(*image)->channels[i][j] = 0.0; (*image)->channels[i][j] = 0.0f;
} }
} }
return LATENT_SVM_OK; return LATENT_SVM_OK;
@ -91,9 +104,9 @@ int allocFFTImage(CvLSVMFftImage **image, int p, int dimX, int dimY)
int freeFFTImage(CvLSVMFftImage **image) int freeFFTImage(CvLSVMFftImage **image)
{ {
unsigned i; int i;
if (*image == NULL) return LATENT_SVM_OK; if (*image == NULL) return LATENT_SVM_OK;
for (i = 0; i < (*image)->p; i++) for (i = 0; i < (*image)->numFeatures; i++)
{ {
free((*image)->channels[i]); free((*image)->channels[i]);
(*image)->channels[i] = NULL; (*image)->channels[i] = NULL;