removed contrib, legacy and softcsscade modules; removed latentsvm and datamatrix detector from objdetect. removed haartraining and sft apps.

some of the stuff will be moved to opencv_contrib module.
in order to make this PR pass buildbot, please, comment off opencv_legacy, opencv_contrib and opencv_softcascade test runs.
This commit is contained in:
Vadim Pisarevsky
2014-06-24 19:18:51 +04:00
parent 6d4c4dcd94
commit 3858f2291d
398 changed files with 12 additions and 150885 deletions

View File

@@ -1,398 +0,0 @@
/*****************************************************************************/
/* Latent SVM prediction API */
/*****************************************************************************/
#ifndef _LATENTSVM_H_
#define _LATENTSVM_H_
#include <stdio.h>
#include "_lsvm_types.h"
#include "_lsvm_error.h"
#include "_lsvm_routine.h"
//////////////////////////////////////////////////////////////
// Building feature pyramid
// (pyramid constructed both contrast and non-contrast image)
//////////////////////////////////////////////////////////////
/*
// 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
// 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
// maps - feature maps for all levels
// RESULT
// Error status
*/
int getFeaturePyramid(IplImage * image, CvLSVMFeaturePyramid **maps);
/*
// Getting feature map for the selected subimage
//
// 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
*/
int getFeatureMaps(const IplImage * image, const int k, CvLSVMFeatureMap **map);
/*
// Feature map Normalization and Truncation
//
// API
// int normalizationAndTruncationFeatureMaps(featureMap *map, const float alfa);
// INPUT
// map - feature map
// alfa - truncation threshold
// OUTPUT
// map - truncated and normalized feature map
// RESULT
// Error status
*/
int normalizeAndTruncate(CvLSVMFeatureMap *map, const float alfa);
/*
// 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
*/
int PCAFeatureMaps(CvLSVMFeatureMap *map);
//////////////////////////////////////////////////////////////
// search object
//////////////////////////////////////////////////////////////
/*
// Transformation filter displacement from the block space
// to the space of pixels at the initial image
//
// API
// int convertPoints(int countLevel, int lambda,
int initialImageLevel,
CvPoint *points, int *levels,
CvPoint **partsDisplacement, int kPoints, int n,
int maxXBorder,
int maxYBorder);
// INPUT
// countLevel - the number of levels in the feature pyramid
// lambda - method parameter
// initialImageLevel - level of feature pyramid that contains feature map
for initial image
// points - the set of root filter positions (in the block space)
// levels - the set of levels
// partsDisplacement - displacement of part filters (in the block space)
// kPoints - number of root filter positions
// n - number of part filters
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// OUTPUT
// points - the set of root filter positions (in the space of pixels)
// partsDisplacement - displacement of part filters (in the space of pixels)
// RESULT
// Error status
*/
int convertPoints(int countLevel, int lambda,
int initialImageLevel,
CvPoint *points, int *levels,
CvPoint **partsDisplacement, int kPoints, int n,
int maxXBorder,
int maxYBorder);
/*
// Elimination boxes that are outside the image boudaries
//
// API
// int clippingBoxes(int width, int height,
CvPoint *points, int kPoints);
// INPUT
// width - image wediht
// height - image heigth
// points - a set of points (coordinates of top left or
bottom right corners)
// kPoints - points number
// OUTPUT
// points - updated points (if coordinates less than zero then
set zero coordinate, if coordinates more than image
size then set coordinates equal image size)
// RESULT
// Error status
*/
#ifdef __cplusplus
extern "C"
#endif
int clippingBoxes(int width, int height,
CvPoint *points, int kPoints);
/*
// Creation feature pyramid with nullable border
//
// API
// featurePyramid* createFeaturePyramidWithBorder(const IplImage *image,
int maxXBorder, int maxYBorder);
// INPUT
// image - initial image
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// OUTPUT
// RESULT
// Feature pyramid with nullable border
*/
#ifdef __cplusplus
extern "C"
#endif
CvLSVMFeaturePyramid* createFeaturePyramidWithBorder(IplImage *image,
int maxXBorder, int maxYBorder);
/*
// Computation of the root filter displacement and values of score function
//
// API
// int searchObject(const featurePyramid *H, const filterObject **all_F, int n,
float b,
int maxXBorder,
int maxYBorder,
CvPoint **points, int **levels, int *kPoints, float *score,
CvPoint ***partsDisplacement);
// INPUT
// H - feature pyramid
// all_F - the set of filters (the first element is root filter,
other elements - part filters)
// n - the number of part filters
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// OUTPUT
// points - positions (x, y) of the upper-left corner
of root filter frame
// levels - levels that correspond to each position
// kPoints - number of positions
// score - value of the score function
// partsDisplacement - part filters displacement for each position
of the root filter
// RESULT
// Error status
*/
int searchObject(const CvLSVMFeaturePyramid *H, const CvLSVMFilterObject **all_F, int n,
float b,
int maxXBorder,
int maxYBorder,
CvPoint **points, int **levels, int *kPoints, float *score,
CvPoint ***partsDisplacement);
/*
// Computation of the root filter displacement and values of score function
//
// API
// int searchObjectThreshold(const featurePyramid *H,
const filterObject **all_F, int n,
float b,
int maxXBorder, int maxYBorder,
float scoreThreshold,
CvPoint **points, int **levels, int *kPoints,
float **score, CvPoint ***partsDisplacement);
// INPUT
// H - feature pyramid
// all_F - the set of filters (the first element is root filter,
other elements - part filters)
// n - the number of part filters
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// scoreThreshold - score threshold
// OUTPUT
// points - positions (x, y) of the upper-left corner
of root filter frame
// levels - levels that correspond to each position
// kPoints - number of positions
// score - values of the score function
// partsDisplacement - part filters displacement for each position
of the root filter
// RESULT
// Error status
*/
int searchObjectThreshold(const CvLSVMFeaturePyramid *H,
const CvLSVMFilterObject **all_F, int n,
float b,
int maxXBorder, int maxYBorder,
float scoreThreshold,
CvPoint **points, int **levels, int *kPoints,
float **score, CvPoint ***partsDisplacement,
int numThreads CV_DEFAULT(-1));
/*
// Computation root filters displacement and values of score function
//
// API
// int searchObjectThresholdSomeComponents(const featurePyramid *H,
const filterObject **filters,
int kComponents, const int *kPartFilters,
const float *b, float scoreThreshold,
CvPoint **points, CvPoint **oppPoints,
float **score, int *kPoints);
// INPUT
// H - feature pyramid
// filters - filters (root filter then it's part filters, etc.)
// kComponents - root filters number
// kPartFilters - array of part filters number for each component
// b - array of linear terms
// scoreThreshold - score threshold
// OUTPUT
// points - root filters displacement (top left corners)
// oppPoints - root filters displacement (bottom right corners)
// score - array of score values
// kPoints - number of boxes
// RESULT
// Error status
*/
#ifdef __cplusplus
extern "C"
#endif
int searchObjectThresholdSomeComponents(const CvLSVMFeaturePyramid *H,
const CvLSVMFilterObject **filters,
int kComponents, const int *kPartFilters,
const float *b, float scoreThreshold,
CvPoint **points, CvPoint **oppPoints,
float **score, int *kPoints, int numThreads);
/*
// Compute opposite point for filter box
//
// API
// int getOppositePoint(CvPoint point,
int sizeX, int sizeY,
float step, int degree,
CvPoint *oppositePoint);
// INPUT
// point - coordinates of filter top left corner
(in the space of pixels)
// (sizeX, sizeY) - filter dimension in the block space
// step - scaling factor
// degree - degree of the scaling factor
// OUTPUT
// oppositePoint - coordinates of filter bottom corner
(in the space of pixels)
// RESULT
// Error status
*/
int getOppositePoint(CvPoint point,
int sizeX, int sizeY,
float step, int degree,
CvPoint *oppositePoint);
/*
// Drawing root filter boxes
//
// API
// int showRootFilterBoxes(const IplImage *image,
const filterObject *filter,
CvPoint *points, int *levels, int kPoints,
CvScalar color, int thickness,
int line_type, int shift);
// INPUT
// image - initial image
// filter - root filter object
// points - a set of points
// levels - levels of feature pyramid
// kPoints - number of points
// color - line color for each box
// thickness - line thickness
// line_type - line type
// shift - shift
// OUTPUT
// window contained initial image and filter boxes
// RESULT
// Error status
*/
int showRootFilterBoxes(IplImage *image,
const CvLSVMFilterObject *filter,
CvPoint *points, int *levels, int kPoints,
CvScalar color, int thickness,
int line_type, int shift);
/*
// Drawing part filter boxes
//
// API
// int showPartFilterBoxes(const IplImage *image,
const filterObject *filter,
CvPoint *points, int *levels, int kPoints,
CvScalar color, int thickness,
int line_type, int shift);
// INPUT
// image - initial image
// filters - a set of part filters
// n - number of part filters
// partsDisplacement - a set of points
// levels - levels of feature pyramid
// kPoints - number of foot filter positions
// color - line color for each box
// thickness - line thickness
// line_type - line type
// shift - shift
// OUTPUT
// window contained initial image and filter boxes
// RESULT
// Error status
*/
int showPartFilterBoxes(IplImage *image,
const CvLSVMFilterObject **filters,
int n, CvPoint **partsDisplacement,
int *levels, int kPoints,
CvScalar color, int thickness,
int line_type, int shift);
/*
// Drawing boxes
//
// API
// int showBoxes(const IplImage *img,
const CvPoint *points, const CvPoint *oppositePoints, int kPoints,
CvScalar color, int thickness, int line_type, int shift);
// INPUT
// img - initial image
// points - top left corner coordinates
// oppositePoints - right bottom corner coordinates
// kPoints - points number
// color - line color for each box
// thickness - line thickness
// line_type - line type
// shift - shift
// OUTPUT
// RESULT
// Error status
*/
int showBoxes(IplImage *img,
const CvPoint *points, const CvPoint *oppositePoints, int kPoints,
CvScalar color, int thickness, int line_type, int shift);
#endif

View File

@@ -1,138 +0,0 @@
#ifndef _LSVM_DIST_TRANSFORM_H_
#define _LSVM_DIST_TRANSFORM_H_
#include "_lsvm_types.h"
#include "_lsvm_error.h"
/*
// Computation the point of intersection functions
// (parabolas on the variable y)
// a(y - q1) + b(q1 - y)(q1 - y) + f[q1]
// a(y - q2) + b(q2 - y)(q2 - y) + f[q2]
//
// API
// int GetPointOfIntersection(const F_type *f,
const F_type a, const F_type b,
int q1, int q2, F_type *point);
// INPUT
// f - function on the regular grid
// a - coefficient of the function
// b - coefficient of the function
// q1 - parameter of the function
// q2 - parameter of the function
// OUTPUT
// point - point of intersection
// RESULT
// Error status
*/
int GetPointOfIntersection(const float *f,
const float a, const float b,
int q1, int q2, float *point);
/*
// Decision of one dimensional problem generalized distance transform
// on the regular grid at all points
// min (a(y' - y) + b(y' - y)(y' - y) + f(y')) (on y')
//
// API
// int DistanceTransformOneDimensionalProblem(const F_type *f, const int n,
const F_type a, const F_type b,
F_type *distanceTransform,
int *points);
// INPUT
// f - function on the regular grid
// n - grid dimension
// a - coefficient of optimizable function
// b - coefficient of optimizable function
// OUTPUT
// distanceTransform - values of generalized distance transform
// points - arguments that corresponds to the optimal value of function
// RESULT
// Error status
*/
int DistanceTransformOneDimensionalProblem(const float *f, const int n,
const float a, const float b,
float *distanceTransform,
int *points);
/*
// Computation next cycle element
//
// API
// int GetNextCycleElement(int k, int n, int q);
// INPUT
// k - index of the previous cycle element
// n - number of matrix rows
// q - parameter that equal (number_of_rows * number_of_columns - 1)
// OUTPUT
// None
// RESULT
// Next cycle element
*/
int GetNextCycleElement(int k, int n, int q);
/*
// Transposition of cycle elements
//
// API
// void TransposeCycleElements(F_type *a, int *cycle, int cycle_len);
// INPUT
// a - initial matrix
// cycle - cycle
// cycle_len - cycle length
// OUTPUT
// a - matrix with transposed elements
// RESULT
// None
*/
void TransposeCycleElements(float *a, int *cycle, int cycle_len);
/*
// Getting transposed matrix
//
// API
// void Transpose(F_type *a, int n, int m);
// INPUT
// a - initial matrix
// n - number of rows
// m - number of columns
// OUTPUT
// a - transposed matrix
// RESULT
// Error status
*/
void Transpose(float *a, int n, int m);
/*
// Decision of two dimensional problem generalized distance transform
// on the regular grid at all points
// min{d2(y' - y) + d4(y' - y)(y' - y) +
min(d1(x' - x) + d3(x' - x)(x' - x) + f(x',y'))} (on x', y')
//
// API
// int DistanceTransformTwoDimensionalProblem(const F_type *f,
const int n, const int m,
const F_type coeff[4],
F_type *distanceTransform,
int *pointsX, int *pointsY);
// INPUT
// f - function on the regular grid
// n - number of rows
// m - number of columns
// coeff - coefficients of optimizable function
coeff[0] = d1, coeff[1] = d2,
coeff[2] = d3, coeff[3] = d4
// OUTPUT
// distanceTransform - values of generalized distance transform
// pointsX - arguments x' that correspond to the optimal value
// pointsY - arguments y' that correspond to the optimal value
// RESULT
// Error status
*/
int DistanceTransformTwoDimensionalProblem(const float *f,
const int n, const int m,
const float coeff[4],
float *distanceTransform,
int *pointsX, int *pointsY);
#endif

View File

@@ -1,20 +0,0 @@
#ifndef LSVM_ERROR
#define LSVM_ERROR
#define LATENT_SVM_OK 0
#define LATENT_SVM_MEM_NULL 2
#define DISTANCE_TRANSFORM_OK 1
#define DISTANCE_TRANSFORM_GET_INTERSECTION_ERROR -1
#define DISTANCE_TRANSFORM_ERROR -2
#define DISTANCE_TRANSFORM_EQUAL_POINTS -3
#define LATENT_SVM_GET_FEATURE_PYRAMID_FAILED -4
#define LATENT_SVM_SEARCH_OBJECT_FAILED -5
#define LATENT_SVM_FAILED_SUPERPOSITION -6
#define FILTER_OUT_OF_BOUNDARIES -7
#define LATENT_SVM_TBB_SCHEDULE_CREATION_FAILED -8
#define LATENT_SVM_TBB_NUMTHREADS_NOT_CORRECT -9
#define FFT_OK 2
#define FFT_ERROR -10
#define LSVM_PARSER_FILE_NOT_FOUND -11
#endif

View File

@@ -1,79 +0,0 @@
#ifndef _LSVM_FFT_H_
#define _LSVM_FFT_H_
#include "_lsvm_types.h"
#include "_lsvm_error.h"
#include <math.h>
/*
// 1-dimensional FFT
//
// API
// int fft(float *x_in, float *x_out, int n, int shift);
// INPUT
// x_in - input signal
// n - number of elements for searching Fourier image
// shift - shift between input elements
// OUTPUT
// x_out - output signal (contains 2n elements in order
Re(x_in[0]), Im(x_in[0]), Re(x_in[1]), Im(x_in[1]) and etc.)
// RESULT
// Error status
*/
int fft(float *x_in, float *x_out, int n, int shift);
/*
// Inverse 1-dimensional FFT
//
// API
// int fftInverse(float *x_in, float *x_out, int n, int shift);
// INPUT
// x_in - Fourier image of 1d input signal(contains 2n elements
in order Re(x_in[0]), Im(x_in[0]),
Re(x_in[1]), Im(x_in[1]) and etc.)
// n - number of elements for searching counter FFT image
// shift - shift between input elements
// OUTPUT
// x_in - input signal (contains n elements)
// RESULT
// Error status
*/
int fftInverse(float *x_in, float *x_out, int n, int shift);
/*
// 2-dimensional FFT
//
// API
// int fft2d(float *x_in, float *x_out, int numRows, int numColls);
// INPUT
// x_in - input signal (matrix, launched by rows)
// numRows - number of rows
// numColls - number of collumns
// OUTPUT
// x_out - output signal (contains (2 * numRows * numColls) elements
in order Re(x_in[0][0]), Im(x_in[0][0]),
Re(x_in[0][1]), Im(x_in[0][1]) and etc.)
// RESULT
// Error status
*/
int fft2d(float *x_in, float *x_out, int numRows, int numColls);
/*
// Inverse 2-dimensional FFT
//
// API
// int fftInverse2d(float *x_in, float *x_out, int numRows, int numColls);
// INPUT
// x_in - Fourier image of matrix (contains (2 * numRows * numColls)
elements in order Re(x_in[0][0]), Im(x_in[0][0]),
Re(x_in[0][1]), Im(x_in[0][1]) and etc.)
// numRows - number of rows
// numColls - number of collumns
// OUTPUT
// x_out - initial signal (matrix, launched by rows)
// RESULT
// Error status
*/
int fftInverse2d(float *x_in, float *x_out, int numRows, int numColls);
#endif

View File

@@ -1,440 +0,0 @@
/*****************************************************************************/
/* Matching procedure API */
/*****************************************************************************/
//
#ifndef _LSVM_MATCHING_H_
#define _LSVM_MATCHING_H_
#include "_latentsvm.h"
#include "_lsvm_error.h"
#include "_lsvm_distancetransform.h"
#include "_lsvm_fft.h"
#include "_lsvm_routine.h"
#ifdef HAVE_TBB
#include "_lsvm_tbbversion.h"
#endif
//extern "C" {
/*
// Function for convolution computation
//
// API
// int convolution(const filterObject *Fi, const featureMap *map, float *f);
// INPUT
// Fi - filter object
// map - feature map
// OUTPUT
// f - the convolution
// RESULT
// Error status
*/
int convolution(const CvLSVMFilterObject *Fi, const CvLSVMFeatureMap *map, float *f);
/*
// Computation multiplication of FFT images
//
// API
// int fftImagesMulti(float *fftImage1, float *fftImage2, int numRows, int numColls,
float *multi);
// INPUT
// fftImage1 - first fft image
// fftImage2 - second fft image
// (numRows, numColls) - image dimesions
// OUTPUT
// multi - multiplication
// RESULT
// Error status
*/
int fftImagesMulti(float *fftImage1, float *fftImage2, int numRows, int numColls,
float *multi);
/*
// Turnover filter matrix for the single feature
//
// API
// int rot2PI(float *filter, int dimX, int dimY, float *rot2PIFilter,
int p, int shift);
// INPUT
// filter - filter weight matrix
// (dimX, dimY) - dimension of filter matrix
// p - number of features
// shift - number of feature (or channel)
// OUTPUT
// rot2PIFilter - rotated matrix
// RESULT
// Error status
*/
int rot2PI(float *filter, int dimX, int dimY, float *rot2PIFilter,
int p, int shift);
/*
// Addition nullable bars to the dimension of feature map (single feature)
//
// API
// int addNullableBars(float *rot2PIFilter, int dimX, int dimY,
float *newFilter, int newDimX, int newDimY);
// INPUT
// rot2PIFilter - filter matrix for the single feature that was rotated
// (dimX, dimY) - dimension rot2PIFilter
// (newDimX, newDimY)- dimension of feature map for the single feature
// OUTPUT
// newFilter - filter matrix with nullable bars
// RESULT
// Error status
*/
int addNullableBars(float *rot2PIFilter, int dimX, int dimY,
float *newFilter, int newDimX, int newDimY);
/*
// Computation FFT image for filter object
//
// API
// int getFFTImageFilterObject(const filterObject *filter,
int mapDimX, int mapDimY,
fftImage **image);
// INPUT
// filter - filter object
// (mapDimX, mapDimY)- dimension of feature map
// OUTPUT
// image - fft image
// RESULT
// Error status
*/
int getFFTImageFilterObject(const CvLSVMFilterObject *filter,
int mapDimX, int mapDimY,
CvLSVMFftImage **image);
/*
// Computation FFT image for feature map
//
// API
// int getFFTImageFeatureMap(const featureMap *map, fftImage **image);
// INPUT
// OUTPUT
// RESULT
// Error status
*/
int getFFTImageFeatureMap(const CvLSVMFeatureMap *map, CvLSVMFftImage **image);
/*
// Function for convolution computation using FFT
//
// API
// int convFFTConv2d(const fftImage *featMapImage, const fftImage *filterImage,
int filterDimX, int filterDimY, float **conv);
// INPUT
// featMapImage - feature map image
// filterImage - filter image
// (filterDimX,filterDimY) - filter dimension
// OUTPUT
// conv - the convolution
// RESULT
// Error status
*/
int convFFTConv2d(const CvLSVMFftImage *featMapImage, const CvLSVMFftImage *filterImage,
int filterDimX, int filterDimY, float **conv);
/*
// Computation objective function D according the original paper
//
// API
// int filterDispositionLevel(const filterObject *Fi, const featureMap *pyramid,
float **scoreFi,
int **pointsX, int **pointsY);
// INPUT
// Fi - filter object (weights and coefficients of penalty
function that are used in this routine)
// pyramid - feature map
// OUTPUT
// scoreFi - values of distance transform on the level at all positions
// (pointsX, pointsY)- positions that correspond to the maximum value
of distance transform at all grid nodes
// RESULT
// Error status
*/
int filterDispositionLevel(const CvLSVMFilterObject *Fi, const CvLSVMFeatureMap *pyramid,
float **scoreFi,
int **pointsX, int **pointsY);
/*
// Computation objective function D according the original paper using FFT
//
// API
// int filterDispositionLevelFFT(const filterObject *Fi, const fftImage *featMapImage,
float **scoreFi,
int **pointsX, int **pointsY);
// INPUT
// Fi - filter object (weights and coefficients of penalty
function that are used in this routine)
// featMapImage - FFT image of feature map
// OUTPUT
// scoreFi - values of distance transform on the level at all positions
// (pointsX, pointsY)- positions that correspond to the maximum value
of distance transform at all grid nodes
// RESULT
// Error status
*/
int filterDispositionLevelFFT(const CvLSVMFilterObject *Fi, const CvLSVMFftImage *featMapImage,
float **scoreFi,
int **pointsX, int **pointsY);
/*
// Computation border size for feature map
//
// API
// int computeBorderSize(int maxXBorder, int maxYBorder, int *bx, int *by);
// INPUT
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// OUTPUT
// bx - border size (X-direction)
// by - border size (Y-direction)
// RESULT
// Error status
*/
int computeBorderSize(int maxXBorder, int maxYBorder, int *bx, int *by);
/*
// Addition nullable border to the feature map
//
// API
// int addNullableBorder(featureMap *map, int bx, int by);
// INPUT
// map - feature map
// bx - border size (X-direction)
// by - border size (Y-direction)
// OUTPUT
// RESULT
// Error status
*/
int addNullableBorder(CvLSVMFeatureMap *map, int bx, int by);
/*
// Computation the maximum of the score function at the level
//
// API
// int maxFunctionalScoreFixedLevel(const filterObject **all_F, int n,
const featurePyramid *H,
int level, float b,
int maxXBorder, int maxYBorder,
float *score, CvPoint **points, int *kPoints,
CvPoint ***partsDisplacement);
// INPUT
// all_F - the set of filters (the first element is root filter,
the other - part filters)
// n - the number of part filters
// H - feature pyramid
// level - feature pyramid level for computation maximum score
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// OUTPUT
// score - the maximum of the score function at the level
// points - the set of root filter positions (in the block space)
// levels - the set of levels
// kPoints - number of root filter positions
// partsDisplacement - displacement of part filters (in the block space)
// RESULT
// Error status
*/
int maxFunctionalScoreFixedLevel(const CvLSVMFilterObject **all_F, int n,
const CvLSVMFeaturePyramid *H,
int level, float b,
int maxXBorder, int maxYBorder,
float *score, CvPoint **points, int *kPoints,
CvPoint ***partsDisplacement);
/*
// Computation score function at the level that exceed threshold
//
// API
// int thresholdFunctionalScoreFixedLevel(const filterObject **all_F, int n,
const featurePyramid *H,
int level, float b,
int maxXBorder, int maxYBorder,
float scoreThreshold,
float **score, CvPoint **points, int *kPoints,
CvPoint ***partsDisplacement);
// INPUT
// all_F - the set of filters (the first element is root filter,
the other - part filters)
// n - the number of part filters
// H - feature pyramid
// level - feature pyramid level for computation maximum score
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// scoreThreshold - score threshold
// OUTPUT
// score - score function at the level that exceed threshold
// points - the set of root filter positions (in the block space)
// levels - the set of levels
// kPoints - number of root filter positions
// partsDisplacement - displacement of part filters (in the block space)
// RESULT
// Error status
*/
int thresholdFunctionalScoreFixedLevel(const CvLSVMFilterObject **all_F, int n,
const CvLSVMFeaturePyramid *H,
int level, float b,
int maxXBorder, int maxYBorder,
float scoreThreshold,
float **score, CvPoint **points, int *kPoints,
CvPoint ***partsDisplacement);
/*
// Computation the maximum of the score function
//
// API
// int maxFunctionalScore(const filterObject **all_F, int n,
const featurePyramid *H, float b,
int maxXBorder, int maxYBorder,
float *score,
CvPoint **points, int **levels, int *kPoints,
CvPoint ***partsDisplacement);
// INPUT
// all_F - the set of filters (the first element is root filter,
the other - part filters)
// n - the number of part filters
// H - feature pyramid
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// OUTPUT
// score - the maximum of the score function
// points - the set of root filter positions (in the block space)
// levels - the set of levels
// kPoints - number of root filter positions
// partsDisplacement - displacement of part filters (in the block space)
// RESULT
// Error status
*/
int maxFunctionalScore(const CvLSVMFilterObject **all_F, int n,
const CvLSVMFeaturePyramid *H, float b,
int maxXBorder, int maxYBorder,
float *score,
CvPoint **points, int **levels, int *kPoints,
CvPoint ***partsDisplacement);
/*
// Computation score function that exceed threshold
//
// API
// int thresholdFunctionalScore(const filterObject **all_F, int n,
const featurePyramid *H,
float b,
int maxXBorder, int maxYBorder,
float scoreThreshold,
float **score,
CvPoint **points, int **levels, int *kPoints,
CvPoint ***partsDisplacement);
// INPUT
// all_F - the set of filters (the first element is root filter,
the other - part filters)
// n - the number of part filters
// H - feature pyramid
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// scoreThreshold - score threshold
// OUTPUT
// score - score function values that exceed threshold
// points - the set of root filter positions (in the block space)
// levels - the set of levels
// kPoints - number of root filter positions
// partsDisplacement - displacement of part filters (in the block space)
// RESULT
// Error status
*/
int thresholdFunctionalScore(const CvLSVMFilterObject **all_F, int n,
const CvLSVMFeaturePyramid *H,
float b,
int maxXBorder, int maxYBorder,
float scoreThreshold,
float **score,
CvPoint **points, int **levels, int *kPoints,
CvPoint ***partsDisplacement);
#ifdef HAVE_TBB
/*
// int tbbThresholdFunctionalScore(const CvLSVMFilterObject **all_F, int n,
const CvLSVMFeaturePyramid *H,
const float b,
const int maxXBorder, const int maxYBorder,
const float scoreThreshold,
const int threadsNum,
float **score,
CvPoint **points, int **levels, int *kPoints,
CvPoint ***partsDisplacement);
// INPUT
// all_F - the set of filters (the first element is root filter,
the other - part filters)
// n - the number of part filters
// H - feature pyramid
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// scoreThreshold - score threshold
// threadsNum - number of threads that will be created using TBB version
// OUTPUT
// score - score function values that exceed threshold
// points - the set of root filter positions (in the block space)
// levels - the set of levels
// kPoints - number of root filter positions
// partsDisplacement - displacement of part filters (in the block space)
// RESULT
// Error status
*/
int tbbThresholdFunctionalScore(const CvLSVMFilterObject **all_F, int n,
const CvLSVMFeaturePyramid *H,
const float b,
const int maxXBorder, const int maxYBorder,
const float scoreThreshold,
const int threadsNum,
float **score,
CvPoint **points, int **levels, int *kPoints,
CvPoint ***partsDisplacement);
#endif
/*
// Perform non-maximum suppression algorithm (described in original paper)
// to remove "similar" bounding boxes
//
// API
// int nonMaximumSuppression(int numBoxes, const CvPoint *points,
const CvPoint *oppositePoints, const float *score,
float overlapThreshold,
int *numBoxesout, CvPoint **pointsOut,
CvPoint **oppositePointsOut, float **scoreOut);
// INPUT
// numBoxes - number of bounding boxes
// points - array of left top corner coordinates
// oppositePoints - array of right bottom corner coordinates
// score - array of detection scores
// overlapThreshold - threshold: bounding box is removed if overlap part
is greater than passed value
// OUTPUT
// numBoxesOut - the number of bounding boxes algorithm returns
// pointsOut - array of left top corner coordinates
// oppositePointsOut - array of right bottom corner coordinates
// scoreOut - array of detection scores
// RESULT
// Error status
*/
#ifdef __cplusplus
extern "C"
#endif
int nonMaximumSuppression(int numBoxes, const CvPoint *points,
const CvPoint *oppositePoints, const float *score,
float overlapThreshold,
int *numBoxesOut, CvPoint **pointsOut,
CvPoint **oppositePointsOut, float **scoreOut);
#ifdef __cplusplus
extern "C"
#endif
int getMaxFilterDims(const CvLSVMFilterObject **filters, int kComponents,
const int *kPartFilters,
unsigned int *maxXBorder, unsigned int *maxYBorder);
//}
#endif

View File

@@ -1,10 +0,0 @@
#ifndef _LSVM_RESIZEIMG_H_
#define _LSVM_RESIZEIMG_H_
#include "_lsvm_types.h"
IplImage * resize_opencv (IplImage * img, float scale);
IplImage * resize_article_dp1(IplImage * img, float scale, const int k);
IplImage * resize_article_dp(IplImage * img, float scale, const int k);
#endif

View File

@@ -1,37 +0,0 @@
#ifndef _LSVM_ROUTINE_H_
#define _LSVM_ROUTINE_H_
#include "opencv2/objdetect/objdetect_c.h"
#include "_lsvm_types.h"
#include "_lsvm_error.h"
//////////////////////////////////////////////////////////////
// Memory management routines
// All paramaters names correspond to previous data structures description
// All "alloc" functions return allocated memory for 1 object
// with all fields including arrays
// Error status is return value
//////////////////////////////////////////////////////////////
int allocFilterObject(CvLSVMFilterObject **obj, const int sizeX, const int sizeY,
const int p);
int freeFilterObject (CvLSVMFilterObject **obj);
int allocFeatureMapObject(CvLSVMFeatureMap **obj, const int sizeX, const int sizeY,
const int p);
int freeFeatureMapObject (CvLSVMFeatureMap **obj);
#ifdef __cplusplus
extern "C"
#endif
int allocFeaturePyramidObject(CvLSVMFeaturePyramid **obj,
const int countLevel);
#ifdef __cplusplus
extern "C"
#endif
int freeFeaturePyramidObject (CvLSVMFeaturePyramid **obj);
int allocFFTImage(CvLSVMFftImage **image, int p, int dimX, int dimY);
int freeFFTImage(CvLSVMFftImage **image);
#endif

View File

@@ -1,51 +0,0 @@
#ifndef _LSVM_TBBVERSION_H
#define _LSVM_TBBVERSION_H
#include "_lsvm_matching.h"
/*
// Computation score function using TBB tasks
//
// API
// int tbbTasksThresholdFunctionalScore(const CvLSVMFilterObject **filters, const int n,
const CvLSVMFeaturePyramid *H, const float b,
const int maxXBorder, const int maxYBorder,
const float scoreThreshold,
int *kLevels, int **procLevels,
const int threadsNum,
float **score, CvPoint ***points,
int *kPoints,
CvPoint ****partsDisplacement);
// INPUT
// filters - the set of filters (the first element is root filter,
the other - part filters)
// n - the number of part filters
// H - feature pyramid
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// scoreThreshold - score threshold
// kLevels - array that contains number of levels processed
by each thread
// procLevels - array that contains lists of levels processed
by each thread
// threadsNum - the number of created threads
// OUTPUT
// score - score function values that exceed threshold
// points - the set of root filter positions (in the block space)
// kPoints - number of root filter positions
// partsDisplacement - displacement of part filters (in the block space)
// RESULT
//
*/
int tbbTasksThresholdFunctionalScore(const CvLSVMFilterObject **filters, const int n,
const CvLSVMFeaturePyramid *H, const float b,
const int maxXBorder, const int maxYBorder,
const float scoreThreshold,
int *kLevels, int **procLevels,
const int threadsNum,
float **score, CvPoint ***points,
int *kPoints,
CvPoint ****partsDisplacement);
#endif

View File

@@ -1,83 +0,0 @@
#ifndef SVM_TYPE
#define SVM_TYPE
#include "float.h"
//#define FFT_CONV
#define PI CV_PI
#define EPS 0.000001
#define F_MAX FLT_MAX
#define F_MIN -FLT_MAX
// The number of elements in bin
// The number of sectors in gradient histogram building
#define NUM_SECTOR 9
// The number of levels in image resize procedure
// We need Lambda levels to resize image twice
#define LAMBDA 10
// Block size. Used in feature pyramid building procedure
#define SIDE_LENGTH 8
#define VAL_OF_TRUNCATE 0.2f
//////////////////////////////////////////////////////////////
// main data structures //
//////////////////////////////////////////////////////////////
// DataType: STRUCT featureMap
// FEATURE MAP DESCRIPTION
// Rectangular map (sizeX x sizeY),
// every cell stores feature vector (dimension = numFeatures)
// map - matrix of feature vectors
// to set and get feature vectors (i,j)
// used formula map[(j * sizeX + i) * p + k], where
// k - component of feature vector in cell (i, j)
typedef struct{
int sizeX;
int sizeY;
int numFeatures;
float *map;
} CvLSVMFeatureMap;
// DataType: STRUCT featurePyramid
//
// numLevels - number of levels in the feature pyramid
// pyramid - array of pointers to feature map at different levels
typedef struct{
int numLevels;
CvLSVMFeatureMap **pyramid;
} CvLSVMFeaturePyramid;
// DataType: STRUCT filterDisposition
// The structure stores preliminary results in optimization process
// with objective function D
//
// x - array with X coordinates of optimization problems solutions
// y - array with Y coordinates of optimization problems solutions
// score - array with optimal objective values
typedef struct{
float *score;
int *x;
int *y;
} CvLSVMFilterDisposition;
// DataType: STRUCT fftImage
// The structure stores FFT image
//
// numFeatures - number of channels
// x - array of FFT images for 2d signals
// n - number of rows
// m - number of collums
typedef struct{
int numFeatures;
int dimX;
int dimY;
float **channels;
} CvLSVMFftImage;
#endif

View File

@@ -1,66 +0,0 @@
#ifndef LSVM_PARSER
#define LSVM_PARSER
#include "opencv2/objdetect/objdetect_c.h"
#include "_lsvm_types.h"
#define MODEL 1
#define P 2
#define COMP 3
#define SCORE 4
#define RFILTER 100
#define PFILTERs 101
#define PFILTER 200
#define SIZEX 150
#define SIZEY 151
#define WEIGHTS 152
#define TAGV 300
#define Vx 350
#define Vy 351
#define TAGD 400
#define Dx 451
#define Dy 452
#define Dxx 453
#define Dyy 454
#define BTAG 500
#define STEP_END 1000
#define EMODEL (STEP_END + MODEL)
#define EP (STEP_END + P)
#define ECOMP (STEP_END + COMP)
#define ESCORE (STEP_END + SCORE)
#define ERFILTER (STEP_END + RFILTER)
#define EPFILTERs (STEP_END + PFILTERs)
#define EPFILTER (STEP_END + PFILTER)
#define ESIZEX (STEP_END + SIZEX)
#define ESIZEY (STEP_END + SIZEY)
#define EWEIGHTS (STEP_END + WEIGHTS)
#define ETAGV (STEP_END + TAGV)
#define EVx (STEP_END + Vx)
#define EVy (STEP_END + Vy)
#define ETAGD (STEP_END + TAGD)
#define EDx (STEP_END + Dx)
#define EDy (STEP_END + Dy)
#define EDxx (STEP_END + Dxx)
#define EDyy (STEP_END + Dyy)
#define EBTAG (STEP_END + BTAG)
//extern "C" {
int LSVMparser(const char * filename, CvLSVMFilterObject *** model, int *last, int *max,
int **comp, float **b, int *count, float * score);
#ifdef __cplusplus
extern "C"
#endif
int loadModel(
const char *modelPath,
CvLSVMFilterObject ***filters,
int *kFilters,
int *kComponents,
int **kPartFilters,
float **b,
float *scoreThreshold);
//};
#endif

View File

@@ -1,567 +0,0 @@
#include "precomp.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/objdetect/objdetect_c.h"
#include <algorithm>
class Sampler {
public:
CvMat *im;
CvPoint o;
CvPoint c, cc;
CvMat *perim;
CvPoint fcoord(float fx, float fy);
CvPoint coord(int ix, int iy);
Sampler(CvMat *_im, CvPoint _o, CvPoint _c, CvPoint _cc);
uchar getpixel(int ix, int iy);
int isinside(int x, int y);
int overlap(Sampler &other);
int hasbars();
void timing();
CvMat *extract();
Sampler():im(0),perim(0){}
~Sampler(){}
};
class code { // used in this file only
public:
char msg[4];
CvMat *original;
Sampler sa;
};
unsigned char cblk[256] = { 34,19,36,36,51,19,51,51,66,19,36,36,66,19,66,66,49,19,36,36,51,19,51,51,49,19,36,36,
49,19,49,49,32,19,36,36,51,19,51,51,66,19,36,36,66,19,66,66,32,19,36,36,51,19,51,51,32,19,36,36,32,19,32,32,
17,19,36,36,51,19,51,51,66,19,36,36,66,19,66,66,49,19,36,36,51,19,51,51,49,19,36,36,49,19,49,49,17,19,36,36,
51,19,51,51,66,19,36,36,66,19,66,66,17,19,36,36,51,19,51,51,17,19,36,36,17,19,17,17,2,19,2,36,2,19,2,51,2,19,
2,36,2,19,2,66,2,19,2,36,2,19,2,51,2,19,2,36,2,19,2,49,2,19,2,36,2,19,2,51,2,19,2,36,2,19,2,66,2,19,2,36,2,19,
2,51,2,19,2,36,2,19,2,32,2,19,2,36,2,19,2,51,2,19,2,36,2,19,2,66,2,19,2,36,2,19,2,51,2,19,2,36,2,19,2,49,2,19,
2,36,2,19,2,51,2,19,2,36,2,19,2,66,2,19,2,36,2,19,2,51,2,19,2,36,2,19,2,34 };
unsigned char ccblk[256] = { 34,17,2,17,19,19,2,17,36,36,2,36,19,19,2,17,51,51,2,51,19,19,2,51,36,36,2,36,19,19,2,17,
66,66,2,66,19,19,2,66,36,36,2,36,19,19,2,66,51,51,2,51,19,19,2,51,36,36,2,36,19,19,2,17,49,49,2,49,19,19,2,49,36,
36,2,36,19,19,2,49,51,51,2,51,19,19,2,51,36,36,2,36,19,19,2,49,66,66,2,66,19,19,2,66,36,36,2,36,19,19,2,66,51,51,
2,51,19,19,2,51,36,36,2,36,19,19,2,17,32,32,2,32,19,19,2,32,36,36,2,36,19,19,2,32,51,51,2,51,19,19,2,51,36,36,2,
36,19,19,2,32,66,66,2,66,19,19,2,66,36,36,2,36,19,19,2,66,51,51,2,51,19,19,2,51,36,36,2,36,19,19,2,32,49,49,2,49,
19,19,2,49,36,36,2,36,19,19,2,49,51,51,2,51,19,19,2,51,36,36,2,36,19,19,2,49,66,66,2,66,19,19,2,66,36,36,2,36,19,
19,2,66,51,51,2,51,19,19,2,51,36,36,2,36,19,19,2,34 };
static const int pickup[64][2] = { {7,6},{8,6},{7,5},{8,5},{1,5},{7,4},{8,4},{1,4},{1,8},{2,8},{1,7},{2,7},{3,7},
{1,6},{2,6},{3,6},{3,2},{4,2},{3,1},{4,1},{5,1},{3,8},{4,8},{5,8},{6,1},{7,1},{6,8},{7,8},{8,8},{6,7},{7,7},{8,7},
{4,7},{5,7},{4,6},{5,6},{6,6},{4,5},{5,5},{6,5},{2,5},{3,5},{2,4},{3,4},{4,4},{2,3},{3,3},{4,3},{8,3},{1,3},{8,2},
{1,2},{2,2},{8,1},{1,1},{2,1},{5,4},{6,4},{5,3},{6,3},{7,3},{5,2},{6,2},{7,2} };
static const uchar Alog[256] = { 1,2,4,8,16,32,64,128,45,90,180,69,138,57,114,228,229,231,227,235,251,219,155,27,
54,108,216,157,23,46,92,184,93,186,89,178,73,146,9,18,36,72,144,13,26,52,104,208,141,55,110,220,149,7,14,28,
56,112,224,237,247,195,171,123,246,193,175,115,230,225,239,243,203,187,91,182,65,130,41,82,164,101,202,185,95,
190,81,162,105,210,137,63,126,252,213,135,35,70,140,53,106,212,133,39,78,156,21,42,84,168,125,250,217,159,19,
38,76,152,29,58,116,232,253,215,131,43,86,172,117,234,249,223,147,11,22,44,88,176,77,154,25,50,100,200,189,87,
174,113,226,233,255,211,139,59,118,236,245,199,163,107,214,129,47,94,188,85,170,121,242,201,191,83,166,97,194,
169,127,254,209,143,51,102,204,181,71,142,49,98,196,165,103,206,177,79,158,17,34,68,136,61,122,244,197,167,99,
198,161,111,222,145,15,30,60,120,240,205,183,67,134,33,66,132,37,74,148,5,10,20,40,80,160,109,218,153,31,62,
124,248,221,151,3,6,12,24,48,96,192,173,119,238,241,207,179,75,150,1 };
static const uchar Log[256] = { (uchar)-255,255,1,240,2,225,241,53,3,38,226,133,242,43,54,210,4,195,39,
114,227,106,134,28,243,140,44,23,55,118,211,234,5,219,196,96,40,222,115,103,228,78,107,125,
135,8,29,162,244,186,141,180,45,99,24,49,56,13,119,153,212,199,235,91,6,76,220,217,197,11,97,
184,41,36,223,253,116,138,104,193,229,86,79,171,108,165,126,145,136,34,9,74,30,32,163,84,245,
173,187,204,142,81,181,190,46,88,100,159,25,231,50,207,57,147,14,67,120,128,154,248,213,167,
200,63,236,110,92,176,7,161,77,124,221,102,218,95,198,90,12,152,98,48,185,179,42,209,37,132,
224,52,254,239,117,233,139,22,105,27,194,113,230,206,87,158,80,189,172,203,109,175,166,62,127,
247,146,66,137,192,35,252,10,183,75,216,31,83,33,73,164,144,85,170,246,65,174,61,188,202,205,
157,143,169,82,72,182,215,191,251,47,178,89,151,101,94,160,123,26,112,232,21,51,238,208,131,
58,69,148,18,15,16,68,17,121,149,129,19,155,59,249,70,214,250,168,71,201,156,64,60,237,130,
111,20,93,122,177,150 };
#define dethresh 0.92f
#define eincO (2 * dethresh) // e increment orthogonal
#define eincD (1.414f * dethresh) // e increment diagonal
static const float eincs[] = {
eincO, eincD,
eincO, eincD,
eincO, eincD,
eincO, eincD,
999 };
#define Ki(x) _mm_set_epi32((x),(x),(x),(x))
#define Kf(x) _mm_set_ps((x),(x),(x),(x))
static const int CV_DECL_ALIGNED(16) absmask[] = {0x7fffffff, 0x7fffffff, 0x7fffffff, 0x7fffffff};
#define _mm_abs_ps(x) _mm_and_ps((x), *(const __m128*)absmask)
static void writexy(CvMat *m, int r, CvPoint p)
{
int *pdst = (int*)cvPtr2D(m, r, 0);
pdst[0] = p.x;
pdst[1] = p.y;
}
Sampler::Sampler(CvMat *_im, CvPoint _o, CvPoint _c, CvPoint _cc)
{
im = _im;
o = _o;
c = _c;
cc = _cc;
perim = cvCreateMat(4, 1, CV_32SC2);
writexy(perim, 0, fcoord(-.2f,-.2f));
writexy(perim, 1, fcoord(-.2f,1.2f));
writexy(perim, 2, fcoord(1.2f,1.2f));
writexy(perim, 3, fcoord(1.2f,-.2f));
// printf("Sampler %d,%d %d,%d %d,%d\n", o.x, o.y, c.x, c.y, cc.x, cc.y);
}
CvPoint Sampler::fcoord(float fx, float fy)
{
CvPoint r;
r.x = (int)(o.x + fx * (cc.x - o.x) + fy * (c.x - o.x));
r.y = (int)(o.y + fx * (cc.y - o.y) + fy * (c.y - o.y));
return r;
}
CvPoint Sampler::coord(int ix, int iy)
{
return fcoord(0.05f + 0.1f * ix, 0.05f + 0.1f * iy);
}
uchar Sampler::getpixel(int ix, int iy)
{
CvPoint pt = coord(ix, iy);
if ((0 <= pt.x) && (pt.x < im->cols) && (0 <= pt.y) && (pt.y < im->rows))
return *cvPtr2D(im, pt.y, pt.x);
else
return 0;
}
int Sampler::isinside(int x, int y)
{
CvPoint2D32f pt;
pt.x = (float)x;
pt.y = (float)y;
if ((0 <= pt.x) && (pt.x < im->cols) && (0 <= pt.y) && (pt.y < im->rows))
return cvPointPolygonTest(perim, pt, 0) < 0;
else
return 0;
}
int Sampler::overlap(Sampler &other)
{
for (int i = 0; i < 4; i++) {
CvScalar p;
p = cvGet2D(other.perim, i, 0);
if (isinside((int)p.val[0], (int)p.val[1]))
return 1;
p = cvGet2D(perim, i, 0);
if (other.isinside((int)p.val[0], (int)p.val[1]))
return 1;
}
return 0;
}
int Sampler::hasbars()
{
return getpixel(9, 1) > getpixel(9, 0);
}
void Sampler::timing()
{
/*uchar light, dark = getpixel(9, 0);
for (int i = 1; i < 3; i += 2) {
light = getpixel(9, i);
// if (light <= dark)
// goto endo;
dark = getpixel(9, i + 1);
// if (up <= down)
// goto endo;
}*/
}
CvMat *Sampler::extract()
{
// return a 10x10 CvMat for the current contents, 0 is black, 255 is white
// Sampler has (0,0) at bottom left, so invert Y
CvMat *r = cvCreateMat(10, 10, CV_8UC1);
for (int x = 0; x < 10; x++)
for (int y = 0; y < 10; y++)
*cvPtr2D(r, 9 - y, x) = (getpixel(x, y) < 128) ? 0 : 255;
return r;
}
#if CV_SSE2
static void apron(CvMat *v)
{
int r = v->rows;
int c = v->cols;
memset(cvPtr2D(v, 0, 0), 0x22, c);
memset(cvPtr2D(v, 1, 0), 0x22, c);
memset(cvPtr2D(v, r - 2, 0), 0x22, c);
memset(cvPtr2D(v, r - 1, 0), 0x22, c);
int y;
for (y = 2; y < r - 2; y++) {
uchar *lp = cvPtr2D(v, y, 0);
lp[0] = 0x22;
lp[1] = 0x22;
lp[c-2] = 0x22;
lp[c-1] = 0x22;
}
}
static void cfollow(CvMat *src, CvMat *dst)
{
int sx, sy;
uchar *vpd = cvPtr2D(src, 0, 0);
for (sy = 0; sy < src->rows; sy++) {
short *wr = (short*)cvPtr2D(dst, sy, 0);
for (sx = 0; sx < src->cols; sx++) {
int x = sx;
int y = sy;
float e = 0;
int ontrack = true;
int dir;
while (ontrack) {
dir = vpd[y * src->step + x];
int xd = ((dir & 0xf) - 2);
int yd = ((dir >> 4) - 2);
e += (dir == 0x22) ? 999 : ((dir & 1) ? eincD : eincO);
x += xd;
y += yd;
if (e > 10.) {
float d = (float)(((x - sx) * (x - sx)) + ((y - sy) * (y - sy)));
ontrack = d > (e * e);
}
}
if ((24 <= e) && (e < 999)) {
// printf("sx=%d, sy=%d, x=%d, y=%d\n", sx, sy, x, y);
*wr++ = (short)(x - sx);
*wr++ = (short)(y - sy);
} else {
*wr++ = 0;
*wr++ = 0;
}
}
}
}
static uchar gf256mul(uchar a, uchar b)
{
return Alog[(Log[a] + Log[b]) % 255];
}
static int decode(Sampler &sa, code &cc)
{
uchar binary[8] = {0,0,0,0,0,0,0,0};
uchar b = 0;
int sum;
sum = 0;
for (int i = 0; i < 64; i++)
sum += sa.getpixel(1 + (i & 7), 1 + (i >> 3));
uchar mean = (uchar)(sum / 64);
for (int i = 0; i < 64; i++) {
b = (b << 1) + (sa.getpixel(pickup[i][0], pickup[i][1]) <= mean);
if ((i & 7) == 7) {
binary[i >> 3] = b;
b = 0;
}
}
// Compute the 5 RS codewords for the 3 datawords
uchar c[5] = {0,0,0,0,0};
{
uchar a[5] = {228, 48, 15, 111, 62};
int k = 5;
for (int i = 0; i < 3; i++) {
uchar t = binary[i] ^ c[4];
for (int j = k - 1; j != -1; j--) {
if (t == 0)
c[j] = 0;
else
c[j] = gf256mul(t, a[j]);
if (j > 0)
c[j] = c[j - 1] ^ c[j];
}
}
}
if ((c[4] == binary[3]) &&
(c[3] == binary[4]) &&
(c[2] == binary[5]) &&
(c[1] == binary[6]) &&
(c[0] == binary[7])) {
uchar x = 0xff & (binary[0] - 1);
uchar y = 0xff & (binary[1] - 1);
uchar z = 0xff & (binary[2] - 1);
cc.msg[0] = x;
cc.msg[1] = y;
cc.msg[2] = z;
cc.msg[3] = 0;
cc.sa = sa;
cc.original = sa.extract();
return 1;
} else {
return 0;
}
}
static std::deque<CvPoint> trailto(CvMat *v, int x, int y, CvMat *terminal)
{
CvPoint np;
/* Return the last 10th of the trail of points following v from (x,y)
* to terminal
*/
int ex = x + ((short*)cvPtr2D(terminal, y, x))[0];
int ey = y + ((short*)cvPtr2D(terminal, y, x))[1];
std::deque<CvPoint> r;
while ((x != ex) || (y != ey)) {
np.x = x;
np.y = y;
r.push_back(np);
int dir = *cvPtr2D(v, y, x);
int xd = ((dir & 0xf) - 2);
int yd = ((dir >> 4) - 2);
x += xd;
y += yd;
}
int l = (int)(r.size() * 9 / 10);
while (l--)
r.pop_front();
return r;
}
#endif
std::deque <CvDataMatrixCode> cvFindDataMatrix(CvMat *im)
{
#if CV_SSE2
int r = im->rows;
int c = im->cols;
#define SAMESIZE(nm, ty) CvMat *nm = cvCreateMat(r, c, ty);
SAMESIZE(thresh, CV_8UC1)
SAMESIZE(vecpic, CV_8UC1)
SAMESIZE(vc, CV_8UC1)
SAMESIZE(vcc, CV_8UC1)
SAMESIZE(cxy, CV_16SC2)
SAMESIZE(ccxy, CV_16SC2)
cvAdaptiveThreshold(im, thresh, 255.0, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 13);
{
int x, y;
int sstride = thresh->step;
int sw = thresh->cols; // source width
for (y = 2; y < thresh->rows - 2; y++) {
uchar *ps = cvPtr2D(thresh, y, 0);
uchar *pd = cvPtr2D(vecpic, y, 0);
uchar *pvc = cvPtr2D(vc, y, 0);
uchar *pvcc = cvPtr2D(vcc, y, 0);
for (x = 0; x < sw; x++) {
uchar v =
(0x01 & ps[-2 * sstride]) |
(0x02 & ps[-sstride + 1]) |
(0x04 & ps[2]) |
(0x08 & ps[sstride + 1]) |
(0x10 & ps[2 * sstride]) |
(0x20 & ps[sstride - 1]) |
(0x40 & ps[-2]) |
(0x80 & ps[-sstride -1]);
*pd++ = v;
*pvc++ = cblk[v];
*pvcc++ = ccblk[v];
ps++;
}
}
apron(vc);
apron(vcc);
}
cfollow(vc, cxy);
cfollow(vcc, ccxy);
std::deque <CvPoint> candidates;
{
int x, y;
int rows = cxy->rows;
int cols = cxy->cols;
for (y = 0; y < rows; y++) {
const short *cd = (const short*)cvPtr2D(cxy, y, 0);
const short *ccd = (const short*)cvPtr2D(ccxy, y, 0);
for (x = 0; x < cols; x += 4, cd += 8, ccd += 8) {
__m128i v = _mm_loadu_si128((const __m128i*)cd);
__m128 cyxyxA = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(v, v), 16));
__m128 cyxyxB = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpackhi_epi16(v, v), 16));
__m128 cx = _mm_shuffle_ps(cyxyxA, cyxyxB, _MM_SHUFFLE(2, 0, 2, 0));
__m128 cy = _mm_shuffle_ps(cyxyxA, cyxyxB, _MM_SHUFFLE(3, 1, 3, 1));
__m128 cmag = _mm_sqrt_ps(_mm_add_ps(_mm_mul_ps(cx, cx), _mm_mul_ps(cy, cy)));
__m128 crmag = _mm_rcp_ps(cmag);
__m128 ncx = _mm_mul_ps(cx, crmag);
__m128 ncy = _mm_mul_ps(cy, crmag);
v = _mm_loadu_si128((const __m128i*)ccd);
__m128 ccyxyxA = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(v, v), 16));
__m128 ccyxyxB = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpackhi_epi16(v, v), 16));
__m128 ccx = _mm_shuffle_ps(ccyxyxA, ccyxyxB, _MM_SHUFFLE(2, 0, 2, 0));
__m128 ccy = _mm_shuffle_ps(ccyxyxA, ccyxyxB, _MM_SHUFFLE(3, 1, 3, 1));
__m128 ccmag = _mm_sqrt_ps(_mm_add_ps(_mm_mul_ps(ccx, ccx), _mm_mul_ps(ccy, ccy)));
__m128 ccrmag = _mm_rcp_ps(ccmag);
__m128 nccx = _mm_mul_ps(ccx, ccrmag);
__m128 nccy = _mm_mul_ps(ccy, ccrmag);
__m128 dot = _mm_mul_ps(_mm_mul_ps(ncx, nccx), _mm_mul_ps(ncy, nccy));
// iscand = (cmag > 30) & (ccmag > 30) & (numpy.minimum(cmag, ccmag) * 1.1 > numpy.maximum(cmag, ccmag)) & (abs(dot) < 0.25)
__m128 iscand = _mm_and_ps(_mm_cmpgt_ps(cmag, Kf(30)), _mm_cmpgt_ps(ccmag, Kf(30)));
iscand = _mm_and_ps(iscand, _mm_cmpgt_ps(_mm_mul_ps(_mm_min_ps(cmag, ccmag), Kf(1.1f)), _mm_max_ps(cmag, ccmag)));
iscand = _mm_and_ps(iscand, _mm_cmplt_ps(_mm_abs_ps(dot), Kf(0.25f)));
unsigned int CV_DECL_ALIGNED(16) result[4];
_mm_store_ps((float*)result, iscand);
int ix;
CvPoint np;
for (ix = 0; ix < 4; ix++) {
if (result[ix]) {
np.x = x + ix;
np.y = y;
candidates.push_back(np);
}
}
}
}
}
std::deque <code> codes;
size_t i, j, k;
while (!candidates.empty()) {
CvPoint o = candidates.front();
candidates.pop_front();
std::deque<CvPoint> ptc = trailto(vc, o.x, o.y, cxy);
std::deque<CvPoint> ptcc = trailto(vcc, o.x, o.y, ccxy);
for (j = 0; j < ptc.size(); j++) {
for (k = 0; k < ptcc.size(); k++) {
code cc;
Sampler sa(im, o, ptc[j], ptcc[k]);
for (i = 0; i < codes.size(); i++) {
if (sa.overlap(codes[i].sa))
{
cvReleaseMat(&sa.perim);
goto endo;
}
}
if (codes.size() > 0) {
//printf("searching for more\n");
}
if (decode(sa, cc)) {
codes.push_back(cc);
goto endo;
}
cvReleaseMat(&sa.perim);
}
}
endo: ; // end search for this o
}
cvReleaseMat(&thresh);
cvReleaseMat(&vecpic);
cvReleaseMat(&vc);
cvReleaseMat(&vcc);
cvReleaseMat(&cxy);
cvReleaseMat(&ccxy);
std::deque <CvDataMatrixCode> rc;
for (i = 0; i < codes.size(); i++) {
CvDataMatrixCode cc;
strcpy(cc.msg, codes[i].msg);
cc.original = codes[i].original;
cc.corners = codes[i].sa.perim;
rc.push_back(cc);
}
return rc;
#else
(void)im;
std::deque <CvDataMatrixCode> rc;
return rc;
#endif
}
#include <opencv2/imgproc.hpp>
namespace cv
{
void findDataMatrix(InputArray _image,
std::vector<String>& codes,
OutputArray _corners,
OutputArrayOfArrays _dmtx)
{
Mat image = _image.getMat();
CvMat m(image);
std::deque <CvDataMatrixCode> rc = cvFindDataMatrix(&m);
int i, n = (int)rc.size();
Mat corners;
if( _corners.needed() )
{
_corners.create(n, 4, CV_32SC2);
corners = _corners.getMat();
}
if( _dmtx.needed() )
_dmtx.create(n, 1, CV_8U);
codes.resize(n);
for( i = 0; i < n; i++ )
{
CvDataMatrixCode& rc_i = rc[i];
codes[i] = String(rc_i.msg);
if( corners.data )
{
const Point* srcpt = (Point*)rc_i.corners->data.ptr;
Point* dstpt = (Point*)corners.ptr(i);
for( int k = 0; k < 4; k++ )
dstpt[k] = srcpt[k];
}
cvReleaseMat(&rc_i.corners);
if( _dmtx.needed() )
{
_dmtx.create(rc_i.original->rows, rc_i.original->cols, rc_i.original->type, i);
Mat dst = _dmtx.getMat(i);
cv::cvarrToMat(rc_i.original).copyTo(dst);
}
cvReleaseMat(&rc_i.original);
}
}
void drawDataMatrixCodes(InputOutputArray _image,
const std::vector<String>& codes,
InputArray _corners)
{
Mat image = _image.getMat();
Mat corners = _corners.getMat();
int i, n = corners.rows;
if( n > 0 )
{
CV_Assert( corners.depth() == CV_32S &&
corners.cols*corners.channels() == 8 &&
n == (int)codes.size() );
}
for( i = 0; i < n; i++ )
{
Scalar c(0, 255, 0);
Scalar c2(255, 0,0);
const Point* pt = (const Point*)corners.ptr(i);
for( int k = 0; k < 4; k++ )
line(image, pt[k], pt[(k+1)%4], c);
//int baseline = 0;
//Size sz = getTextSize(code_text, CV_FONT_HERSHEY_SIMPLEX, 1, 1, &baseline);
putText(image, codes[i], pt[0], CV_FONT_HERSHEY_SIMPLEX, 0.8, c2, 1, CV_AA, false);
}
}
}

View File

@@ -1,383 +0,0 @@
#include "precomp.hpp"
#include "_lsvm_distancetransform.h"
/*
// Computation the point of intersection functions
// (parabolas on the variable y)
// a(y - q1) + b(q1 - y)(q1 - y) + f[q1]
// a(y - q2) + b(q2 - y)(q2 - y) + f[q2]
//
//
// API
// int GetPointOfIntersection(const float *f,
const float a, const float b,
int q1, int q2, float *point);
// INPUT
// f - function on the regular grid
// a - coefficient of the function
// b - coefficient of the function
// q1 - parameter of the function
// q2 - parameter of the function
// OUTPUT
// point - point of intersection
// RESULT
// Error status
*/
int GetPointOfIntersection(const float *f,
const float a, const float b,
int q1, int q2, float *point)
{
if (q1 == q2)
{
return DISTANCE_TRANSFORM_EQUAL_POINTS;
} /* if (q1 == q2) */
(*point) = ( (f[q2] - a * q2 + b *q2 * q2) -
(f[q1] - a * q1 + b * q1 * q1) ) / (2 * b * (q2 - q1));
return DISTANCE_TRANSFORM_OK;
}
/*
// Decision of one dimensional problem generalized distance transform
// on the regular grid at all points
// min (a(y' - y) + b(y' - y)(y' - y) + f(y')) (on y')
//
// API
// int DistanceTransformOneDimensionalProblem(const float *f, const int n,
const float a, const float b,
float *distanceTransform,
int *points);
// INPUT
// f - function on the regular grid
// n - grid dimension
// a - coefficient of optimizable function
// b - coefficient of optimizable function
// OUTPUT
// distanceTransform - values of generalized distance transform
// points - arguments that corresponds to the optimal value of function
// RESULT
// Error status
*/
int DistanceTransformOneDimensionalProblem(const float *f, const int n,
const float a, const float b,
float *distanceTransform,
int *points)
{
int i, k;
int tmp;
int diff;
float pointIntersection;
int *v;
float *z;
k = 0;
// Allocation memory (must be free in this function)
v = (int *)malloc (sizeof(int) * n);
z = (float *)malloc (sizeof(float) * (n + 1));
v[0] = 0;
z[0] = (float)F_MIN; // left border of envelope
z[1] = (float)F_MAX; // right border of envelope
for (i = 1; i < n; i++)
{
tmp = GetPointOfIntersection(f, a, b, v[k], i, &pointIntersection);
if (tmp != DISTANCE_TRANSFORM_OK)
{
free(v);
free(z);
return DISTANCE_TRANSFORM_GET_INTERSECTION_ERROR;
} /* if (tmp != DISTANCE_TRANSFORM_OK) */
if (pointIntersection <= z[k])
{
// Envelope doesn't contain current parabola
do
{
k--;
tmp = GetPointOfIntersection(f, a, b, v[k], i, &pointIntersection);
if (tmp != DISTANCE_TRANSFORM_OK)
{
free(v);
free(z);
return DISTANCE_TRANSFORM_GET_INTERSECTION_ERROR;
} /* if (tmp != DISTANCE_TRANSFORM_OK) */
}while (pointIntersection <= z[k]);
// Addition parabola to the envelope
k++;
v[k] = i;
z[k] = pointIntersection;
z[k + 1] = (float)F_MAX;
}
else
{
// Addition parabola to the envelope
k++;
v[k] = i;
z[k] = pointIntersection;
z[k + 1] = (float)F_MAX;
} /* if (pointIntersection <= z[k]) */
}
// Computation values of generalized distance transform at all grid points
k = 0;
for (i = 0; i < n; i++)
{
while (z[k + 1] < i)
{
k++;
}
points[i] = v[k];
diff = i - v[k];
distanceTransform[i] = a * diff + b * diff * diff + f[v[k]];
}
// Release allocated memory
free(v);
free(z);
return DISTANCE_TRANSFORM_OK;
}
/*
// Computation next cycle element
//
// API
// int GetNextCycleElement(int k, int n, int q);
// INPUT
// k - index of the previous cycle element
// n - number of matrix rows
// q - parameter that equal
(number_of_rows * number_of_columns - 1)
// OUTPUT
// None
// RESULT
// Next cycle element
*/
int GetNextCycleElement(int k, int n, int q)
{
return ((k * n) % q);
}
/*
// Transpose cycle elements
//
// API
// void TransposeCycleElements(float *a, int *cycle, int cycle_len)
// INPUT
// a - initial matrix
// cycle - indeces array of cycle
// cycle_len - number of elements in the cycle
// OUTPUT
// a - matrix with transposed elements
// RESULT
// Error status
*/
void TransposeCycleElements(float *a, int *cycle, int cycle_len)
{
int i;
float buf;
for (i = cycle_len - 1; i > 0 ; i--)
{
buf = a[ cycle[i] ];
a[ cycle[i] ] = a[ cycle[i - 1] ];
a[ cycle[i - 1] ] = buf;
}
}
/*
// Transpose cycle elements
//
// API
// void TransposeCycleElements(int *a, int *cycle, int cycle_len)
// INPUT
// a - initial matrix
// cycle - indeces array of cycle
// cycle_len - number of elements in the cycle
// OUTPUT
// a - matrix with transposed elements
// RESULT
// Error status
*/
static void TransposeCycleElements_int(int *a, int *cycle, int cycle_len)
{
int i;
int buf;
for (i = cycle_len - 1; i > 0 ; i--)
{
buf = a[ cycle[i] ];
a[ cycle[i] ] = a[ cycle[i - 1] ];
a[ cycle[i - 1] ] = buf;
}
}
/*
// Getting transposed matrix
//
// API
// void Transpose(float *a, int n, int m);
// INPUT
// a - initial matrix
// n - number of rows
// m - number of columns
// OUTPUT
// a - transposed matrix
// RESULT
// None
*/
void Transpose(float *a, int n, int m)
{
int *cycle;
int i, k, q, cycle_len;
int max_cycle_len;
max_cycle_len = n * m;
// Allocation memory (must be free in this function)
cycle = (int *)malloc(sizeof(int) * max_cycle_len);
cycle_len = 0;
q = n * m - 1;
for (i = 1; i < q; i++)
{
k = GetNextCycleElement(i, n, q);
cycle[cycle_len] = i;
cycle_len++;
while (k > i)
{
cycle[cycle_len] = k;
cycle_len++;
k = GetNextCycleElement(k, n, q);
}
if (k == i)
{
TransposeCycleElements(a, cycle, cycle_len);
} /* if (k == i) */
cycle_len = 0;
}
// Release allocated memory
free(cycle);
}
/*
// Getting transposed matrix
//
// API
// void Transpose_int(int *a, int n, int m);
// INPUT
// a - initial matrix
// n - number of rows
// m - number of columns
// OUTPUT
// a - transposed matrix
// RESULT
// None
*/
static void Transpose_int(int *a, int n, int m)
{
int *cycle;
int i, k, q, cycle_len;
int max_cycle_len;
max_cycle_len = n * m;
// Allocation memory (must be free in this function)
cycle = (int *)malloc(sizeof(int) * max_cycle_len);
cycle_len = 0;
q = n * m - 1;
for (i = 1; i < q; i++)
{
k = GetNextCycleElement(i, n, q);
cycle[cycle_len] = i;
cycle_len++;
while (k > i)
{
cycle[cycle_len] = k;
cycle_len++;
k = GetNextCycleElement(k, n, q);
}
if (k == i)
{
TransposeCycleElements_int(a, cycle, cycle_len);
} /* if (k == i) */
cycle_len = 0;
}
// Release allocated memory
free(cycle);
}
/*
// Decision of two dimensional problem generalized distance transform
// on the regular grid at all points
// min{d2(y' - y) + d4(y' - y)(y' - y) +
min(d1(x' - x) + d3(x' - x)(x' - x) + f(x',y'))} (on x', y')
//
// API
// int DistanceTransformTwoDimensionalProblem(const float *f,
const int n, const int m,
const float coeff[4],
float *distanceTransform,
int *pointsX, int *pointsY);
// INPUT
// f - function on the regular grid
// n - number of rows
// m - number of columns
// coeff - coefficients of optimizable function
coeff[0] = d1, coeff[1] = d2,
coeff[2] = d3, coeff[3] = d4
// OUTPUT
// distanceTransform - values of generalized distance transform
// pointsX - arguments x' that correspond to the optimal value
// pointsY - arguments y' that correspond to the optimal value
// RESULT
// Error status
*/
int DistanceTransformTwoDimensionalProblem(const float *f,
const int n, const int m,
const float coeff[4],
float *distanceTransform,
int *pointsX, int *pointsY)
{
int i, j, tmp;
int resOneDimProblem;
int size = n * m;
std::vector<float> internalDistTrans(size);
std::vector<int> internalPointsX(size);
for (i = 0; i < n; i++)
{
resOneDimProblem = DistanceTransformOneDimensionalProblem(
f + i * m, m,
coeff[0], coeff[2],
&internalDistTrans[i * m],
&internalPointsX[i * m]);
if (resOneDimProblem != DISTANCE_TRANSFORM_OK)
return DISTANCE_TRANSFORM_ERROR;
}
Transpose(&internalDistTrans[0], n, m);
for (j = 0; j < m; j++)
{
resOneDimProblem = DistanceTransformOneDimensionalProblem(
&internalDistTrans[j * n], n,
coeff[1], coeff[3],
distanceTransform + j * n,
pointsY + j * n);
if (resOneDimProblem != DISTANCE_TRANSFORM_OK)
return DISTANCE_TRANSFORM_ERROR;
}
Transpose(distanceTransform, m, n);
Transpose_int(pointsY, m, n);
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
tmp = pointsY[i * m + j];
pointsX[i * m + j] = internalPointsX[tmp * m + j];
}
}
return DISTANCE_TRANSFORM_OK;
}

View File

@@ -1,515 +0,0 @@
#include "precomp.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "_latentsvm.h"
#include "_lsvm_resizeimg.h"
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
/*
// Getting feature map for the selected subimage
//
// 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
*/
int getFeatureMaps(const IplImage* image, const int k, CvLSVMFeatureMap **map)
{
int sizeX, sizeY;
int p, px, stringSize;
int height, width, numChannels;
int i, j, kk, c, ii, jj, d;
float * datadx, * datady;
int ch;
float magnitude, x, y, tx, ty;
IplImage * dx, * dy;
int *nearest;
float *w, a_x, b_x;
float kernel[3] = {-1.f, 0.f, 1.f};
CvMat kernel_dx = cvMat(1, 3, CV_32F, kernel);
CvMat kernel_dy = cvMat(3, 1, CV_32F, kernel);
float * r;
int * alfa;
float boundary_x[NUM_SECTOR + 1];
float boundary_y[NUM_SECTOR + 1];
float max, dotProd;
int maxi;
height = image->height;
width = image->width ;
numChannels = image->nChannels;
dx = cvCreateImage(cvSize(image->width, image->height),
IPL_DEPTH_32F, 3);
dy = cvCreateImage(cvSize(image->width, image->height),
IPL_DEPTH_32F, 3);
sizeX = width / k;
sizeY = height / k;
px = 3 * NUM_SECTOR;
p = px;
stringSize = sizeX * p;
allocFeatureMapObject(map, sizeX, sizeY, p);
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++)
{
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++) */
r = (float *)malloc( sizeof(float) * (width * height));
alfa = (int *)malloc( sizeof(int ) * (width * height * 2));
for(j = 1; j < height - 1; j++)
{
datadx = (float*)(dx->imageData + dx->widthStep * j);
datady = (float*)(dy->imageData + dy->widthStep * j);
for(i = 1; i < width - 1; i++)
{
c = 0;
x = (datadx[i * numChannels + c]);
y = (datady[i * numChannels + c]);
r[j * width + i] =sqrtf(x * x + y * y);
for(ch = 1; ch < numChannels; ch++)
{
tx = (datadx[i * numChannels + ch]);
ty = (datady[i * numChannels + ch]);
magnitude = sqrtf(tx * tx + ty * ty);
if(magnitude > r[j * width + i])
{
r[j * width + i] = magnitude;
c = ch;
x = tx;
y = ty;
}
}/*for(ch = 1; ch < numChannels; ch++)*/
max = boundary_x[0] * x + boundary_y[0] * y;
maxi = 0;
for (kk = 0; kk < NUM_SECTOR; kk++)
{
dotProd = boundary_x[kk] * x + boundary_y[kk] * y;
if (dotProd > max)
{
max = dotProd;
maxi = kk;
}
else
{
if (-dotProd > max)
{
max = -dotProd;
maxi = kk + NUM_SECTOR;
}
}
}
alfa[j * width * 2 + i * 2 ] = maxi % NUM_SECTOR;
alfa[j * width * 2 + i * 2 + 1] = maxi;
}/*for(i = 0; i < width; i++)*/
}/*for(j = 0; j < height; j++)*/
nearest = (int *)malloc(sizeof(int ) * k);
w = (float*)malloc(sizeof(float) * (k * 2));
for(i = 0; i < k / 2; i++)
{
nearest[i] = -1;
}/*for(i = 0; i < k / 2; i++)*/
for(i = k / 2; i < k; i++)
{
nearest[i] = 1;
}/*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++)*/
for(i = 0; i < sizeY; i++)
{
for(j = 0; j < sizeX; j++)
{
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))
{
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++)*/
}/*for(i = 1; i < sizeY - 1; i++)*/
cvReleaseImage(&dx);
cvReleaseImage(&dy);
free(w);
free(nearest);
free(r);
free(alfa);
return LATENT_SVM_OK;
}
/*
// Feature map Normalization and Truncation
//
// API
// int normalizeAndTruncate(featureMap *map, const float alfa);
// INPUT
// map - feature map
// alfa - truncation threshold
// OUTPUT
// map - truncated and normalized feature map
// RESULT
// Error status
*/
int normalizeAndTruncate(CvLSVMFeatureMap *map, const float alfa)
{
int i,j, ii;
int sizeX, sizeY, p, pos, pp, xp, pos1, pos2;
float * partOfNorm; // norm of C(i, j)
float * newData;
float valOfNorm;
sizeX = map->sizeX;
sizeY = map->sizeY;
partOfNorm = (float *)malloc (sizeof(float) * (sizeX * sizeY));
p = NUM_SECTOR;
xp = NUM_SECTOR * 3;
pp = NUM_SECTOR * 12;
for(i = 0; i < sizeX * sizeY; i++)
{
valOfNorm = 0.0f;
pos = i * map->numFeatures;
for(j = 0; j < p; j++)
{
valOfNorm += map->map[pos + j] * map->map[pos + j];
}/*for(j = 0; j < p; j++)*/
partOfNorm[i] = valOfNorm;
}/*for(i = 0; i < sizeX * sizeY; i++)*/
sizeX -= 2;
sizeY -= 2;
newData = (float *)malloc (sizeof(float) * (sizeX * sizeY * pp));
//normalization
for(i = 1; i <= sizeY; i++)
{
for(j = 1; j <= sizeX; j++)
{
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)]) + FLT_EPSILON;
pos1 = (i ) * (sizeX + 2) * xp + (j ) * xp;
pos2 = (i-1) * (sizeX ) * pp + (j-1) * pp;
for(ii = 0; ii < p; ii++)
{
newData[pos2 + ii ] = map->map[pos1 + ii ] / valOfNorm;
}/*for(ii = 0; ii < p; ii++)*/
for(ii = 0; ii < 2 * p; ii++)
{
newData[pos2 + ii + p * 4] = map->map[pos1 + ii + p] / valOfNorm;
}/*for(ii = 0; ii < 2 * p; ii++)*/
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)]) + FLT_EPSILON;
for(ii = 0; ii < p; ii++)
{
newData[pos2 + ii + p ] = map->map[pos1 + ii ] / valOfNorm;
}/*for(ii = 0; ii < p; ii++)*/
for(ii = 0; ii < 2 * p; ii++)
{
newData[pos2 + ii + p * 6] = map->map[pos1 + ii + p] / valOfNorm;
}/*for(ii = 0; ii < 2 * p; ii++)*/
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)]) + FLT_EPSILON;
for(ii = 0; ii < p; ii++)
{
newData[pos2 + ii + p * 2] = map->map[pos1 + ii ] / valOfNorm;
}/*for(ii = 0; ii < p; ii++)*/
for(ii = 0; ii < 2 * p; ii++)
{
newData[pos2 + ii + p * 8] = map->map[pos1 + ii + p] / valOfNorm;
}/*for(ii = 0; ii < 2 * p; ii++)*/
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)]) + FLT_EPSILON;
for(ii = 0; ii < p; ii++)
{
newData[pos2 + ii + p * 3 ] = map->map[pos1 + ii ] / valOfNorm;
}/*for(ii = 0; ii < p; ii++)*/
for(ii = 0; ii < 2 * p; ii++)
{
newData[pos2 + ii + p * 10] = map->map[pos1 + ii + p] / valOfNorm;
}/*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++)
{
if(newData [i] > alfa) newData [i] = alfa;
}/*for(i = 0; i < sizeX * sizeY * pp; i++)*/
//swap data
map->numFeatures = pp;
map->sizeX = sizeX;
map->sizeY = sizeY;
free (map->map);
free (partOfNorm);
map->map = newData;
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
*/
int PCAFeatureMaps(CvLSVMFeatureMap *map)
{
int i,j, ii, jj, k;
int sizeX, sizeY, p, pp, xp, yp, pos1, pos2;
float * newData;
float val;
float nx, ny;
sizeX = map->sizeX;
sizeY = map->sizeY;
p = map->numFeatures;
pp = NUM_SECTOR * 3 + 4;
yp = 4;
xp = NUM_SECTOR;
nx = 1.0f / sqrtf((float)(xp * 2));
ny = 1.0f / sqrtf((float)(yp ));
newData = (float *)malloc (sizeof(float) * (sizeX * sizeY * pp));
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++)
{
val += map->map[pos1 + yp * xp + ii * xp * 2 + jj];
}/*for(ii = 0; ii < yp; ii++)*/
newData[pos2 + k] = val * ny;
k++;
}/*for(jj = 0; jj < xp * 2; jj++)*/
for(jj = 0; jj < xp; jj++)
{
val = 0;
for(ii = 0; ii < yp; ii++)
{
val += map->map[pos1 + ii * xp + jj];
}/*for(ii = 0; ii < yp; ii++)*/
newData[pos2 + k] = val * ny;
k++;
}/*for(jj = 0; jj < xp; jj++)*/
for(ii = 0; ii < yp; ii++)
{
val = 0;
for(jj = 0; jj < 2 * xp; jj++)
{
val += map->map[pos1 + yp * xp + ii * xp * 2 + jj];
}/*for(jj = 0; jj < xp; jj++)*/
newData[pos2 + k] = val * nx;
k++;
} /*for(ii = 0; ii < yp; ii++)*/
}/*for(j = 0; j < sizeX; j++)*/
}/*for(i = 0; i < sizeY; i++)*/
//swap data
map->numFeatures = pp;
free (map->map);
map->map = newData;
return LATENT_SVM_OK;
}
static int getPathOfFeaturePyramid(IplImage * image,
float step, int numStep, int startIndex,
int sideLength, CvLSVMFeaturePyramid **maps)
{
CvLSVMFeatureMap *map;
IplImage *scaleTmp;
float scale;
int i;
for(i = 0; i < numStep; i++)
{
scale = 1.0f / powf(step, (float)i);
scaleTmp = resize_opencv (image, scale);
getFeatureMaps(scaleTmp, sideLength, &map);
normalizeAndTruncate(map, VAL_OF_TRUNCATE);
PCAFeatureMaps(map);
(*maps)->pyramid[startIndex + i] = map;
cvReleaseImage(&scaleTmp);
}/*for(i = 0; i < numStep; i++)*/
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
*/
int getFeaturePyramid(IplImage * image, CvLSVMFeaturePyramid **maps)
{
IplImage *imgResize;
float step;
int numStep;
int maxNumCells;
int W, H;
if(image->depth == IPL_DEPTH_32F)
{
imgResize = image;
}
else
{
imgResize = cvCreateImage(cvSize(image->width , image->height) ,
IPL_DEPTH_32F , 3);
cvConvert(image, imgResize);
}
W = imgResize->width;
H = imgResize->height;
step = powf(2.0f, 1.0f / ((float)LAMBDA));
maxNumCells = W / SIDE_LENGTH;
if( maxNumCells > H / SIDE_LENGTH )
{
maxNumCells = H / SIDE_LENGTH;
}
numStep = (int)(logf((float) maxNumCells / (5.0f)) / logf( step )) + 1;
allocFeaturePyramidObject(maps, numStep + LAMBDA);
getPathOfFeaturePyramid(imgResize, step , LAMBDA, 0,
SIDE_LENGTH / 2, maps);
getPathOfFeaturePyramid(imgResize, step, numStep, LAMBDA,
SIDE_LENGTH , maps);
if(image->depth != IPL_DEPTH_32F)
{
cvReleaseImage(&imgResize);
}
return LATENT_SVM_OK;
}

View File

@@ -1,246 +0,0 @@
#include "precomp.hpp"
#include "_lsvm_fft.h"
// static int getEntireRes(int number, int divisor, int *entire, int *res)
// {
// *entire = number / divisor;
// *res = number % divisor;
// return FFT_OK;
// }
static int getMultipliers(int n, int *n1, int *n2)
{
int multiplier, i;
if (n == 1)
{
*n1 = 1;
*n2 = 1;
return FFT_ERROR; // n = 1
}
multiplier = n / 2;
for (i = multiplier; i >= 2; i--)
{
if (n % i == 0)
{
*n1 = i;
*n2 = n / i;
return FFT_OK; // n = n1 * n2
}
}
*n1 = 1;
*n2 = n;
return FFT_ERROR; // n - prime number
}
/*
// 1-dimensional FFT
//
// API
// int fft(float *x_in, float *x_out, int n, int shift);
// INPUT
// x_in - input signal
// n - number of elements for searching Fourier image
// shift - shift between input elements
// OUTPUT
// x_out - output signal (contains 2n elements in order
Re(x_in[0]), Im(x_in[0]), Re(x_in[1]), Im(x_in[1]) and etc.)
// RESULT
// Error status
*/
int fft(float *x_in, float *x_out, int n, int shift)
{
int n1, n2, res, k1, k2, m1, m2, index, idx;
float alpha, beta, gamma, angle, cosAngle, sinAngle;
float tmpGamma, tmpAlpha, tmpBeta;
float tmpRe, tmpIm, phaseRe, phaseIm;
res = getMultipliers(n, &n1, &n2);
if (res == FFT_OK)
{
fft(x_in, x_out, n1, shift);
fft(x_in, x_out, n2, shift);
}
alpha = (float)(2.0 * PI / ((float)n));
beta = (float)(2.0 * PI / ((float)n1));
gamma = (float)(2.0 * PI / ((float)n2));
for (k1 = 0; k1 < n1; k1++)
{
tmpBeta = beta * k1;
for (k2 = 0; k2 < n2; k2++)
{
idx = shift * (n2 * k1 + k2);
x_out[idx] = 0.0;
x_out[idx + 1] = 0.0;
tmpGamma = gamma * k2;
tmpAlpha = alpha * k2;
for (m1 = 0; m1 < n1; m1++)
{
tmpRe = 0.0;
tmpIm = 0.0;
for (m2 = 0; m2 < n2; m2++)
{
angle = tmpGamma * m2;
index = shift * (n1 * m2 + m1);
cosAngle = cosf(angle);
sinAngle = sinf(angle);
tmpRe += x_in[index] * cosAngle + x_in[index + 1] * sinAngle;
tmpIm += x_in[index + 1] * cosAngle - x_in[index] * sinAngle;
}
angle = tmpAlpha * m1;
cosAngle = cosf(angle);
sinAngle = sinf(angle);
phaseRe = cosAngle * tmpRe + sinAngle * tmpIm;
phaseIm = cosAngle * tmpIm - sinAngle * tmpRe;
angle = tmpBeta * m1;
cosAngle = cosf(angle);
sinAngle = sinf(angle);
x_out[idx] += (cosAngle * phaseRe + sinAngle * phaseIm);
x_out[idx + 1] += (cosAngle * phaseIm - sinAngle * phaseRe);
}
}
}
return FFT_OK;
}
/*
// Inverse 1-dimensional FFT
//
// API
// int fftInverse(float *x_in, float *x_out, int n, int shift);
// INPUT
// x_in - Fourier image of 1d input signal(contains 2n elements
in order Re(x_in[0]), Im(x_in[0]),
Re(x_in[1]), Im(x_in[1]) and etc.)
// n - number of elements for searching counter FFT image
// shift - shift between input elements
// OUTPUT
// x_in - input signal (contains n elements)
// RESULT
// Error status
*/
int fftInverse(float *x_in, float *x_out, int n, int shift)
{
int n1, n2, res, k1, k2, m1, m2, index, idx;
float alpha, beta, gamma, angle, cosAngle, sinAngle;
float tmpRe, tmpIm, phaseRe, phaseIm;
res = getMultipliers(n, &n1, &n2);
if (res == FFT_OK)
{
fftInverse(x_in, x_out, n1, shift);
fftInverse(x_in, x_out, n2, shift);
}
alpha = (float)(2.0f * PI / ((float)n));
beta = (float)(2.0f * PI / ((float)n1));
gamma = (float)(2.0f * PI / ((float)n2));
for (m1 = 0; m1 < n1; m1++)
{
for (m2 = 0; m2 < n2; m2++)
{
idx = (n1 * m2 + m1) * shift;
x_out[idx] = 0.0;
x_out[idx + 1] = 0.0;
for (k2 = 0; k2 < n2; k2++)
{
tmpRe = 0.0;
tmpIm = 0.0;
for (k1 = 0; k1 < n1; k1++)
{
angle = beta * k1 * m1;
index = shift *(n2 * k1 + k2);
sinAngle = sinf(angle);
cosAngle = cosf(angle);
tmpRe += x_in[index] * cosAngle - x_in[index + 1] * sinAngle;
tmpIm += x_in[index] * sinAngle + x_in[index + 1] * cosAngle;
}
angle = alpha * m1 * k2;
sinAngle = sinf(angle);
cosAngle = cosf(angle);
phaseRe = cosAngle * tmpRe - sinAngle * tmpIm;
phaseIm = cosAngle * tmpIm + sinAngle * tmpRe;
angle = gamma * k2 * m2;
sinAngle = sinf(angle);
cosAngle = cosf(angle);
x_out[idx] += cosAngle * phaseRe - sinAngle * phaseIm;
x_out[idx + 1] += cosAngle * phaseIm + sinAngle * phaseRe;
}
x_out[idx] /= n;
x_out[idx + 1] /= n;
}
}
return FFT_OK;
}
/*
// 2-dimensional FFT
//
// API
// int fft2d(float *x_in, float *x_out, int numRows, int numColls);
// INPUT
// x_in - input signal (matrix, launched by rows)
// numRows - number of rows
// numColls - number of collumns
// OUTPUT
// x_out - output signal (contains (2 * numRows * numColls) elements
in order Re(x_in[0][0]), Im(x_in[0][0]),
Re(x_in[0][1]), Im(x_in[0][1]) and etc.)
// RESULT
// Error status
*/
int fft2d(float *x_in, float *x_out, int numRows, int numColls)
{
int i, size;
float *x_outTmp;
size = numRows * numColls;
x_outTmp = (float *)malloc(sizeof(float) * (2 * size));
for (i = 0; i < numRows; i++)
{
fft(x_in + i * 2 * numColls,
x_outTmp + i * 2 * numColls,
numColls, 2);
}
for (i = 0; i < numColls; i++)
{
fft(x_outTmp + 2 * i,
x_out + 2 * i,
numRows, 2 * numColls);
}
free(x_outTmp);
return FFT_OK;
}
/*
// Inverse 2-dimensional FFT
//
// API
// int fftInverse2d(float *x_in, float *x_out, int numRows, int numColls);
// INPUT
// x_in - Fourier image of matrix (contains (2 * numRows * numColls)
elements in order Re(x_in[0][0]), Im(x_in[0][0]),
Re(x_in[0][1]), Im(x_in[0][1]) and etc.)
// numRows - number of rows
// numColls - number of collumns
// OUTPUT
// x_out - initial signal (matrix, launched by rows)
// RESULT
// Error status
*/
int fftInverse2d(float *x_in, float *x_out, int numRows, int numColls)
{
int i, size;
float *x_outTmp;
size = numRows * numColls;
x_outTmp = (float *)malloc(sizeof(float) * (2 * size));
for (i = 0; i < numRows; i++)
{
fftInverse(x_in + i * 2 * numColls,
x_outTmp + i * 2 * numColls,
numColls, 2);
}
for (i = 0; i < numColls; i++)
{
fftInverse(x_outTmp + 2 * i,
x_out + 2 * i,
numRows, 2 * numColls);
}
free(x_outTmp);
return FFT_OK;
}

View File

@@ -1,644 +0,0 @@
#include "precomp.hpp"
#include "_latentsvm.h"
#include "_lsvm_matching.h"
/*
// Transformation filter displacement from the block space
// to the space of pixels at the initial image
//
// API
// int convertPoints(int countLevel, CvPoint *points, int *levels,
CvPoint **partsDisplacement, int kPoints, int n);
// INPUT
// countLevel - the number of levels in the feature pyramid
// points - the set of root filter positions (in the block space)
// levels - the set of levels
// partsDisplacement - displacement of part filters (in the block space)
// kPoints - number of root filter positions
// n - number of part filters
// initialImageLevel - level that contains features for initial image
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// OUTPUT
// points - the set of root filter positions (in the space of pixels)
// partsDisplacement - displacement of part filters (in the space of pixels)
// RESULT
// Error status
*/
int convertPoints(int /*countLevel*/, int lambda,
int initialImageLevel,
CvPoint *points, int *levels,
CvPoint **partsDisplacement, int kPoints, int n,
int maxXBorder,
int maxYBorder)
{
int i, j, bx, by;
float step, scale;
step = powf( 2.0f, 1.0f / ((float)lambda) );
computeBorderSize(maxXBorder, maxYBorder, &bx, &by);
for (i = 0; i < kPoints; i++)
{
// scaling factor for root filter
scale = SIDE_LENGTH * powf(step, (float)(levels[i] - initialImageLevel));
points[i].x = (int)((points[i].x - bx + 1) * scale);
points[i].y = (int)((points[i].y - by + 1) * scale);
// scaling factor for part filters
scale = SIDE_LENGTH * powf(step, (float)(levels[i] - lambda - initialImageLevel));
for (j = 0; j < n; j++)
{
partsDisplacement[i][j].x = (int)((partsDisplacement[i][j].x -
2 * bx + 1) * scale);
partsDisplacement[i][j].y = (int)((partsDisplacement[i][j].y -
2 * by + 1) * scale);
}
}
return LATENT_SVM_OK;
}
/*
// Elimination boxes that are outside the image boudaries
//
// API
// int clippingBoxes(int width, int height,
CvPoint *points, int kPoints);
// INPUT
// width - image wediht
// height - image heigth
// points - a set of points (coordinates of top left or
bottom right corners)
// kPoints - points number
// OUTPUT
// points - updated points (if coordinates less than zero then
set zero coordinate, if coordinates more than image
size then set coordinates equal image size)
// RESULT
// Error status
*/
int clippingBoxes(int width, int height,
CvPoint *points, int kPoints)
{
int i;
for (i = 0; i < kPoints; i++)
{
if (points[i].x > width - 1)
{
points[i].x = width - 1;
}
if (points[i].x < 0)
{
points[i].x = 0;
}
if (points[i].y > height - 1)
{
points[i].y = height - 1;
}
if (points[i].y < 0)
{
points[i].y = 0;
}
}
return LATENT_SVM_OK;
}
/*
// Creation feature pyramid with nullable border
//
// API
// featurePyramid* createFeaturePyramidWithBorder(const IplImage *image,
int maxXBorder, int maxYBorder);
// INPUT
// image - initial image
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// OUTPUT
// RESULT
// Feature pyramid with nullable border
*/
CvLSVMFeaturePyramid* createFeaturePyramidWithBorder(IplImage *image,
int maxXBorder, int maxYBorder)
{
int opResult;
int bx, by;
int level;
CvLSVMFeaturePyramid *H;
// Obtaining feature pyramid
opResult = getFeaturePyramid(image, &H);
if (opResult != LATENT_SVM_OK)
{
freeFeaturePyramidObject(&H);
return NULL;
} /* if (opResult != LATENT_SVM_OK) */
// Addition nullable border for each feature map
// the size of the border for root filters
computeBorderSize(maxXBorder, maxYBorder, &bx, &by);
for (level = 0; level < H->numLevels; level++)
{
addNullableBorder(H->pyramid[level], bx, by);
}
return H;
}
/*
// Computation of the root filter displacement and values of score function
//
// API
// int searchObject(const featurePyramid *H, const filterObject **all_F, int n,
float b,
int maxXBorder,
int maxYBorder,
CvPoint **points, int **levels, int *kPoints, float *score,
CvPoint ***partsDisplacement);
// INPUT
// image - initial image for searhing object
// all_F - the set of filters (the first element is root filter,
other elements - part filters)
// n - the number of part filters
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// OUTPUT
// points - positions (x, y) of the upper-left corner
of root filter frame
// levels - levels that correspond to each position
// kPoints - number of positions
// score - value of the score function
// partsDisplacement - part filters displacement for each position
of the root filter
// RESULT
// Error status
*/
int searchObject(const CvLSVMFeaturePyramid *H, const CvLSVMFilterObject **all_F,
int n, float b,
int maxXBorder,
int maxYBorder,
CvPoint **points, int **levels, int *kPoints, float *score,
CvPoint ***partsDisplacement)
{
int opResult;
// Matching
opResult = maxFunctionalScore(all_F, n, H, b, maxXBorder, maxYBorder,
score, points, levels,
kPoints, partsDisplacement);
if (opResult != LATENT_SVM_OK)
{
return LATENT_SVM_SEARCH_OBJECT_FAILED;
}
// Transformation filter displacement from the block space
// to the space of pixels at the initial image
// that settles at the level number LAMBDA
convertPoints(H->numLevels, LAMBDA, LAMBDA, (*points),
(*levels), (*partsDisplacement), (*kPoints), n,
maxXBorder, maxYBorder);
return LATENT_SVM_OK;
}
/*
// Computation right bottom corners coordinates of bounding boxes
//
// API
// int estimateBoxes(CvPoint *points, int *levels, int kPoints,
int sizeX, int sizeY, CvPoint **oppositePoints);
// INPUT
// points - left top corners coordinates of bounding boxes
// levels - levels of feature pyramid where points were found
// (sizeX, sizeY) - size of root filter
// OUTPUT
// oppositePoins - right bottom corners coordinates of bounding boxes
// RESULT
// Error status
*/
static int estimateBoxes(CvPoint *points, int *levels, int kPoints,
int sizeX, int sizeY, CvPoint **oppositePoints)
{
int i;
float step;
step = powf( 2.0f, 1.0f / ((float)(LAMBDA)));
*oppositePoints = (CvPoint *)malloc(sizeof(CvPoint) * kPoints);
for (i = 0; i < kPoints; i++)
{
getOppositePoint(points[i], sizeX, sizeY, step, levels[i] - LAMBDA, &((*oppositePoints)[i]));
}
return LATENT_SVM_OK;
}
/*
// Computation of the root filter displacement and values of score function
//
// API
// int searchObjectThreshold(const featurePyramid *H,
const filterObject **all_F, int n,
float b,
int maxXBorder, int maxYBorder,
float scoreThreshold,
CvPoint **points, int **levels, int *kPoints,
float **score, CvPoint ***partsDisplacement);
// INPUT
// H - feature pyramid
// all_F - the set of filters (the first element is root filter,
other elements - part filters)
// n - the number of part filters
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// scoreThreshold - score threshold
// OUTPUT
// points - positions (x, y) of the upper-left corner
of root filter frame
// levels - levels that correspond to each position
// kPoints - number of positions
// score - values of the score function
// partsDisplacement - part filters displacement for each position
of the root filter
// RESULT
// Error status
*/
int searchObjectThreshold(const CvLSVMFeaturePyramid *H,
const CvLSVMFilterObject **all_F, int n,
float b,
int maxXBorder, int maxYBorder,
float scoreThreshold,
CvPoint **points, int **levels, int *kPoints,
float **score, CvPoint ***partsDisplacement,
int numThreads)
{
int opResult;
// Matching
#ifdef HAVE_TBB
if (numThreads <= 0)
{
opResult = LATENT_SVM_TBB_NUMTHREADS_NOT_CORRECT;
return opResult;
}
opResult = tbbThresholdFunctionalScore(all_F, n, H, b, maxXBorder, maxYBorder,
scoreThreshold, numThreads, score,
points, levels, kPoints,
partsDisplacement);
#else
opResult = thresholdFunctionalScore(all_F, n, H, b,
maxXBorder, maxYBorder,
scoreThreshold,
score, points, levels,
kPoints, partsDisplacement);
(void)numThreads;
#endif
if (opResult != LATENT_SVM_OK)
{
return LATENT_SVM_SEARCH_OBJECT_FAILED;
}
// Transformation filter displacement from the block space
// to the space of pixels at the initial image
// that settles at the level number LAMBDA
convertPoints(H->numLevels, LAMBDA, LAMBDA, (*points),
(*levels), (*partsDisplacement), (*kPoints), n,
maxXBorder, maxYBorder);
return LATENT_SVM_OK;
}
/*
// Compute opposite point for filter box
//
// API
// int getOppositePoint(CvPoint point,
int sizeX, int sizeY,
float step, int degree,
CvPoint *oppositePoint);
// INPUT
// point - coordinates of filter top left corner
(in the space of pixels)
// (sizeX, sizeY) - filter dimension in the block space
// step - scaling factor
// degree - degree of the scaling factor
// OUTPUT
// oppositePoint - coordinates of filter bottom corner
(in the space of pixels)
// RESULT
// Error status
*/
int getOppositePoint(CvPoint point,
int sizeX, int sizeY,
float step, int degree,
CvPoint *oppositePoint)
{
float scale;
scale = SIDE_LENGTH * powf(step, (float)degree);
oppositePoint->x = (int)(point.x + sizeX * scale);
oppositePoint->y = (int)(point.y + sizeY * scale);
return LATENT_SVM_OK;
}
/*
// Drawing root filter boxes
//
// API
// int showRootFilterBoxes(const IplImage *image,
const filterObject *filter,
CvPoint *points, int *levels, int kPoints,
CvScalar color, int thickness,
int line_type, int shift);
// INPUT
// image - initial image
// filter - root filter object
// points - a set of points
// levels - levels of feature pyramid
// kPoints - number of points
// color - line color for each box
// thickness - line thickness
// line_type - line type
// shift - shift
// OUTPUT
// window contained initial image and filter boxes
// RESULT
// Error status
*/
int showRootFilterBoxes(IplImage *image,
const CvLSVMFilterObject *filter,
CvPoint *points, int *levels, int kPoints,
CvScalar color, int thickness,
int line_type, int shift)
{
int i;
float step;
CvPoint oppositePoint;
step = powf( 2.0f, 1.0f / ((float)LAMBDA));
for (i = 0; i < kPoints; i++)
{
// Drawing rectangle for filter
getOppositePoint(points[i], filter->sizeX, filter->sizeY,
step, levels[i] - LAMBDA, &oppositePoint);
cvRectangle(image, points[i], oppositePoint,
color, thickness, line_type, shift);
}
#ifdef HAVE_OPENCV_HIGHGUI
cv::imshow("Initial image", cv::cvarrToMat(image));
#endif
return LATENT_SVM_OK;
}
/*
// Drawing part filter boxes
//
// API
// int showPartFilterBoxes(const IplImage *image,
const filterObject *filter,
CvPoint *points, int *levels, int kPoints,
CvScalar color, int thickness,
int line_type, int shift);
// INPUT
// image - initial image
// filters - a set of part filters
// n - number of part filters
// partsDisplacement - a set of points
// levels - levels of feature pyramid
// kPoints - number of foot filter positions
// color - line color for each box
// thickness - line thickness
// line_type - line type
// shift - shift
// OUTPUT
// window contained initial image and filter boxes
// RESULT
// Error status
*/
int showPartFilterBoxes(IplImage *image,
const CvLSVMFilterObject **filters,
int n, CvPoint **partsDisplacement,
int *levels, int kPoints,
CvScalar color, int thickness,
int line_type, int shift)
{
int i, j;
float step;
CvPoint oppositePoint;
step = powf( 2.0f, 1.0f / ((float)LAMBDA));
for (i = 0; i < kPoints; i++)
{
for (j = 0; j < n; j++)
{
// Drawing rectangles for part filters
getOppositePoint(partsDisplacement[i][j],
filters[j + 1]->sizeX, filters[j + 1]->sizeY,
step, levels[i] - 2 * LAMBDA, &oppositePoint);
cvRectangle(image, partsDisplacement[i][j], oppositePoint,
color, thickness, line_type, shift);
}
}
#ifdef HAVE_OPENCV_HIGHGUI
cv::imshow("Initial image", cv::cvarrToMat(image));
#endif
return LATENT_SVM_OK;
}
/*
// Drawing boxes
//
// API
// int showBoxes(const IplImage *img,
const CvPoint *points, const CvPoint *oppositePoints, int kPoints,
CvScalar color, int thickness, int line_type, int shift);
// INPUT
// img - initial image
// points - top left corner coordinates
// oppositePoints - right bottom corner coordinates
// kPoints - points number
// color - line color for each box
// thickness - line thickness
// line_type - line type
// shift - shift
// OUTPUT
// RESULT
// Error status
*/
int showBoxes(IplImage *img,
const CvPoint *points, const CvPoint *oppositePoints, int kPoints,
CvScalar color, int thickness, int line_type, int shift)
{
int i;
for (i = 0; i < kPoints; i++)
{
cvRectangle(img, points[i], oppositePoints[i],
color, thickness, line_type, shift);
}
#ifdef HAVE_OPENCV_HIGHGUI
cv::imshow("Initial image", cv::cvarrToMat(img));
#endif
return LATENT_SVM_OK;
}
/*
// Computation maximum filter size for each dimension
//
// API
// int getMaxFilterDims(const filterObject **filters, int kComponents,
const int *kPartFilters,
unsigned int *maxXBorder, unsigned int *maxYBorder);
// INPUT
// filters - a set of filters (at first root filter, then part filters
and etc. for all components)
// kComponents - number of components
// kPartFilters - number of part filters for each component
// OUTPUT
// maxXBorder - maximum of filter size at the horizontal dimension
// maxYBorder - maximum of filter size at the vertical dimension
// RESULT
// Error status
*/
int getMaxFilterDims(const CvLSVMFilterObject **filters, int kComponents,
const int *kPartFilters,
unsigned int *maxXBorder, unsigned int *maxYBorder)
{
int i, componentIndex;
*maxXBorder = filters[0]->sizeX;
*maxYBorder = filters[0]->sizeY;
componentIndex = kPartFilters[0] + 1;
for (i = 1; i < kComponents; i++)
{
if ((unsigned)filters[componentIndex]->sizeX > *maxXBorder)
{
*maxXBorder = filters[componentIndex]->sizeX;
}
if ((unsigned)filters[componentIndex]->sizeY > *maxYBorder)
{
*maxYBorder = filters[componentIndex]->sizeY;
}
componentIndex += (kPartFilters[i] + 1);
}
return LATENT_SVM_OK;
}
/*
// Computation root filters displacement and values of score function
//
// API
// int searchObjectThresholdSomeComponents(const featurePyramid *H,
const filterObject **filters,
int kComponents, const int *kPartFilters,
const float *b, float scoreThreshold,
CvPoint **points, CvPoint **oppPoints,
float **score, int *kPoints);
// INPUT
// H - feature pyramid
// filters - filters (root filter then it's part filters, etc.)
// kComponents - root filters number
// kPartFilters - array of part filters number for each component
// b - array of linear terms
// scoreThreshold - score threshold
// OUTPUT
// points - root filters displacement (top left corners)
// oppPoints - root filters displacement (bottom right corners)
// score - array of score values
// kPoints - number of boxes
// RESULT
// Error status
*/
int searchObjectThresholdSomeComponents(const CvLSVMFeaturePyramid *H,
const CvLSVMFilterObject **filters,
int kComponents, const int *kPartFilters,
const float *b, float scoreThreshold,
CvPoint **points, CvPoint **oppPoints,
float **score, int *kPoints,
int numThreads)
{
//int error = 0;
int i, j, s, f, componentIndex;
unsigned int maxXBorder, maxYBorder;
CvPoint **pointsArr, **oppPointsArr, ***partsDisplacementArr;
float **scoreArr;
int *kPointsArr, **levelsArr;
// Allocation memory
pointsArr = (CvPoint **)malloc(sizeof(CvPoint *) * kComponents);
oppPointsArr = (CvPoint **)malloc(sizeof(CvPoint *) * kComponents);
scoreArr = (float **)malloc(sizeof(float *) * kComponents);
kPointsArr = (int *)malloc(sizeof(int) * kComponents);
levelsArr = (int **)malloc(sizeof(int *) * kComponents);
partsDisplacementArr = (CvPoint ***)malloc(sizeof(CvPoint **) * kComponents);
// Getting maximum filter dimensions
/*error = */getMaxFilterDims(filters, kComponents, kPartFilters, &maxXBorder, &maxYBorder);
componentIndex = 0;
*kPoints = 0;
// For each component perform searching
for (i = 0; i < kComponents; i++)
{
int error = searchObjectThreshold(H, &(filters[componentIndex]), kPartFilters[i],
b[i], maxXBorder, maxYBorder, scoreThreshold,
&(pointsArr[i]), &(levelsArr[i]), &(kPointsArr[i]),
&(scoreArr[i]), &(partsDisplacementArr[i]), numThreads);
if (error != LATENT_SVM_OK)
{
// Release allocated memory
free(pointsArr);
free(oppPointsArr);
free(scoreArr);
free(kPointsArr);
free(levelsArr);
free(partsDisplacementArr);
return LATENT_SVM_SEARCH_OBJECT_FAILED;
}
estimateBoxes(pointsArr[i], levelsArr[i], kPointsArr[i],
filters[componentIndex]->sizeX, filters[componentIndex]->sizeY, &(oppPointsArr[i]));
componentIndex += (kPartFilters[i] + 1);
*kPoints += kPointsArr[i];
}
*points = (CvPoint *)malloc(sizeof(CvPoint) * (*kPoints));
*oppPoints = (CvPoint *)malloc(sizeof(CvPoint) * (*kPoints));
*score = (float *)malloc(sizeof(float) * (*kPoints));
s = 0;
for (i = 0; i < kComponents; i++)
{
f = s + kPointsArr[i];
for (j = s; j < f; j++)
{
(*points)[j].x = pointsArr[i][j - s].x;
(*points)[j].y = pointsArr[i][j - s].y;
(*oppPoints)[j].x = oppPointsArr[i][j - s].x;
(*oppPoints)[j].y = oppPointsArr[i][j - s].y;
(*score)[j] = scoreArr[i][j - s];
}
s = f;
}
// Release allocated memory
for (i = 0; i < kComponents; i++)
{
free(pointsArr[i]);
free(oppPointsArr[i]);
free(scoreArr[i]);
free(levelsArr[i]);
for (j = 0; j < kPointsArr[i]; j++)
{
free(partsDisplacementArr[i][j]);
}
free(partsDisplacementArr[i]);
}
free(pointsArr);
free(oppPointsArr);
free(scoreArr);
free(kPointsArr);
free(levelsArr);
free(partsDisplacementArr);
return LATENT_SVM_OK;
}

View File

@@ -1,271 +0,0 @@
#include "precomp.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/objdetect/objdetect_c.h"
#include "_lsvmparser.h"
#include "_lsvm_matching.h"
/*
// load trained detector from a file
//
// API
// CvLatentSvmDetector* cvLoadLatentSvmDetector(const char* filename);
// INPUT
// filename - path to the file containing the parameters of
// - trained Latent SVM detector
// OUTPUT
// trained Latent SVM detector in internal representation
*/
CvLatentSvmDetector* cvLoadLatentSvmDetector(const char* filename)
{
CvLatentSvmDetector* detector = 0;
CvLSVMFilterObject** filters = 0;
int kFilters = 0;
int kComponents = 0;
int* kPartFilters = 0;
float* b = 0;
float scoreThreshold = 0.f;
int err_code = 0;
err_code = loadModel(filename, &filters, &kFilters, &kComponents, &kPartFilters, &b, &scoreThreshold);
if (err_code != LATENT_SVM_OK) return 0;
detector = (CvLatentSvmDetector*)malloc(sizeof(CvLatentSvmDetector));
detector->filters = filters;
detector->b = b;
detector->num_components = kComponents;
detector->num_filters = kFilters;
detector->num_part_filters = kPartFilters;
detector->score_threshold = scoreThreshold;
return detector;
}
/*
// release memory allocated for CvLatentSvmDetector structure
//
// API
// void cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector);
// INPUT
// detector - CvLatentSvmDetector structure to be released
// OUTPUT
*/
void cvReleaseLatentSvmDetector(CvLatentSvmDetector** detector)
{
free((*detector)->b);
free((*detector)->num_part_filters);
for (int i = 0; i < (*detector)->num_filters; i++)
{
free((*detector)->filters[i]->H);
free((*detector)->filters[i]);
}
free((*detector)->filters);
free((*detector));
*detector = 0;
}
/*
// find rectangular regions in the given image that are likely
// to contain objects and corresponding confidence levels
//
// API
// CvSeq* cvLatentSvmDetectObjects(const IplImage* image,
// CvLatentSvmDetector* detector,
// CvMemStorage* storage,
// float overlap_threshold = 0.5f,
int numThreads = -1);
// INPUT
// image - image to detect objects in
// detector - Latent SVM detector in internal representation
// storage - memory storage to store the resultant sequence
// of the object candidate rectangles
// overlap_threshold - threshold for the non-maximum suppression algorithm [here will be the reference to original paper]
// OUTPUT
// sequence of detected objects (bounding boxes and confidence levels stored in CvObjectDetection structures)
*/
CvSeq* cvLatentSvmDetectObjects(IplImage* image,
CvLatentSvmDetector* detector,
CvMemStorage* storage,
float overlap_threshold, int numThreads)
{
CvLSVMFeaturePyramid *H = 0;
CvPoint *points = 0, *oppPoints = 0;
int kPoints = 0;
float *score = 0;
unsigned int maxXBorder = 0, maxYBorder = 0;
int numBoxesOut = 0;
CvPoint *pointsOut = 0;
CvPoint *oppPointsOut = 0;
float *scoreOut = 0;
CvSeq* result_seq = 0;
int error = 0;
if(image->nChannels == 3)
cvCvtColor(image, image, CV_BGR2RGB);
// Getting maximum filter dimensions
getMaxFilterDims((const CvLSVMFilterObject**)(detector->filters), detector->num_components,
detector->num_part_filters, &maxXBorder, &maxYBorder);
// Create feature pyramid with nullable border
H = createFeaturePyramidWithBorder(image, maxXBorder, maxYBorder);
// Search object
error = searchObjectThresholdSomeComponents(H, (const CvLSVMFilterObject**)(detector->filters),
detector->num_components, detector->num_part_filters, detector->b, detector->score_threshold,
&points, &oppPoints, &score, &kPoints, numThreads);
if (error != LATENT_SVM_OK)
{
return NULL;
}
// Clipping boxes
clippingBoxes(image->width, image->height, points, kPoints);
clippingBoxes(image->width, image->height, oppPoints, kPoints);
// NMS procedure
nonMaximumSuppression(kPoints, points, oppPoints, score, overlap_threshold,
&numBoxesOut, &pointsOut, &oppPointsOut, &scoreOut);
result_seq = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvObjectDetection), storage );
for (int i = 0; i < numBoxesOut; i++)
{
CvObjectDetection detection = {CvRect(), 0};
detection.score = scoreOut[i];
CvRect bounding_box;
bounding_box.x = pointsOut[i].x;
bounding_box.y = pointsOut[i].y;
bounding_box.width = oppPointsOut[i].x - pointsOut[i].x;
bounding_box.height = oppPointsOut[i].y - pointsOut[i].y;
detection.rect = bounding_box;
cvSeqPush(result_seq, &detection);
}
if(image->nChannels == 3)
cvCvtColor(image, image, CV_RGB2BGR);
freeFeaturePyramidObject(&H);
free(points);
free(oppPoints);
free(score);
free(scoreOut);
return result_seq;
}
namespace cv
{
LatentSvmDetector::ObjectDetection::ObjectDetection() : score(0.f), classID(-1)
{}
LatentSvmDetector::ObjectDetection::ObjectDetection( const Rect& _rect, float _score, int _classID ) :
rect(_rect), score(_score), classID(_classID)
{}
LatentSvmDetector::LatentSvmDetector()
{}
LatentSvmDetector::LatentSvmDetector( const std::vector<String>& filenames, const std::vector<String>& _classNames )
{
load( filenames, _classNames );
}
LatentSvmDetector::~LatentSvmDetector()
{
clear();
}
void LatentSvmDetector::clear()
{
for( size_t i = 0; i < detectors.size(); i++ )
cvReleaseLatentSvmDetector( &detectors[i] );
detectors.clear();
classNames.clear();
}
bool LatentSvmDetector::empty() const
{
return detectors.empty();
}
const std::vector<String>& LatentSvmDetector::getClassNames() const
{
return classNames;
}
size_t LatentSvmDetector::getClassCount() const
{
return classNames.size();
}
static String extractModelName( const String& filename )
{
size_t startPos = filename.rfind('/');
if( startPos == String::npos )
startPos = filename.rfind('\\');
if( startPos == String::npos )
startPos = 0;
else
startPos++;
const int extentionSize = 4; //.xml
int substrLength = (int)(filename.size() - startPos - extentionSize);
return filename.substr(startPos, substrLength);
}
bool LatentSvmDetector::load( const std::vector<String>& filenames, const std::vector<String>& _classNames )
{
clear();
CV_Assert( _classNames.empty() || _classNames.size() == filenames.size() );
for( size_t i = 0; i < filenames.size(); i++ )
{
const String filename = filenames[i];
if( filename.length() < 5 || filename.substr(filename.length()-4, 4) != ".xml" )
continue;
CvLatentSvmDetector* detector = cvLoadLatentSvmDetector( filename.c_str() );
if( detector )
{
detectors.push_back( detector );
if( _classNames.empty() )
{
classNames.push_back( extractModelName(filenames[i]) );
}
else
classNames.push_back( _classNames[i] );
}
}
return !empty();
}
void LatentSvmDetector::detect( const Mat& image,
std::vector<ObjectDetection>& objectDetections,
float overlapThreshold,
int numThreads )
{
objectDetections.clear();
if( numThreads <= 0 )
numThreads = 1;
for( size_t classID = 0; classID < detectors.size(); classID++ )
{
IplImage image_ipl = image;
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* detections = cvLatentSvmDetectObjects( &image_ipl, detectors[classID], storage, overlapThreshold, numThreads );
// convert results
objectDetections.reserve( objectDetections.size() + detections->total );
for( int detectionIdx = 0; detectionIdx < detections->total; detectionIdx++ )
{
CvObjectDetection detection = *(CvObjectDetection*)cvGetSeqElem( detections, detectionIdx );
objectDetections.push_back( ObjectDetection(Rect(detection.rect), detection.score, (int)classID) );
}
cvReleaseMemStorage( &storage );
}
}
} // namespace cv

View File

@@ -1,817 +0,0 @@
#include "precomp.hpp"
#include <stdio.h>
#include "string.h"
#include "_lsvmparser.h"
#include "_lsvm_error.h"
namespace
{
int isMODEL (char *str){
char stag [] = "<Model>";
char etag [] = "</Model>";
if(strcmp(stag, str) == 0)return MODEL;
if(strcmp(etag, str) == 0)return EMODEL;
return 0;
}
int isP (char *str){
char stag [] = "<P>";
char etag [] = "</P>";
if(strcmp(stag, str) == 0)return P;
if(strcmp(etag, str) == 0)return EP;
return 0;
}
int isSCORE (char *str){
char stag [] = "<ScoreThreshold>";
char etag [] = "</ScoreThreshold>";
if(strcmp(stag, str) == 0)return SCORE;
if(strcmp(etag, str) == 0)return ESCORE;
return 0;
}
int isCOMP (char *str){
char stag [] = "<Component>";
char etag [] = "</Component>";
if(strcmp(stag, str) == 0)return COMP;
if(strcmp(etag, str) == 0)return ECOMP;
return 0;
}
int isRFILTER (char *str){
char stag [] = "<RootFilter>";
char etag [] = "</RootFilter>";
if(strcmp(stag, str) == 0)return RFILTER;
if(strcmp(etag, str) == 0)return ERFILTER;
return 0;
}
int isPFILTERs (char *str){
char stag [] = "<PartFilters>";
char etag [] = "</PartFilters>";
if(strcmp(stag, str) == 0)return PFILTERs;
if(strcmp(etag, str) == 0)return EPFILTERs;
return 0;
}
int isPFILTER (char *str){
char stag [] = "<PartFilter>";
char etag [] = "</PartFilter>";
if(strcmp(stag, str) == 0)return PFILTER;
if(strcmp(etag, str) == 0)return EPFILTER;
return 0;
}
int isSIZEX (char *str){
char stag [] = "<sizeX>";
char etag [] = "</sizeX>";
if(strcmp(stag, str) == 0)return SIZEX;
if(strcmp(etag, str) == 0)return ESIZEX;
return 0;
}
int isSIZEY (char *str){
char stag [] = "<sizeY>";
char etag [] = "</sizeY>";
if(strcmp(stag, str) == 0)return SIZEY;
if(strcmp(etag, str) == 0)return ESIZEY;
return 0;
}
int isWEIGHTS (char *str){
char stag [] = "<Weights>";
char etag [] = "</Weights>";
if(strcmp(stag, str) == 0)return WEIGHTS;
if(strcmp(etag, str) == 0)return EWEIGHTS;
return 0;
}
int isV (char *str){
char stag [] = "<V>";
char etag [] = "</V>";
if(strcmp(stag, str) == 0)return TAGV;
if(strcmp(etag, str) == 0)return ETAGV;
return 0;
}
int isVx (char *str){
char stag [] = "<Vx>";
char etag [] = "</Vx>";
if(strcmp(stag, str) == 0)return Vx;
if(strcmp(etag, str) == 0)return EVx;
return 0;
}
int isVy (char *str){
char stag [] = "<Vy>";
char etag [] = "</Vy>";
if(strcmp(stag, str) == 0)return Vy;
if(strcmp(etag, str) == 0)return EVy;
return 0;
}
int isD (char *str){
char stag [] = "<Penalty>";
char etag [] = "</Penalty>";
if(strcmp(stag, str) == 0)return TAGD;
if(strcmp(etag, str) == 0)return ETAGD;
return 0;
}
int isDx (char *str){
char stag [] = "<dx>";
char etag [] = "</dx>";
if(strcmp(stag, str) == 0)return Dx;
if(strcmp(etag, str) == 0)return EDx;
return 0;
}
int isDy (char *str){
char stag [] = "<dy>";
char etag [] = "</dy>";
if(strcmp(stag, str) == 0)return Dy;
if(strcmp(etag, str) == 0)return EDy;
return 0;
}
int isDxx (char *str){
char stag [] = "<dxx>";
char etag [] = "</dxx>";
if(strcmp(stag, str) == 0)return Dxx;
if(strcmp(etag, str) == 0)return EDxx;
return 0;
}
int isDyy (char *str){
char stag [] = "<dyy>";
char etag [] = "</dyy>";
if(strcmp(stag, str) == 0)return Dyy;
if(strcmp(etag, str) == 0)return EDyy;
return 0;
}
int isB (char *str){
char stag [] = "<LinearTerm>";
char etag [] = "</LinearTerm>";
if(strcmp(stag, str) == 0)return BTAG;
if(strcmp(etag, str) == 0)return EBTAG;
return 0;
}
int getTeg(char *str){
int sum = 0;
sum = isMODEL (str)+
isP (str)+
isSCORE (str)+
isCOMP (str)+
isRFILTER (str)+
isPFILTERs (str)+
isPFILTER (str)+
isSIZEX (str)+
isSIZEY (str)+
isWEIGHTS (str)+
isV (str)+
isVx (str)+
isVy (str)+
isD (str)+
isDx (str)+
isDy (str)+
isDxx (str)+
isDyy (str)+
isB (str);
return sum;
}
void addFilter(CvLSVMFilterObject *** model, int *last, int *max){
CvLSVMFilterObject ** nmodel;
int i;
(*last) ++;
if((*last) >= (*max)){
(*max) += 10;
nmodel = (CvLSVMFilterObject **)malloc(sizeof(CvLSVMFilterObject *) * (*max));
for(i = 0; i < *last; i++){
nmodel[i] = (* model)[i];
}
free(* model);
(*model) = nmodel;
}
(*model) [(*last)] = (CvLSVMFilterObject *)malloc(sizeof(CvLSVMFilterObject));
}
void parserRFilter (FILE * xmlf, int p, CvLSVMFilterObject * model, float *b){
int st = 0;
int sizeX=0, sizeY=0;
int tag;
int tagVal;
char ch;
int i,j,ii;
char buf[1024];
char tagBuf[1024];
double *data;
//printf("<RootFilter>\n");
model->V.x = 0;
model->V.y = 0;
model->V.l = 0;
model->fineFunction[0] = 0.0;
model->fineFunction[1] = 0.0;
model->fineFunction[2] = 0.0;
model->fineFunction[3] = 0.0;
i = 0;
j = 0;
st = 0;
tag = 0;
while(!feof(xmlf)){
ch = (char)fgetc( xmlf );
if(ch == '<'){
tag = 1;
j = 1;
tagBuf[j - 1] = ch;
}else {
if(ch == '>'){
tagBuf[j ] = ch;
tagBuf[j + 1] = '\0';
tagVal = getTeg(tagBuf);
if(tagVal == ERFILTER){
//printf("</RootFilter>\n");
return;
}
if(tagVal == SIZEX){
st = 1;
i = 0;
}
if(tagVal == ESIZEX){
st = 0;
buf[i] = '\0';
sizeX = atoi(buf);
model->sizeX = sizeX;
//printf("<sizeX>%d</sizeX>\n", sizeX);
}
if(tagVal == SIZEY){
st = 1;
i = 0;
}
if(tagVal == ESIZEY){
st = 0;
buf[i] = '\0';
sizeY = atoi(buf);
model->sizeY = sizeY;
//printf("<sizeY>%d</sizeY>\n", sizeY);
}
if(tagVal == WEIGHTS){
data = (double *)malloc( sizeof(double) * p * sizeX * sizeY);
size_t elements_read = fread(data, sizeof(double), p * sizeX * sizeY, xmlf);
CV_Assert(elements_read == (size_t)(p * sizeX * sizeY));
model->H = (float *)malloc(sizeof(float)* p * sizeX * sizeY);
for(ii = 0; ii < p * sizeX * sizeY; ii++){
model->H[ii] = (float)data[ii];
}
free(data);
}
if(tagVal == EWEIGHTS){
//printf("WEIGHTS OK\n");
}
if(tagVal == BTAG){
st = 1;
i = 0;
}
if(tagVal == EBTAG){
st = 0;
buf[i] = '\0';
*b =(float) atof(buf);
//printf("<B>%f</B>\n", *b);
}
tag = 0;
i = 0;
}else{
if((tag == 0)&& (st == 1)){
buf[i] = ch; i++;
}else{
tagBuf[j] = ch; j++;
}
}
}
}
}
void parserV (FILE * xmlf, int /*p*/, CvLSVMFilterObject * model){
int st = 0;
int tag;
int tagVal;
char ch;
int i,j;
char buf[1024];
char tagBuf[1024];
//printf(" <V>\n");
i = 0;
j = 0;
st = 0;
tag = 0;
while(!feof(xmlf)){
ch = (char)fgetc( xmlf );
if(ch == '<'){
tag = 1;
j = 1;
tagBuf[j - 1] = ch;
}else {
if(ch == '>'){
tagBuf[j ] = ch;
tagBuf[j + 1] = '\0';
tagVal = getTeg(tagBuf);
if(tagVal == ETAGV){
//printf(" </V>\n");
return;
}
if(tagVal == Vx){
st = 1;
i = 0;
}
if(tagVal == EVx){
st = 0;
buf[i] = '\0';
model->V.x = atoi(buf);
//printf(" <Vx>%d</Vx>\n", model->V.x);
}
if(tagVal == Vy){
st = 1;
i = 0;
}
if(tagVal == EVy){
st = 0;
buf[i] = '\0';
model->V.y = atoi(buf);
//printf(" <Vy>%d</Vy>\n", model->V.y);
}
tag = 0;
i = 0;
}else{
if((tag == 0)&& (st == 1)){
buf[i] = ch; i++;
}else{
tagBuf[j] = ch; j++;
}
}
}
}
}
void parserD (FILE * xmlf, int /*p*/, CvLSVMFilterObject * model){
int st = 0;
int tag;
int tagVal;
char ch;
int i,j;
char buf[1024];
char tagBuf[1024];
//printf(" <D>\n");
i = 0;
j = 0;
st = 0;
tag = 0;
while(!feof(xmlf)){
ch = (char)fgetc( xmlf );
if(ch == '<'){
tag = 1;
j = 1;
tagBuf[j - 1] = ch;
}else {
if(ch == '>'){
tagBuf[j ] = ch;
tagBuf[j + 1] = '\0';
tagVal = getTeg(tagBuf);
if(tagVal == ETAGD){
//printf(" </D>\n");
return;
}
if(tagVal == Dx){
st = 1;
i = 0;
}
if(tagVal == EDx){
st = 0;
buf[i] = '\0';
model->fineFunction[0] = (float)atof(buf);
//printf(" <Dx>%f</Dx>\n", model->fineFunction[0]);
}
if(tagVal == Dy){
st = 1;
i = 0;
}
if(tagVal == EDy){
st = 0;
buf[i] = '\0';
model->fineFunction[1] = (float)atof(buf);
//printf(" <Dy>%f</Dy>\n", model->fineFunction[1]);
}
if(tagVal == Dxx){
st = 1;
i = 0;
}
if(tagVal == EDxx){
st = 0;
buf[i] = '\0';
model->fineFunction[2] = (float)atof(buf);
//printf(" <Dxx>%f</Dxx>\n", model->fineFunction[2]);
}
if(tagVal == Dyy){
st = 1;
i = 0;
}
if(tagVal == EDyy){
st = 0;
buf[i] = '\0';
model->fineFunction[3] = (float)atof(buf);
//printf(" <Dyy>%f</Dyy>\n", model->fineFunction[3]);
}
tag = 0;
i = 0;
}else{
if((tag == 0)&& (st == 1)){
buf[i] = ch; i++;
}else{
tagBuf[j] = ch; j++;
}
}
}
}
}
void parserPFilter (FILE * xmlf, int p, int /*N_path*/, CvLSVMFilterObject * model){
int st = 0;
int sizeX=0, sizeY=0;
int tag;
int tagVal;
char ch;
int i,j, ii;
char buf[1024];
char tagBuf[1024];
double *data;
//printf("<PathFilter> (%d)\n", N_path);
model->V.x = 0;
model->V.y = 0;
model->V.l = 0;
model->fineFunction[0] = 0.0f;
model->fineFunction[1] = 0.0f;
model->fineFunction[2] = 0.0f;
model->fineFunction[3] = 0.0f;
i = 0;
j = 0;
st = 0;
tag = 0;
while(!feof(xmlf)){
ch = (char)fgetc( xmlf );
if(ch == '<'){
tag = 1;
j = 1;
tagBuf[j - 1] = ch;
}else {
if(ch == '>'){
tagBuf[j ] = ch;
tagBuf[j + 1] = '\0';
tagVal = getTeg(tagBuf);
if(tagVal == EPFILTER){
//printf("</PathFilter>\n");
return;
}
if(tagVal == TAGV){
parserV(xmlf, p, model);
}
if(tagVal == TAGD){
parserD(xmlf, p, model);
}
if(tagVal == SIZEX){
st = 1;
i = 0;
}
if(tagVal == ESIZEX){
st = 0;
buf[i] = '\0';
sizeX = atoi(buf);
model->sizeX = sizeX;
//printf("<sizeX>%d</sizeX>\n", sizeX);
}
if(tagVal == SIZEY){
st = 1;
i = 0;
}
if(tagVal == ESIZEY){
st = 0;
buf[i] = '\0';
sizeY = atoi(buf);
model->sizeY = sizeY;
//printf("<sizeY>%d</sizeY>\n", sizeY);
}
if(tagVal == WEIGHTS){
data = (double *)malloc( sizeof(double) * p * sizeX * sizeY);
size_t elements_read = fread(data, sizeof(double), p * sizeX * sizeY, xmlf);
CV_Assert(elements_read == (size_t)(p * sizeX * sizeY));
model->H = (float *)malloc(sizeof(float)* p * sizeX * sizeY);
for(ii = 0; ii < p * sizeX * sizeY; ii++){
model->H[ii] = (float)data[ii];
}
free(data);
}
if(tagVal == EWEIGHTS){
//printf("WEIGHTS OK\n");
}
tag = 0;
i = 0;
}else{
if((tag == 0)&& (st == 1)){
buf[i] = ch; i++;
}else{
tagBuf[j] = ch; j++;
}
}
}
}
}
void parserPFilterS (FILE * xmlf, int p, CvLSVMFilterObject *** model, int *last, int *max){
int st = 0;
int N_path = 0;
int tag;
int tagVal;
char ch;
int /*i,*/j;
//char buf[1024];
char tagBuf[1024];
//printf("<PartFilters>\n");
//i = 0;
j = 0;
st = 0;
tag = 0;
while(!feof(xmlf)){
ch = (char)fgetc( xmlf );
if(ch == '<'){
tag = 1;
j = 1;
tagBuf[j - 1] = ch;
}else {
if(ch == '>'){
tagBuf[j ] = ch;
tagBuf[j + 1] = '\0';
tagVal = getTeg(tagBuf);
if(tagVal == EPFILTERs){
//printf("</PartFilters>\n");
return;
}
if(tagVal == PFILTER){
addFilter(model, last, max);
parserPFilter (xmlf, p, N_path, (*model)[*last]);
N_path++;
}
tag = 0;
//i = 0;
}else{
if((tag == 0)&& (st == 1)){
//buf[i] = ch; i++;
}else{
tagBuf[j] = ch; j++;
}
}
}
}
}
void parserComp (FILE * xmlf, int p, int *N_comp, CvLSVMFilterObject *** model, float *b, int *last, int *max){
int st = 0;
int tag;
int tagVal;
char ch;
int /*i,*/j;
//char buf[1024];
char tagBuf[1024];
//printf("<Component> %d\n", *N_comp);
//i = 0;
j = 0;
st = 0;
tag = 0;
while(!feof(xmlf)){
ch = (char)fgetc( xmlf );
if(ch == '<'){
tag = 1;
j = 1;
tagBuf[j - 1] = ch;
}else {
if(ch == '>'){
tagBuf[j ] = ch;
tagBuf[j + 1] = '\0';
tagVal = getTeg(tagBuf);
if(tagVal == ECOMP){
(*N_comp) ++;
return;
}
if(tagVal == RFILTER){
addFilter(model, last, max);
parserRFilter (xmlf, p, (*model)[*last],b);
}
if(tagVal == PFILTERs){
parserPFilterS (xmlf, p, model, last, max);
}
tag = 0;
//i = 0;
}else{
if((tag == 0)&& (st == 1)){
//buf[i] = ch; i++;
}else{
tagBuf[j] = ch; j++;
}
}
}
}
}
void parserModel(FILE * xmlf, CvLSVMFilterObject *** model, int *last, int *max, int **comp, float **b, int *count, float * score){
int p = 0;
int N_comp = 0;
int * cmp;
float *bb;
int st = 0;
int tag;
int tagVal;
char ch;
int i,j, ii = 0;
char buf[1024];
char tagBuf[1024];
//printf("<Model>\n");
i = 0;
j = 0;
st = 0;
tag = 0;
while(!feof(xmlf)){
ch = (char)fgetc( xmlf );
if(ch == '<'){
tag = 1;
j = 1;
tagBuf[j - 1] = ch;
}else {
if(ch == '>'){
tagBuf[j ] = ch;
tagBuf[j + 1] = '\0';
tagVal = getTeg(tagBuf);
if(tagVal == EMODEL){
//printf("</Model>\n");
for(ii = 0; ii <= *last; ii++){
(*model)[ii]->numFeatures = p;
}
* count = N_comp;
return;
}
if(tagVal == COMP){
if(N_comp == 0){
cmp = (int *)malloc(sizeof(int));
bb = (float *)malloc(sizeof(float));
* comp = cmp;
* b = bb;
* count = N_comp + 1;
} else {
cmp = (int *)malloc(sizeof(int) * (N_comp + 1));
bb = (float *)malloc(sizeof(float) * (N_comp + 1));
for(ii = 0; ii < N_comp; ii++){
cmp[ii] = (* comp)[ii];
bb [ii] = (* b )[ii];
}
free(* comp);
free(* b );
* comp = cmp;
* b = bb;
* count = N_comp + 1;
}
parserComp(xmlf, p, &N_comp, model, &((*b)[N_comp]), last, max);
cmp[N_comp - 1] = *last;
}
if(tagVal == P){
st = 1;
i = 0;
}
if(tagVal == EP){
st = 0;
buf[i] = '\0';
p = atoi(buf);
//printf("<P>%d</P>\n", p);
}
if(tagVal == SCORE){
st = 1;
i = 0;
}
if(tagVal == ESCORE){
st = 0;
buf[i] = '\0';
*score = (float)atof(buf);
//printf("<ScoreThreshold>%f</ScoreThreshold>\n", score);
}
tag = 0;
i = 0;
}else{
if((tag == 0)&& (st == 1)){
buf[i] = ch; i++;
}else{
tagBuf[j] = ch; j++;
}
}
}
}
}
}//namespace
int LSVMparser(const char * filename, CvLSVMFilterObject *** model, int *last, int *max, int **comp, float **b, int *count, float * score){
//int st = 0;
int tag;
char ch;
int /*i,*/j;
FILE *xmlf;
//char buf[1024];
char tagBuf[1024];
(*max) = 10;
(*last) = -1;
(*model) = (CvLSVMFilterObject ** )malloc((sizeof(CvLSVMFilterObject * )) * (*max));
//printf("parse : %s\n", filename);
xmlf = fopen(filename, "rb");
if(xmlf == NULL) {
free(*model);
*model = NULL;
return LSVM_PARSER_FILE_NOT_FOUND;
}
//i = 0;
j = 0;
//st = 0;
tag = 0;
while(!feof(xmlf)){
ch = (char)fgetc( xmlf );
if(ch == '<'){
tag = 1;
j = 1;
tagBuf[j - 1] = ch;
}else {
if(ch == '>'){
tag = 0;
//i = 0;
tagBuf[j ] = ch;
tagBuf[j + 1] = '\0';
if(getTeg(tagBuf) == MODEL){
parserModel(xmlf, model, last, max, comp, b, count, score);
}
}else{
if(tag == 0){
//buf[i] = ch; i++;
}else{
tagBuf[j] = ch; j++;
}
}
}
}
fclose(xmlf);
return LATENT_SVM_OK;
}
int loadModel(
const char *modelPath,
CvLSVMFilterObject ***filters,
int *kFilters,
int *kComponents,
int **kPartFilters,
float **b,
float *scoreThreshold){
int last;
int max;
int *comp = NULL;
int count;
int i;
int err;
float score;
//printf("start_parse\n\n");
err = LSVMparser(modelPath, filters, &last, &max, &comp, b, &count, &score);
if(err != LATENT_SVM_OK){
return err;
}
(*kFilters) = last + 1;
(*kComponents) = count;
(*scoreThreshold) = (float) score;
(*kPartFilters) = (int *)malloc(sizeof(int) * count);
for(i = 1; i < count;i++){
(*kPartFilters)[i] = (comp[i] - comp[i - 1]) - 1;
}
(*kPartFilters)[0] = comp[0];
free(comp);
return 0;
}

View File

@@ -1,122 +0,0 @@
#include "precomp.hpp"
#ifdef HAVE_TBB
#include "_lsvm_tbbversion.h"
/*
// Task class
*/
class ScoreComputation : public tbb::task
{
private:
const CvLSVMFilterObject **filters;
const int n;
const CvLSVMFeaturePyramid *H;
const float b;
const int maxXBorder;
const int maxYBorder;
const float scoreThreshold;
const int kLevels;
const int *procLevels;
public:
float **score;
CvPoint ***points;
CvPoint ****partsDisplacement;
int *kPoints;
public:
ScoreComputation(const CvLSVMFilterObject **_filters, int _n,
const CvLSVMFeaturePyramid *_H,
float _b, int _maxXBorder, int _maxYBorder,
float _scoreThreshold, int _kLevels, const int *_procLevels,
float **_score, CvPoint ***_points, int *_kPoints,
CvPoint ****_partsDisplacement) :
n(_n), b(_b), maxXBorder(_maxXBorder),
maxYBorder(_maxYBorder), scoreThreshold(_scoreThreshold),
kLevels(_kLevels), score(_score), points(_points),
partsDisplacement(_partsDisplacement), kPoints(_kPoints)
{
filters = _filters;
H = _H;
procLevels = _procLevels;
};
task* execute()
{
int i, level, partsLevel, res;
for (i = 0; i < kLevels; i++)
{
level = procLevels[i];
partsLevel = level - LAMBDA;//H->lambda;
res = thresholdFunctionalScoreFixedLevel(
filters, n, H, level, b,
maxXBorder, maxYBorder, scoreThreshold, &(score[partsLevel]),
points[partsLevel], &(kPoints[partsLevel]),
partsDisplacement[partsLevel]);
if (res != LATENT_SVM_OK)
{
continue;
}
}
return NULL;
}
};
/*
// Computation score function using TBB tasks
//
// API
// int tbbTasksThresholdFunctionalScore(const CvLSVMFilterObject **filters, const int n,
const CvLSVMFeatureMap *H, const float b,
const int maxXBorder, const int maxYBorder,
const float scoreThreshold,
int *kLevels, int **procLevels,
const int threadsNum,
float **score, CvPoint ***points,
int *kPoints,
CvPoint ****partsDisplacement);
// INPUT
// filters - the set of filters (the first element is root filter,
the other - part filters)
// n - the number of part filters
// H - feature pyramid
// b - linear term of the score function
// maxXBorder - the largest root filter size (X-direction)
// maxYBorder - the largest root filter size (Y-direction)
// scoreThreshold - score threshold
// kLevels - array that contains number of levels processed
by each thread
// procLevels - array that contains lists of levels processed
by each thread
// threadsNum - the number of created threads
// OUTPUT
// score - score function values that exceed threshold
// points - the set of root filter positions (in the block space)
// kPoints - number of root filter positions
// partsDisplacement - displacement of part filters (in the block space)
// RESULT
//
*/
int tbbTasksThresholdFunctionalScore(const CvLSVMFilterObject **filters, const int n,
const CvLSVMFeaturePyramid *H, const float b,
const int maxXBorder, const int maxYBorder,
const float scoreThreshold,
int *kLevels, int **procLevels,
const int threadsNum,
float **score, CvPoint ***points,
int *kPoints,
CvPoint ****partsDisplacement)
{
tbb::task_list tasks;
int i;
for (i = 0; i < threadsNum; i++)
{
ScoreComputation& sc =
*new(tbb::task::allocate_root()) ScoreComputation(filters, n, H, b,
maxXBorder, maxYBorder, scoreThreshold, kLevels[i], procLevels[i],
score, points, kPoints, partsDisplacement);
tasks.push_back(sc);
}
tbb::task::spawn_root_and_wait(tasks);
return LATENT_SVM_OK;
};
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -1,242 +0,0 @@
#include "precomp.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "_lsvm_resizeimg.h"
#include <stdio.h>
#include <assert.h>
#include <math.h>
IplImage* resize_opencv(IplImage* img, float scale)
{
IplImage* imgTmp;
int W, H, tW, tH;
W = img->width;
H = img->height;
tW = (int)(((float)W) * scale + 0.5);
tH = (int)(((float)H) * scale + 0.5);
imgTmp = cvCreateImage(cvSize(tW , tH), img->depth, img->nChannels);
cvResize(img, imgTmp, CV_INTER_AREA);
return imgTmp;
}
//
///*
// * Fast image subsampling.
// * This is used to construct the feature pyramid.
// */
//
//// struct used for caching interpolation values
//typedef struct {
// int si, di;
// float alpha;
//}alphainfo;
//
//// copy src into dst using pre-computed interpolation values
//void alphacopy(float *src, float *dst, alphainfo *ofs, int n) {
// int i;
// for(i = 0; i < n; i++){
// dst[ofs[i].di] += ofs[i].alpha * src[ofs[i].si];
// }
//}
//
//int round(float val){
// return (int)(val + 0.5);
//}
//void bzero(float * arr, int cnt){
// int i;
// for(i = 0; i < cnt; i++){
// arr[i] = 0.0f;
// }
//}
//// resize along each column
//// result is transposed, so we can apply it twice for a complete resize
//void resize1dtran(float *src, int sheight, float *dst, int dheight,
// int width, int chan) {
// alphainfo *ofs;
// float scale = (float)dheight/(float)sheight;
// float invscale = (float)sheight/(float)dheight;
//
// // we cache the interpolation values since they can be
// // shared among different columns
// int len = (int)ceilf(dheight*invscale) + 2*dheight;
// int k = 0;
// int dy;
// float fsy1;
// float fsy2;
// int sy1;
// int sy2;
// int sy;
// int c, x;
// float *s, *d;
//
// ofs = (alphainfo *) malloc (sizeof(alphainfo) * len);
// for (dy = 0; dy < dheight; dy++) {
// fsy1 = dy * invscale;
// fsy2 = fsy1 + invscale;
// sy1 = (int)ceilf(fsy1);
// sy2 = (int)floorf(fsy2);
//
// if (sy1 - fsy1 > 1e-3) {
// assert(k < len);
// assert(sy1 - 1 >= 0);
// ofs[k].di = dy*width;
// ofs[k].si = sy1-1;
// ofs[k++].alpha = (sy1 - fsy1) * scale;
// }
//
// for (sy = sy1; sy < sy2; sy++) {
// assert(k < len);
// assert(sy < sheight);
// ofs[k].di = dy*width;
// ofs[k].si = sy;
// ofs[k++].alpha = scale;
// }
//
// if (fsy2 - sy2 > 1e-3) {
// assert(k < len);
// assert(sy2 < sheight);
// ofs[k].di = dy*width;
// ofs[k].si = sy2;
// ofs[k++].alpha = (fsy2 - sy2) * scale;
// }
// }
//
// // resize each column of each color channel
// bzero(dst, chan*width*dheight);
// for (c = 0; c < chan; c++) {
// for (x = 0; x < width; x++) {
// s = src + c*width*sheight + x*sheight;
// d = dst + c*width*dheight + x;
// alphacopy(s, d, ofs, k);
// }
// }
// free(ofs);
//}
//
//IplImage * resize_article_dp(IplImage * img, float scale, const int k){
// IplImage * imgTmp;
// float W, H;
// unsigned char *dataSrc;
// float * dataf;
// float *src, *dst, *tmp;
// int i, j, kk, channels;
// int index;
// int widthStep;
// int tW, tH;
//
// W = (float)img->width;
// H = (float)img->height;
// channels = img->nChannels;
// widthStep = img->widthStep;
//
// tW = (int)(((float)W) * scale + 0.5f);
// tH = (int)(((float)H) * scale + 0.5f);
//
// src = (float *)malloc(sizeof(float) * (int)(W * H * 3));
//
// dataSrc = (unsigned char*)(img->imageData);
// index = 0;
// for (kk = 0; kk < channels; kk++)
// {
// for (i = 0; i < W; i++)
// {
// for (j = 0; j < H; j++)
// {
// src[index++] = (float)dataSrc[j * widthStep + i * channels + kk];
// }
// }
// }
//
// imgTmp = cvCreateImage(cvSize(tW , tH), IPL_DEPTH_32F, channels);
//
// dst = (float *)malloc(sizeof(float) * (int)(tH * tW) * channels);
// tmp = (float *)malloc(sizeof(float) * (int)(tH * W) * channels);
//
// resize1dtran(src, (int)H, tmp, (int)tH, (int)W , 3);
//
// resize1dtran(tmp, (int)W, dst, (int)tW, (int)tH, 3);
//
// index = 0;
// //dataf = (float*)imgTmp->imageData;
// for (kk = 0; kk < channels; kk++)
// {
// for (i = 0; i < tW; i++)
// {
// for (j = 0; j < tH; j++)
// {
// dataf = (float*)(imgTmp->imageData + j * imgTmp->widthStep);
// dataf[ i * channels + kk] = dst[index++];
// }
// }
// }
//
// free(src);
// free(dst);
// free(tmp);
// return imgTmp;
//}
//
//IplImage * resize_article_dp1(IplImage * img, float scale, const int k){
// IplImage * imgTmp;
// float W, H;
// float * dataf;
// float *src, *dst, *tmp;
// int i, j, kk, channels;
// int index;
// int widthStep;
// int tW, tH;
//
// W = (float)img->width;
// H = (float)img->height;
// channels = img->nChannels;
// widthStep = img->widthStep;
//
// tW = (int)(((float)W) * scale + 0.5f);
// tH = (int)(((float)H) * scale + 0.5f);
//
// src = (float *)malloc(sizeof(float) * (int)(W * H) * 3);
//
// index = 0;
// for (kk = 0; kk < channels; kk++)
// {
// for (i = 0; i < W; i++)
// {
// for (j = 0; j < H; j++)
// {
// src[index++] = (float)(*( (float *)(img->imageData + j * widthStep) + i * channels + kk));
// }
// }
// }
//
// imgTmp = cvCreateImage(cvSize(tW , tH), IPL_DEPTH_32F, channels);
//
// dst = (float *)malloc(sizeof(float) * (int)(tH * tW) * channels);
// tmp = (float *)malloc(sizeof(float) * (int)(tH * W) * channels);
//
// resize1dtran(src, (int)H, tmp, (int)tH, (int)W , 3);
//
// resize1dtran(tmp, (int)W, dst, (int)tW, (int)tH, 3);
//
// index = 0;
// for (kk = 0; kk < channels; kk++)
// {
// for (i = 0; i < tW; i++)
// {
// for (j = 0; j < tH; j++)
// {
// dataf = (float *)(imgTmp->imageData + j * imgTmp->widthStep);
// dataf[ i * channels + kk] = dst[index++];
// }
// }
// }
//
// free(src);
// free(dst);
// free(tmp);
// return imgTmp;
//}
//

View File

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