Mat image = imread("D:/ju.jpg");
imshow("素材图", image);
int bins = 256; //直条为256
int hist_size[] = { bins };
float range[] = { 0,256 };
const float* ranges[] = { range }; MatND redHist, greenHist, blueHist;
//MAT数据为BGR
int channels_r[] = { 2 }; //r通道
calcHist(&image, 1, channels_r, Mat(), redHist, 1, hist_size, ranges, true, false);
int channels_g[] = { 1 };
calcHist(&image, 1, channels_g, Mat(), greenHist, 1, hist_size, ranges, true, false);
int channels_b[] = { 0 };
calcHist(&image, 1, channels_b, Mat(), blueHist, 1, hist_size, ranges, true, false); //准备参数绘制三色直方图
double maxValue_red, maxValue_green, maxValue_blue;
minMaxLoc(redHist, 0, &maxValue_red, 0, 0);
minMaxLoc(greenHist, 0, &maxValue_green, 0, 0);
minMaxLoc(blueHist, 0, &maxValue_blue, 0, 0); int scale = 1;
int histHeight = 256;
//bins * 3 是因为要绘制三个通道,每个通道的像素取值在 0-bins
Mat histImage = Mat::zeros(histHeight, bins * 3, CV_8UC3); //开始绘制
for (int i = 0; i < bins; i++) {
float binValue_red = redHist.at<float>(i);
float binValue_green = greenHist.at<float>(i);
float binValue_blue = blueHist.at<float>(i); //计算高度时的乘除与下面绘图的 histHeight - intensity 是为了便于显示,否则有的色度很低
//要绘制的高度
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);

效果如下:

最新文章

  1. BZOJ4698: Sdoi2008 Sandy的卡片
  2. 浏览器地址栏输入一个URL后回车,将会发生的事情
  3. stopping NetworkManager daemon failed
  4. shell 删除某个目录下的重复文件
  5. spring任务计划
  6. 创建数据库时报&quot;FILESTREAM 功能被禁用&quot;
  7. Dubbo 应用容器
  8. Java集合框架学习笔记
  9. 折叠Collapse插件
  10. 数据库迁移 - SQLServer-&gt;MySQL
  11. Swing-setMaximumSize()用法-入门
  12. windows下用pip安装软件超时解决方案
  13. Angular开发技巧
  14. 判断浏览器大于等于ie9
  15. Java 泛型中的PECS原则
  16. 整合shiro出现【Correct the classpath of your application so that it contains a single, compatible version of org.quartz.Scheduler】
  17. [centos][ntp][administrator] chrony ntp
  18. django 存在则忽略, 不存在则创 TagSheet.objects.get_or_create(tag=&#39;test&#39;)
  19. CentOS7安装BugFree
  20. My latest news

热门文章

  1. C语言typedef的用法详解
  2. Real DOM和 Virtual DOM 的区别?优缺点?
  3. lucene 类介绍
  4. debian9 python环境设置
  5. Docker与k8s的恩怨情仇(六)—— “容器编排”上演“终结者”大片
  6. HCNA Routing&amp;Switching之OSPF度量值和基础配置命令总结
  7. Python基础之PyQt5关闭界面
  8. js学习笔记之正则
  9. 论文笔记:(2017NIPS)DeepSets
  10. 开发者如何快速搭建自己的电商App?