Skip to content

Instantly share code, notes, and snippets.

@jrdi
Last active December 20, 2015 03:29
Show Gist options
  • Save jrdi/6063555 to your computer and use it in GitHub Desktop.
Save jrdi/6063555 to your computer and use it in GitHub Desktop.
// entropy(hsv)
// it computes the histogram on a neighborhood
Features::VectImgType Features::Histogram (const VectUcharPointType& pt)
{
VectImgType hist( 3, cv::Mat::zeros(256, 1, CV_8UC1) );
for (VectUcharPointType::const_iterator it = pt.begin();
it != pt.end();
++it )
{
const cv::Vec3b& pix = *it;
for( int d = 0; d < 3; d++)
{
cv::Mat& tempHist = hist[d];
uchar val = pix[d];
tempHist.at<uchar>( val, static_cast<uchar>( 0 ) ) = tempHist.at<uchar>( val, static_cast<uchar>( 0 ) ) + static_cast<uchar>( 1 );
}
}
return hist;
}
// it compute the entropy
cv::Vec3f Features::Entropy (const VectUcharPointType& pt, const VectImgType& hist)
{
assert( !pt.empty() );
size_t N = pt.size();
const float invN = 1. / static_cast< float >( N );
cv::Vec3f ent(0., 0., 0.);
for( int d = 0; d < 3; d++)
{
const cv::Mat& Hist = hist[d];
float entropy = 0.;
for (size_t i = 0; i < N; i++ )
{
const float val = ( pt[i] )[d];
const float p = static_cast<float>( Hist.at< uchar >( val, 0 ) ) * invN;
if( p > 0. )
{
entropy -= p*log(p);
}
else
{
std::cout << "p = 0"<<std::endl;
}
}
ent[d] = entropy;
}
return ent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment