计算彩色的BGR图像的直方图

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>

using namespace std;
using namespace cv;

class ColorHistogram
{
private:
	int histSize[3];
	float hranges[2];
	const float *ranges[3];
	int channels[3];
public:
	ColorHistogram();
	cv::MatND getHistogram(const cv::Mat &image);
	cv::SparseMat getSparseHistogram(const cv::Mat &image);
	cv::Mat getHistogramImage(const cv::Mat &image,int i);
};
ColorHistogram::ColorHistogram()
{
	histSize[0]=256;
		histSize[1]=256;
			histSize[2]=256;
	hranges[0]=0.0;
	hranges[1]=255.0;
	ranges[0]=hranges;
	ranges[1]=hranges;
	ranges[2]=hranges;
	channels[0]=0;
	channels[1]=1;
	channels[2]=2;
}
cv::MatND ColorHistogram::getHistogram(const cv::Mat &image)
{
	cv::MatND hist;
	cv::calcHist(&image,1,channels,cv::Mat(),hist,3,histSize,ranges);
	return hist;
}
cv::SparseMat ColorHistogram::getSparseHistogram(const cv::Mat &image)
{
	cv::SparseMat hist(3,histSize,CV_32F);
	cv::calcHist(&image,1,channels,cv::Mat(),hist,3,histSize,ranges);
	return hist;
}

你可能感兴趣的:(C++,opencv,直方图,opencv2)