⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cvaux.hpp

📁 用OpenCV编写的人脸识别代码
💻 HPP
📖 第 1 页 / 共 4 页
字号:
    
    // low-level iterators
    raw_iterator raw_begin() const { return storage.raw_begin(); }
    raw_iterator raw_end() const { return storage.raw_end(); }
    
    // normalizing
    void  normalize( value_type norm_factor = 1 );
    void  normalize( histogram& result, value_type norm_factor = 1 ) const;
    // thresholding
    void  threshold( value_type threshold );
    void  threshold( value_type threshold, histogram& result );

    histogram& operator -= (value_type val);
    histogram& operator *= (value_type val);
    double mean() const;
    
    // some other operations: -=val, *=val, blur, mean ...
    
    // constructors/destructor 
    CVHistogram() { dims = 0; }
    CVHistogram( int bins0, int bins1 = 0, int bins2 = 0, 
                  int bins3 = 0, int bins4 = 0, int bins5 = 0 )
    { create( bins0, bins1, bins2, bins3, bins4, bins5 ); }
    CVHistogram( const histogram& another );
    ~CVHistogram() { destroy(); }

    // copy operation
    histogram& operator = (const histogram& another);
    
    int   get_dims() const { return dims; }
    int   get_dim_size(int n = 0) const { return bins[n]; };

   // helper template method for histograms comparing
    template<class Op> double operate_with( const histogram& another, Op operation ) const
    {
        return storage.operate_with( another.storage, operation );
    }

    bool  check_size_equality( const histogram& another )
    {
        if( dims != another.dims ) return false;
        for( int i = 0; i < dims; i++ ) if( bins[i] != another.bins[i] ) return false;
        return true;
    }

    enum { max_dims = 6 };

protected:

    int           dims;
    int           bins[max_dims];
    int           mbins[max_dims];
    storage_type  storage;

    idx_type get_idx( int bin0, int bin1 = 0, int bin2 = 0,
                      int bin3 = 0, int bin4 = 0, int bin5 = 0 ) const
        { return bin0 * mbins[0] + bin1 * mbins[1] + bin2 * mbins[2] + bin3 * mbins[3]
               + bin4 * mbins[4] + bin5 * mbins[5]; }
};


template <class Hist> inline double calc_histogram_intersection( const Hist& hist1,
                                                                 const Hist& hist2 );

template <class Hist> inline double calc_histogram_chi_square( const Hist& hist1,
                                                               const Hist& hist2 );

template <class Hist> inline double calc_histogram_correlation( const Hist& hist1,
                                                                const Hist& hist2 );

template<class Histogram, class SrcType, class ThreshType> void
    calc_histogram_from_images( Histogram& hist, SrcType** src,
                                CvSize roi, int  step, ThreshType** thresh );

template<class Histogram, class SrcType, class ThreshType, class DstType> void 
    calc_back_project_from_images( Histogram&   Hmodel,
                                   SrcType*     src,
                                   CvSize      roi,
                                   int          src_step,
                                   ThreshType** thresh,
                                   DstType*     measure,
                                   int          dst_step,
                                   DstType      threshold = 0 );

#define CvBackProject calc_back_project_from_images
#define CvCalculateC1 calc_histogram_from_images


#ifndef CVH_STORAGE_IMPLEMENTED
#define CVH_STORAGE_IMPLEMENTED

#include <math.h>

/****************************************************************************************\
*                            CvArray storage implementation                             *
\****************************************************************************************/

template <class Val> CvArray<Val>::CvArray( idx_type _size )
{
    _size = _size > 0 ? _size : 1;
    array = new value_type[_size];
    size = capacity = _size;
}


template <class Val> CvArray<Val>::CvArray( idx_type _size,
                                              idx_type _capacity )
{
    if( _size > _capacity ) _capacity = _size;

    size = _size;
    capacity = _capacity;
    array = new value_type[_capacity];
}


template <class Val> CvArray<Val>::CvArray( const storage& another )
{
    size = another.size;
    capacity = another.capacity;
    array = new value_type[capacity];

    for( idx_type i = 0; i < size; i++ ) array[i] = another.array[i];
}


template <class Val> CvArray<Val>& CvArray<Val>:: operator = ( const storage& another )
{
    /* Check for not empty storage */
    if( another.size > 0 && another.capacity > 0 && another.array != 0 )
    {
        size = another.size;
        capacity = another.capacity;
        delete array;
        array = new value_type[capacity];
        
        for( idx_type i = 0; i < size; i++ ) array[i] = another.array[i];
    }
    else /* If storage is empty */
    {
        size = 0;
        capacity = 0;
        if( array != 0 ) delete array;
        array = 0;
    }
    return *this;
}


template <class Val> CvArray<Val>::iterator CvArray<Val>::begin() const
{
    if( array != 0 ) return iterator( &array[0], &array[0] );
    else return iterator();
}


template <class Val> CvArray<Val>::iterator CvArray<Val>::end() const
{
    if( array != 0 ) return iterator( &array[0], &array[size - 1] );
    else return iterator();
}


template <class Val> CvArray<Val>::raw_iterator CvArray<Val>::raw_begin() const
{
    if( array != 0 ) return iterator( &array[0], &array[0] );
    else return iterator();
}


template <class Val> CvArray<Val>::raw_iterator CvArray<Val>::raw_end() const
{
    if( array != 0 ) return iterator( &array[0], &array[size - 1] );
    else return iterator();
}


template <class Val> void CvArray<Val>::clear()
{
    if( array == 0 ) return;
    for( idx_type i = 0; i < size; i++ ) array[i] = 0;
}


template <class Val> void CvArray<Val>::destroy()
{
    size = capacity = 0;
    if( array != 0 ) delete array;
    array = 0;
}


template <class Val> void CvArray<Val>::remove( idx_type idx )
{
    if( array != 0 && idx < size ) array[idx] = 0;
}


template <class Val> void CvArray<Val>::set_size( idx_type _size )
{
    idx_type i;     
    if( _size < capacity )
    {
        for( i = size; i < _size; i++ ) {
            array[i] = value_type();
        }
        size = _size;
    }
    else
    {
        value_type* bak = new value_type[_size];
        for( i = 0; i < size; i++ ) bak[i] = array[i];
        for( i = size; i < _size; i++ ) bak[i] = value_type();
        size = capacity = _size;
        delete array;
        array = bak;                                  
    }
}


template <class Val> void CvArray<Val>::set_capacity( idx_type _capacity )
{

    if( size < _capacity )
    {
    	idx_type i;
        value_type* bak = new value_type[_capacity];
        for( i = 0; i < size; i++ ) bak[i] = array[i];
        capacity = _capacity;
        delete array;
        array = bak;
    }
}


/****************************************************************************************\
*                               _CvNodeBlock implementation                             *
\****************************************************************************************/
template <class Node> _CvNodeBlock<Node>::_CvNodeBlock()
{
    next = 0;
    /* Links all nodes to next node */
    for( node_type::idx_type i = 0; i < block_nodes - 1; i++ )
        data[i].next() = &data[i + 1];
}


/****************************************************************************************\
*                             _CvNodeManager implementation                             *
\****************************************************************************************/
template <class Node> _CvNodeManager<Node>::_CvNodeManager()
{
    first_free  = 0;
    first_block = 0;
    last_block  = 0;
}


template <class Node> _CvNodeManager<Node>::node_type*
                      _CvNodeManager<Node>::allocate_new_block()
{
    if( last_block != 0 ) /* blocks present */
    {
        /* Making links in new block */
        last_block->next = new block_type;
        ((*last_block->next)[block_type::block_nodes - 1]).next() = first_free;
        
        /* Setting link to first/last block & node */
        last_block = last_block->next;
        first_free = &((*last_block)[0]);
    }
    else /* no blocks present */
    {
        first_block = last_block = new block_type;
        first_free = &((*first_block)[0]);
    }
    return &(*last_block)[0];
}


template <class Node> _CvNodeManager<Node>::node_type* _CvNodeManager<Node>::new_node()
{
    if( first_free == 0 ) first_free = allocate_new_block();
    node_type* _node = first_free;

    first_free = first_free->next();
    _node->link[0] = _node->link[1] = _node->link[2] = 0;
    return _node;
}


template <class Node> void _CvNodeManager<Node>::release_node( Node* _node )
{
    _node->link[1] = _node->link[2] = 0;
    _node->idx = 0;
    _node->balance = 0;
    _node->val = 0;
    _node->next() = first_free;
    first_free = _node;
}


template <class Node> void _CvNodeManager<Node>::destroy()
{
    block_type* _block = first_block;
    while( _block != 0 )
    {
        first_block = _block->next;
        delete _block;
        _block = first_block;
    }

    first_free = 0;
    first_block = 0;
    last_block = 0;
}


template <class Node> void _CvNodeManager<Node>::clear()
{
    if( first_block == 0 ) return;
    iterator iter_first( first_block, 1 );
    iterator iter_second( first_block );

    while( iter_first != iter_second )
        (iter_second++).get_node()->next() = (iter_first++).get_node();
}


/****************************************************************************************\
*                                    CvTree implementation                              *
\****************************************************************************************/
template <class Val, class Idx> CvTree<Val, Idx>::node_type*
            CvTree<Val, Idx>::create_node(CvTree<Val, Idx>::idx_type idx)
{
    assert( size == 0 || idx < size );

    if( root == 0 ) /* if tree is empty */
    {
        root = manager.new_node();
        root->idx = idx;
        root->balance = node_type::center;
        return root;
    }

    node_type* nodeA = root;
    node_type* nodeB;
    node_type* nodeC;

    if( root->idx == idx ) return root; /* if node exists alredy return this node */
    
    /* Find point, where this node must be placed */
    while( nodeA->link[idx > nodeA->idx] != 0 )
    {
        nodeA = nodeA->link[idx > nodeA->idx];
        if( nodeA->idx == idx ) return nodeA; /* if node exists alredy return this node */
    }
    if( nodeA->idx == idx ) return nodeA; /* if node exists alredy return this node */

    node_type* node = manager.new_node();
    node->link[node_type::up] = nodeA;
    node->idx = idx;
    node->balance = node_type::center;
    nodeA->link[idx > nodeA->idx] = node;

    /* Balancing tree :( */
    do{
        switch( nodeA->balance )
        {
        case node_type::left:
            if( idx > nodeA->idx )
            {
                nodeA->balance = node_type::center;
                return node;
            }
            else /* idx < nodeA->idx */
            {
                nodeB = nodeA->link[node_type::left];
                switch( nodeB->balance )
                {
                case node_type::left:
                    nodeA->link[node_type::left] = nodeB->link[node_type::right];
                    if( nodeA->link[node_type::left] != 0 )
                        nodeA->link[node_type::left]->link[node_type::up] = nodeA;
                    nodeB->link[node_type::right] = nodeA;
                    nodeB->link[node_type::up] = nodeA->link[node_type::up];
                    nodeA->link[node_type::up] = nodeB;
                    nodeA->balance = nodeB->balance = node_type::center;
                    if( nodeB->link[node_type::up] != 0 )
                        nodeB->link[node_type::up]->
                        link[idx > nodeB->link[node_type::up]->idx] = nodeB;
                    else root = nodeB;
                    return node;
                case node_type::right:
                    nodeC = nodeB->link[node_type::right];
                    nodeC->link[node_type::up] = nodeA->link[node_type::up];
                    nodeA->link[node_type::left] = nodeC->link[node_type::right];
                    if( nodeA->link[node_type::left] != 0 )
                        nodeA->link[node_type::left]->link[node_type::up] = nodeA;
                    nodeB->link[node_type::right] = nodeC->link[node_type::left];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -