rgb三色直方图的绘制

三色和单色是一致的:程序的写法在下一个CSDN
不仅仅是RGB还有HSV图像都会有比较重要的画法:色调,饱和度,亮度
绘制三色直方图还是三种向量的直方图都会有意义:
1.参数准备:
int bins=256;每一个分量都是256的
int hist_size[]={bins};
float range[]={0,256};
const float* ranges[]={range};
MatND redHist,greenHist,blueHist;
int channels_r[]={2};
int channels_g[]={1};
int channels_b[]={0};
calcHist(&rgb图像,1,channel_r,Mat(),redHist,1,hist_size,ranges,true,false);得到红色分量的直方图
calcHist(&rgb图像,1,channel_g,Mat(),greenHist,1,hist_size,ranges,true,false);得到绿色分量的直方图
calcHist(&rgb图像,1,channel_b,Mat(),blueHist,1,hist_size,ranges,true,false); 得到蓝色分量的直方图
//得到各个分量的直方图
2.获取各个直方图中的最大值和最小值
double max_Value_red,max_Value_green,max_Value_blue;
minMaxLoc(redHist,0,&max_Value_red,0,0);
minMaxLoc(blueHist,0,&max_Value_blue,0,0);
minMaxLoc(greenHist,0,&max_Value_green,0,0); //得到三个分量的最大值,也就是点数最多的像素点

3.绘制三个分量的直方图
定义要显示的直方图:
Mat histImg=Mat::zeros(256,2563,CV_8UC3);把三个分量的直方图绘制在一个图片上面
for(int i=0;i<bins;i++)
{
获取各个分量的实际的数值
float binValue_red=redHist.at(i);
float binValue_green=greenHist.at(i);
float binValue_blue=blueHist.at(i);
要绘制的高度,对于每一个分量来说的
int intensty_red=cvRound(binValue_red
256/max_Value_red);红色分量要绘制的高度
int intensity_green=cvRound(binValue_green256/max_Value_green);绿色分量的高度
int intensity_blue=cvRound(binValue_blue
256/max_Value_blue);蓝色分量对应的高度
用矩形进行绘制
红色分量的绘制
rectangle(histImg,Point(i,255),Point(i,256-intensity_red),Scalar(0,0,255));
rectangle(histImg,Point((i+256,255),Point(i+256,256-intensity_green),Scalar(0,255,0));
rectangle(histImg,Point(i+2256,255),Point(i+2256,256-intensity_blue),Scalar(255,0,0));
}
这就进行绘制完毕了
4.最后一步了:显示绘制好的三色直方图

猜你喜欢

转载自blog.csdn.net/nbxuwentao/article/details/86533150