#include
#include
using namespace cv;
int main( )
{
Mat srcImage;
srcImage=imread("8.jpg");
imshow( "素材图", srcImage );
int bins = 256;
int hist_size[] = {bins};
float range[] = { 0, 256 };
const float* ranges[] = { range};
MatND redHist,grayHist,blueHist;
int channels_r[] = {0};
//进行直方图的计算(红色分量部分)
calcHist( &srcImage, 1, channels_r, Mat(),
redHist, 1, hist_size, ranges,
true, false );
//进行直方图的计算(绿色分量部分)
int channels_g[] = {1};
calcHist( &srcImage, 1, channels_g, Mat(), // do not use mask
grayHist, 1, hist_size, ranges,
true, // the histogram is uniform
false );
//进行直方图的计算(蓝色分量部分)
int channels_b[] = {2};
calcHist( &srcImage, 1, channels_b, Mat(), // do not use mask
blueHist, 1, hist_size, ranges,
true, // the histogram is uniform
false );
//-----------------------绘制出三色直方图------------------------
//参数准备
double maxValue_red,maxValue_green,maxValue_blue;
minMaxLoc(redHist, 0, &maxValue_red, 0, 0);
minMaxLoc(grayHist, 0, &maxValue_green, 0, 0);
minMaxLoc(blueHist, 0, &maxValue_blue, 0, 0);
int scale = 1;
int histHeight=256;
Mat histImage = Mat::zeros(histHeight,bins*3, CV_8UC3);
//正式开始绘制
for(int i=0;i(i);
float binValue_green = grayHist.at(i);
float binValue_blue = blueHist.at(i);
int intensity_red = cvRound(binValue_red*histHeight/maxValue_red); //要绘制的高度
int intensity_green = cvRound(binValue_green*histHeight/maxValue_green); //要绘制的高度
int intensity_blue = cvRound(binValue_blue*histHeight/maxValue_blue); //要绘制的高度
//绘制红色分量的直方图
rectangle(histImage,Point(i*scale,histHeight-1),
Point((i+1)*scale - 1, histHeight - intensity_red),
Scalar(255,0,0));
//绘制绿色分量的直方图
rectangle(histImage,Point((i+bins)*scale,histHeight-1),
Point((i+bins+1)*scale - 1, histHeight - intensity_green),
Scalar(0,255,0));
//绘制蓝色分量的直方图
rectangle(histImage,Point((i+bins*2)*scale,histHeight-1),
Point((i+bins*2+1)*scale - 1, histHeight - intensity_blue),
Scalar(0,0,255));
}
//在窗口中显示出绘制好的直方图
imshow( "图像的RGB直方图", histImage );
waitKey(0);
return 0;
}
程序运行结果: