Package pycv :: Package cs :: Package cv :: Module haar
[hide private]
[frames] | no frames]

Module haar

source code

Classes [hide private]
  THaarClassifier
Functions [hide private]
 
_features_from_shape(M, N, appearance)
Generate all rectangular Haar-like features, of a specific 'appearance'.
source code
 
generate_Haars(M, N)
Find all Haar-like features, applied on a M-by-N image patch.
source code
 
subsample_Haars(haars, ratio)
Take a set of Haar features and subsample it.
source code
 
_project_Haar_stats(stats2, haars)
Consider T Haar features generated by generate_Haars().
source code
 
get_local_stats(cd, haars)
Take a 2-class ClassificationDataset, obtain the global statistics, then project using different Haar directions.
source code
 
project_Haar(input_data, A, ind)
Project the input data into a line using haar direction specified by coefficients 'A' and indices 'ind'
source code
 
project_SR_stats(stats2, A, ind)
Project the high-dim statistics into a line using a SR direction specified by coefficients 'A' and indices 'ind'
source code
 
train_Haar_with_local_stats(Binvert, haars, crit, param1)
Given local statistics, train a THaarClassifier.
source code
 
train_Haar(cd, haars, crit, param1)
Take a 2-class ClassificationDataset, then train a THaarClassifier.
source code
 
train_Haar_UAC_filter(scd, haars, rmaxFRR=0.001, rmaxFAR=0.8)
Take a ScoringCDataset, then train a THaarClassifier 'filter' using UAC.
source code
 
train_Haar_UAC_improver2(scd, haars, rmaxFRR=0.001, rmaxFAR=0.8)
Take a ScoringCDataset, then train a THaarClassifier 'filter' using UAC.
source code
 
train_Haar_UAC_improver(scd, haars, thelambda, bsol)
Take a ScoringCDataset, then train a THaarClassifier 'improver' using UAC.
source code
 
train_Haar_GC(scd, cdgenerator, haars, thelambda=1.0, rmaxFRR=0.001, rmaxFAR=0.8, maxM=20)
Take a CDataset, then train a GeneralizedCascade of THaarClassifiers.
source code
 
train_SRClassifier(stats2, criterion, param1, nrects, nseconds)
Train Sparse Rectangular features and select the best feature classifier.
source code
 
train_refined_SRClassifier(wcd)
A training function that takes in a WeightedCDataset and produces a weak BinaryClassifier.
source code
Function Details [hide private]

_features_from_shape(M, N, appearance)

source code 

Generate all rectangular Haar-like features, of a specific 'appearance'. All rectangles are bounded by a M-by-N image patch.

Input:
M, N: number of rows and columns of an image patch appearance: an 'int' 2D numpy.array describing the appearance. E.g. array([[1,-1],[-1,1]]).
Output:
A: a 1D numpy.array of projection coefficients, in vectorintegrated form ind: a 2D numpy.array of indices of each feature, in vectorintegrated form ind2: a 2D numpy.array of 4 corners of each feature in the patch

generate_Haars(M, N)

source code 

Find all Haar-like features, applied on a M-by-N image patch.

Input:

M, N: number of rows and columns of an image patch

Output:

haars: a list of tuples (A,ind,ind2) output from _features_from_shape()

subsample_Haars(haars, ratio)

source code 
Take a set of Haar features and subsample it.

Input:
    haars: a set of Haar features, output of generate_Haars()
    ratio: the ratio to subsample, if 'haars' has N features,
        the output 'haars' will have roughly ratio*N features
Output:
    ohaars: a smaller set of Haar features

_project_Haar_stats(stats2, haars)

source code 
Consider T Haar features generated by generate_Haars().
For each feature: project, compute the statistics of the two classes,
then refine if needed. Return a list of J*2-class Stats2e, each for each
type of Haar features.

Input:
    stats2: The 'global' statistics of the two classes, can be obtained
        by calling compute_Stats2_integral()
    haars: the T Haar features generated, by caling generate_Haars()
Output:
    B: a list of J*2-class Stats2e, each for each type of Haar features.
    invert: a list of numpy.arrays, each array tells whether which feature
        needs negating its projection direction

get_local_stats(cd, haars)

source code 

Take a 2-class ClassificationDataset, obtain the global statistics,
then project using different Haar directions.

Input:
    cd: a 2-class ClassificationDataset
    haars: the generated Haar feature set of J features
Output: output of _project_Haar_stats() actually
    B: a list of J*2-class Stats2e, each for each type of Haar features.
    invert: a list of numpy.arrays, each array tells whether which feature
        needs negating its projection direction

project_Haar(input_data, A, ind)

source code 

Project the input data into a line using haar direction specified by coefficients 'A' and indices 'ind'

Input:

input_data: a vectorintegrated 2D numpy.array A,ind: coefficients and indices of a Haar feature

Output:

x: a 1D numpy.array of projected values

project_SR_stats(stats2, A, ind)

source code 
Project the high-dim statistics into a line using a SR direction specified by coefficients 'A' and indices 'ind'
Parameters:
  • stats2, Stats2 - a vectorintegrated 2D numpy.array
  • ind, array(shape=(n, ), dtype='int') - indices of the SR feature
  • A, array(shape=(n, ), dtype='double') - coefficients of the SR feature
Returns:
stats2e: a 1D Stats2e representing the projected result

train_Haar_with_local_stats(Binvert, haars, crit, param1)

source code 
Given local statistics, train a THaarClassifier.
Select the 'best' Haar feature in the set.
The features are trained using one of three criteria:
    crit = 0: param1 is 'thelambda', then call gaussian.find_classification_threshold()
    crit = 1: param1 is 'minDR', then call gaussian.find_filtering_threshold()
    crit = 2: param1 is 'maxFAR', then call gaussian.find_filtering_threshold2()

Input: 
    Binvert: (B,invert), is output of get_local_stats() actually
        B: a list of J*2-class Stats2e, each for each type of Haar features.
        invert: a list of numpy.arrays, each array tells whether which feature
            needs negating its projection direction
    haars: the generated Haar feature set of J features
    crit, param1: as mentioned above
Output:
    lc: a THaarClassifier, with lc.err as the estimated 'error'

train_Haar(cd, haars, crit, param1)

source code 

Take a 2-class ClassificationDataset, then train a THaarClassifier. Select the 'best' Haar feature in the set. This function actually calls get_local_stats(), followed by train_Haar_with_local_stats()

Input:

cd: a 2-class ClassificationDataset haars: the generated Haar feature set crit, param1: as mentioned above

Output:

lc: a THaarClassifier, with lc.err as the estimated 'error'

train_Haar_UAC_filter(scd, haars, rmaxFRR=0.001, rmaxFAR=0.8)

source code 
Take a ScoringCDataset, then train a THaarClassifier 'filter' using UAC.
Select the 'best' Haar feature in the set.

Input:
    scd: a ScoringCDataset -- must be vectorintegrated
    haars: the generated Haar feature set
    rmaxFRR, rmaxFAR: train 'filter' to reject if there is a classifier
        that can do FRR <= rmaxFRR and FAR <= rmaxFAR
Output:
    lc: a THaarClassifier, with:
        lc.sol: training (FRR,FAR)
    c: voting coefficient
    both lc and c will be None if there's no filter satisfied

train_Haar_UAC_improver2(scd, haars, rmaxFRR=0.001, rmaxFAR=0.8)

source code 
Take a ScoringCDataset, then train a THaarClassifier 'filter' using UAC.
Select the 'best' Haar feature in the set.

Input:
    scd: a ScoringCDataset -- must be vectorintegrated
    haars: the generated Haar feature set
    rmaxFRR, rmaxFAR: train 'filter' to reject if there is a classifier
        that can do FRR <= rmaxFRR and FAR <= rmaxFAR
Output:
    lc: a THaarClassifier, with:
        lc.sol: training (FRR,FAR)
    c: voting coefficient
    both lc and c will be None if there's no filter satisfied

train_Haar_UAC_improver(scd, haars, thelambda, bsol)

source code 

Take a ScoringCDataset, then train a THaarClassifier 'improver' using UAC. Select the 'best' Haar feature in the set.

Input:
scd: a ScoringCDataset -- must be vectorintegrated haars: the generated Haar feature set thelambda: the goal is to minimize thelambda * FRR + FAR bsol: previously best (FRR,FAR)
Output:
lc: a THaarClassifier, with:
lc.sol: training (FRR,FAR)

c: voting coefficient both lc and c will be None if there's no filter satisfied

train_Haar_GC(scd, cdgenerator, haars, thelambda=1.0, rmaxFRR=0.001, rmaxFAR=0.8, maxM=20)

source code 
Take a CDataset, then train a GeneralizedCascade of THaarClassifiers.

Input:
    scd: a 2-class ScoringCDataset -- must be vectorintegrated
        scd *is modified* after the training process
    cdgenerator: a CDGenerator that can generate negative points for scd
    haars: the generated Haar feature set
    thelambda: the goal for 'improver' is to minimize thelambda*FRR+FAR
    rmaxFRR, rmaxFAR: train 'filter' to reject if there is a classifier
        that can do FRR <= rmaxFRR and FAR <= rmaxFAR
Output:
    gc: a GeneralizedCascade

train_SRClassifier(stats2, criterion, param1, nrects, nseconds)

source code 
Train Sparse Rectangular features and select the best feature classifier.

Suppose we have 2 classes c: positive and negative.
Suppose the vector-integrated version of an image of size 'len'-by-'len'
is a random vector y.
Let 'stats' be the Stats2 statistics of the two classes.
Consider 'criterion' and 'param1' described in sdGTSolve().

Now consider the following features called Sparse Rectangular features:
   H = \sum_{k=1}^{nrects} a_k \pi(x_k,y_k,w_k,h_k)
This function trains and selects the 'best' Sparse Rectangular feature
classifier based on sdGTSolve()'s criterion.
The classifier is as follows:
    return sign( (H \convolve I) - threshold )

The underlying algorithm is Differential Evolution.
It runs for 'nseconds' seconds.

:Parameters:
    stats : Stats2
        Statistics of the 2 classes
    criterion : int
        from 0 to 3, used in conjunction with param1, see sdGTSolve()
    param1 : double
        see 'criterion'
    nrects : int
        number of rectangles of the sparse feature
    nseconds : double
        this algorithm uses Differential Evolution, so this tells at
        most how many seconds to run

:Returns:
    fc: THaarClassifier
        the best feature classifier obtained
        fc.err is the estimated 'error' obtained

    ngen : int
        number of generations used in DE
    ngenome : int
        number of genomes generated in DE
    nvgenome : int
        number of *valid* genomes generated in DE

train_refined_SRClassifier(wcd)

source code 

A training function that takes in a WeightedCDataset and produces a weak BinaryClassifier.

This training function uses train_SRClassifier() to train but with 6 rectangles and 2 seconds. Also, the threshold of the weak classifier is refined after training. The objective is to minimize classification error with lambda = 1.0

Parameters:
  • wcd (WeightedCDataset) - a weighted dataset of vector-integrated patches
Returns:
fc: THaarClassifier
the best feature classifier obtained with

fc.b being the best threshold after refined fc.err is the estimated 'error' corresponding to fc.b fc.b2 is the best threshold estimated by train_SRClassifier() fc.err2 is its corresponding 'estimated error' fc.stats2e is its corresponding 1D statistics