📄 cvaux.hpp
字号:
{
mbins[0] = 1;
mbins[1] = bins1;
mbins[2] = mbins[1] * bins2;
mbins[3] = mbins[2] * bins3;
mbins[4] = mbins[3] * bins4;
mbins[5] = mbins[4] * bins5;
bins[0] = bins0;
bins[1] = bins1;
bins[2] = bins2;
bins[3] = bins3;
bins[4] = bins4;
bins[5] = bins5;
storage.clear();
if( bins0 == 0 ) {dims = 0; return;}
if( bins1 == 0 ) {dims = 1; storage.set_size( bins0 ); return;}
if( bins2 == 0 ) {dims = 2; storage.set_size( bins0 * mbins[1] ); return;}
if( bins3 == 0 ) {dims = 3; storage.set_size( bins0 * mbins[2] ); return;}
if( bins4 == 0 ) {dims = 4; storage.set_size( bins0 * mbins[3] ); return;}
if( bins5 == 0 ) {dims = 5; storage.set_size( bins0 * mbins[4] ); return;}
dims = 6;
storage.set_size( (idx_type)bins0 * mbins[5] );
}
template<class Storage> void CVHistogram<Storage>::normalize( value_type norm_factor )
{
double sum = 0;
iterator iter = begin();
iterator _end = end();
do
{
sum += *iter;
}while( iter++ != _end );
if( sum == 0 ) return;
sum = 1.0 / sum * norm_factor;
raw_iterator raw_iter = raw_begin();
raw_iterator _raw_end = raw_end();
do
{
*raw_iter = (value_type)(sum * (*raw_iter));
}while( raw_iter++ != _raw_end );
}
template<class Storage> void CVHistogram<Storage>::
normalize( histogram& result, value_type norm_factor ) const
{
double sum = 0;
iterator iter = begin();
iterator bak = iter;
iterator _end = end();
do
{
sum += *iter;
}while( iter++ != _end );
if( sum == 0 ) return;
sum = 1.0 / sum * norm_factor;
result.clear();
iter = bak;
do
{
result[iter.get_idx()] = (value_type)(sum * (*iter));
}while( iter++ != _end );
}
template<class Storage> void CVHistogram<Storage>::threshold( value_type threshold )
{
raw_iterator iter = raw_begin();
raw_iterator _raw_end = raw_end();
do
{
*iter = (value_type)(*iter >= threshold ? *iter : 0);
}while( iter++ != _raw_end );
}
template<class Storage> void
CVHistogram<Storage>::threshold( value_type threshold, histogram& result )
{
raw_iterator iter = raw_begin();
raw_iterator _raw_end = raw_end();
result.clear();
do
{
result[iter.get_idx()] = (value_type)(*iter >= threshold ? *iter : 0);
}while( iter++ != _raw_end );
}
template<class Storage> CVHistogram<Storage>::histogram&
CVHistogram<Storage>::operator =( const histogram& another )
{
raw_iterator iter = another.raw_begin();
raw_iterator _raw_end = another.raw_end();
create( another.bins[0], another.bins[1], another.bins[2],
another.bins[3], another.bins[4], another.bins[5] );
if( &(*iter) == 0 ) return *this;
do
{
(*this)[iter.get_idx()] = *iter;
}while( iter++ != _raw_end );
return *this;
}
template<class Storage> CVHistogram<Storage>::histogram&
CVHistogram<Storage>::operator -= ( value_type val )
{
raw_iterator iter = raw_begin();
raw_iterator _end = raw_end();
do
{
*iter = (value_type)(*iter > val ? *iter - val : 0);
}while(iter++ != _end);
return *this;
}
template<class Storage> CVHistogram<Storage>::histogram&
CVHistogram<Storage>::operator *= ( value_type val )
{
raw_iterator iter = raw_begin();
raw_iterator _end = raw_end();
do *iter = (value_type)(*iter * val); while(iter++ != _end);
return *this;
}
template<class Storage> double CVHistogram<Storage>::mean() const
{
iterator iter = begin();
iterator _end = end();
double s = 0;
do
s += *iter;
while(iter++ != _end);
return s / (bins[0] * mbins[dims - 1]);
}
template<class Storage> CVHistogram<Storage>::value_type&
CVHistogram<Storage>::operator () (const int* idxs)
{
switch( dims )
{
case 1:
return (*this)(idxs[0]);
case 2:
return (*this)(idxs[0], idxs[1]);
case 3:
return (*this)(idxs[0], idxs[1], idxs[2]);
case 4:
return (*this)(idxs[0], idxs[1], idxs[2], idxs[3]);
case 5:
return (*this)(idxs[0], idxs[1], idxs[2], idxs[3], idxs[4]);
case 6:
return (*this)(idxs[0], idxs[1], idxs[2], idxs[3], idxs[4], idxs[5]);
default:
assert( 0 );
return (*this)[*idxs];
}
}
template<class Storage> CVHistogram<Storage>::value_type
CVHistogram<Storage>::query(const int* idxs) const
{
switch( dims )
{
case 0:
return 0;
case 1:
return (*this).query(idxs[0]);
case 2:
return (*this).query(idxs[0], idxs[1]);
case 3:
return (*this).query(idxs[0], idxs[1], idxs[2]);
case 4:
return (*this).query(idxs[0], idxs[1], idxs[2], idxs[3]);
case 5:
return (*this).query(idxs[0], idxs[1], idxs[2], idxs[3], idxs[4]);
case 6:
return (*this).query(idxs[0], idxs[1], idxs[2], idxs[3], idxs[4], idxs[5]);
default:
assert( 0 );
return 0;
}
}
/****************************************************************************************\
* Histogram calculation functions *
\****************************************************************************************/
/*F///////////////////////////////////////////////////////////////////////////////////////
// Name: CvCalculateC1
// Purpose: Calculating the histogram from C1 data type
// Context:
// Parameters:
// hist - destination histogram
// src - source image
// roi - region fo interest
// step - width step
// thresh - threshold values for each dimension of histogram
// Returns:
// Notes:
//F*/
template<class Histogram, class SrcType, class ThreshType> void
CvCalculateC1( Histogram& hist,
SrcType** src,
CvSize roi,
int step,
ThreshType** thresh )
{
assert( roi.width * (int)sizeof( SrcType ) <= step );
int offset = 0;
step /= sizeof( SrcType );
for( int y = 0; y < roi.height; y++ )
{
for( int x = 0; x < roi.width; x++ )
{
int addr[Histogram::max_dims];
for( int plan = 0; plan < hist.get_dims(); plan++ )
{
int num;
SrcType p = src[plan][offset + x];
int nums = hist.get_dim_size(plan);
if( p < thresh[plan][0] || p > thresh[plan][nums] ) goto _next_point;
for( num = 1; num < nums; num++ )
if( p <= thresh[plan][num] ) break;
addr[plan] = num - 1;
}
hist(addr)++;
_next_point:;
}
offset += step;
}
}
template<class Histogram, class SrcType, class ThreshType, class DstType> void
CvBackProject( Histogram& hist,
SrcType** src,
CvSize roi,
int src_step,
ThreshType** thresh,
DstType* measure,
int dst_step,
DstType threshold )
{
assert( roi.width * (int)sizeof( SrcType ) <= src_step );
assert( roi.width * (int)sizeof( DstType ) <= dst_step );
int src_offset = 0;
int dst_offset = 0;
src_step /= sizeof( SrcType );
dst_step /= sizeof( DstType );
for( int y = 0; y < roi.height; y++ )
{
for( int x = 0; x < roi.width; x++ )
{
int num;
int addr[Histogram::max_dims];
for( int plan = 0; plan < hist.get_dims(); plan++ )
{
SrcType p = src[plan][src_offset + x];
int nums = hist.get_dim_size(plan);
if( p < thresh[plan][0] || p > thresh[plan][nums] )
{
measure[dst_offset + x] = 0;
goto _next_point;
}
for( num = 1; num < nums; num++ )
if( p <= thresh[plan][num] ) break;
addr[plan] = num - 1;
}
measure[dst_offset + x] = (DstType)(hist.query(addr));
if( measure[dst_offset + x] < threshold ) measure[dst_offset + x] = 0;
_next_point:;
}
src_offset += src_step;
dst_offset += dst_step;
}
}
/****************************************************************************************\
* Operations implementation *
\****************************************************************************************/
/****************************************************************************************\
* Intersection *
\****************************************************************************************/
class Intersection
{
public:
typedef double result_type;
template<class Val> double operator () ( double s, const Val val1, const Val val2 )
{ return s + (val1 > val2 ? val2 : val1); }
};
template <class Hist> inline double calc_histogram_intersection( const Hist& hist1,
const Hist& hist2 )
{ return hist1.operate_with( hist2, Intersection() ); }
/****************************************************************************************\
* ChiSqr *
\****************************************************************************************/
class ChiSqr
{
public:
typedef double result_type;
template<class Val> double operator () ( double s, Val val1, Val val2 )
{ return s + (double)(val1 - val2) * (val1 - val2) / (val1 + val2); }
};
template <class Hist> inline double calc_histogram_chi_square( const Hist& hist1,
const Hist& hist2 )
{ return hist1.operate_with( hist2, ChiSqr() ); }
/****************************************************************************************\
* Correl *
\****************************************************************************************/
class _Correl
{
public:
double Nom;
double DeNom1, DeNom2;
_Correl( double val ) { Nom = DeNom1 = DeNom2 = val; }
void operator=( double val ) { Nom = DeNom1 = DeNom2 = val; }
operator double() { return Nom / sqrt( DeNom1 * DeNom2 ); }
};
class Correl
{
public:
typedef _Correl result_type;
Correl() { mean1 = mean2 = 0; }
Correl( double _mean1, double _mean2 = 0 ) { mean1 = _mean1; mean2 = _mean2; }
template<class Val> result_type operator () ( result_type s, Val val1, Val val2 )
{
double v1 = val1 - mean1;
double v2 = val2 - mean2;
s.Nom += v1 * v2;
s.DeNom1 += v1 * v1;
s.DeNom2 += v2 * v2;
return s;
}
protected:
double mean1;
double mean2;
};
template <class Hist> inline double calc_histogram_correlation( const Hist& hist1,
const Hist& hist2 )
{ return hist1.operate_with( hist2, Correl( hist1.mean(), hist2.mean() ) ); }
#endif
#endif /* #if defined _MSC_VER || defined __ICL || defined __BORLANDC__ */
#endif /* __cplusplus */
/* End of file. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -