colorhistogram.h

来源:互联网 发布:网络直播营销方案 编辑:程序博客网 时间:2024/05/20 10:55
#if !defined COLHISTOGRAM
#define COLHISTOGRAM
 
#include <opencv2\core\core.hpp>
#include <opencv2\imgproc\imgproc.hpp>
 
classColorHistogram {
 
 
  private:
 
    inthistSize[3];
floathranges[2];
    constfloat* ranges[3];
    intchannels[3];
 
  public:
 
ColorHistogram() {
 
 
// Prepare arguments for a color histogram
histSize[0]= histSize[1]= histSize[2]= 256;
hranges[0]= 0.0;   // BRG range
hranges[1]= 255.0;
ranges[0]= hranges;// all channels have the same range
ranges[1]= hranges;
ranges[2]= hranges;
channels[0]= 0;// the three channels
channels[1]= 1;
channels[2]= 2;
 
}
 
// Computes the histogram.
cv::MatND getHistogram(constcv::Mat &image) {
 
 
cv::MatND hist;
 
// BGR color histogram
hranges[0]= 0.0;   // BRG range
hranges[1]= 255.0;
channels[0]= 0;// the three channels
channels[1]= 1;
channels[2]= 2;
 
// Compute histogram
cv::calcHist(&image,
1,// histogram of 1 image only
channels,// the channel used
cv::Mat(),// no mask is used
hist,// the resulting histogram
3,// it is a 3D histogram
histSize,// number of bins
ranges// pixel value range
);
 
returnhist;
 
}
 
// Computes the histogram.
cv::SparseMat getSparseHistogram(constcv::Mat &image) {
 
 
cv::SparseMat hist(3,histSize,CV_32F);
 
// BGR color histogram
hranges[0]= 0.0;   // BRG range
hranges[1]= 255.0;
channels[0]= 0;// the three channels
channels[1]= 1;
channels[2]= 2;
 
// Compute histogram
cv::calcHist(&image,
1,// histogram of 1 image only
channels,// the channel used
cv::Mat(),// no mask is used
hist,// the resulting histogram
3,// it is a 3D histogram
histSize,// number of bins
ranges// pixel value range
);
 
returnhist;
 
}
 
// Computes the 2D ab histogram.
// BGR source image is converted to Lab
cv::MatND getabHistogram(constcv::Mat &image) {
 
 
cv::MatND hist;
 
// Convert to Lab color space
cv::Mat lab;
cv::cvtColor(image, lab, CV_BGR2Lab);
 
// Prepare arguments for a 2D color histogram
hranges[0]= -128.0;
hranges[1]= 127.0;
channels[0]= 1;// the two channels used are ab
channels[1]= 2;
 
// Compute histogram
cv::calcHist(&lab,
1,// histogram of 1 image only
channels,// the channel used
cv::Mat(),// no mask is used
hist,// the resulting histogram
2,// it is a 2D histogram
histSize,// number of bins
ranges// pixel value range
);
 
returnhist;
 
}
 
// Computes the 1D Hue histogram with a mask.
// BGR source image is converted to HSV
cv::MatND getHueHistogram(constcv::Mat &image) {
 
 
cv::MatND hist;
 
// Convert to Lab color space
cv::Mat hue;
cv::cvtColor(image, hue, CV_BGR2HSV);
 
// Prepare arguments for a 1D hue histogram
hranges[0]= 0.0;
hranges[1]= 180.0;
channels[0]= 0;// the hue channel
 
// Compute histogram
cv::calcHist(&hue,
1,// histogram of 1 image only
channels,// the channel used
cv::Mat(),// no mask is used
hist,// the resulting histogram
1,// it is a 1D histogram
histSize,// number of bins
ranges// pixel value range
);
 
returnhist;
 
}
 
cv::Mat colorReduce(constcv::Mat &image, intdiv=64) {
 
 
  intn= static_cast<int>(log(static_cast<double>(div))/log(2.0));
  // mask used to round the pixel value
  uchar mask= 0xFF<<n;// e.g. for div=16, mask= 0xF0
 
  cv::Mat_<cv::Vec3b>::const_iterator it= image.begin<cv::Vec3b>();
  cv::Mat_<cv::Vec3b>::const_iterator itend= image.end<cv::Vec3b>();
 
  // Set output image (always 1-channel)
  cv::Mat result(image.rows,image.cols,image.type());
  cv::Mat_<cv::Vec3b>::iterator itr= result.begin<cv::Vec3b>();
 
  for( ; it!= itend; ++it, ++itr) {
 
         
        (*itr)[0]= ((*it)[0]&mask) +div/2;
        (*itr)[1]= ((*it)[1]&mask) +div/2;
        (*itr)[2]= ((*it)[2]&mask) +div/2;
   
}
 
  returnresult;
 
}
 
 
};
 
 
#endif
0 0
原创粉丝点击