#include <opencv2/opencv.hpp>
#include <iostream> using namespace cv;
using namespace std; int main(int argc, char** argv)
{
Mat src = imread("f:/images/butterfly.jpg");
if (src.empty())
{
printf("Could not find the image!\n");
return -1;
} namedWindow("input", WINDOW_AUTOSIZE);
imshow("input", src); vector<Mat> mv;
split(src, mv); // 计算直方图
int histSize = 256;
float range[] = { 0, 255 };
const float* histRanges = { range };
Mat b_hist, g_hist, r_hist;
calcHist(&mv[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRanges, true, false);
calcHist(&mv[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRanges, true, false);
calcHist(&mv[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRanges, true, false); Mat result = Mat::zeros(Size(600, 400), CV_8UC3);
int margin = 50;
int maxValue = result.rows - 2 * margin;
// 归一化
normalize(b_hist, b_hist, 0, maxValue, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, maxValue, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, maxValue, NORM_MINMAX, -1, Mat()); float step = 500.0 / 256.0;
// 绘制直方图
for (int i = 0; i < 255; i++) {
float bh1 = b_hist.at<float>(i, 0);
float gh1 = g_hist.at<float>(i, 0);
float rh1 = r_hist.at<float>(i, 0); float bh2 = b_hist.at<float>(i + 1, 0);
float gh2 = g_hist.at<float>(i + 1, 0);
float rh2 = r_hist.at<float>(i + 1, 0); line(result,
Point(step * i + margin, maxValue + 50 - bh1),
Point(step * (i + 1) + margin, maxValue + 50 - bh2),
Scalar(255, 0, 0), 1, 8, 0);
line(result,
Point(step * i + margin, maxValue + 50 - gh1),
Point(step * (i + 1) + margin, maxValue + 50 - gh2),
Scalar(0, 255, 0), 1, 8, 0);
line(result,
Point(step * i + margin, maxValue + 50 - rh1),
Point(step * (i + 1) + margin, maxValue + 50 - rh2),
Scalar(0, 0, 255), 1, 8, 0);
} imshow("result", result); waitKey(0);
destroyAllWindows();
}

函数抽取:

void show_hist(string name, Mat src) {
vector<Mat> mv;
split(src, mv); // 计算直方图
int histSize = 256;
float range[] = { 0, 255 };
const float* histRanges = { range };
Mat b_hist, g_hist, r_hist;
calcHist(&mv[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRanges, true, false);
calcHist(&mv[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRanges, true, false);
calcHist(&mv[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRanges, true, false); Mat result = Mat::zeros(Size(600, 400), CV_8UC3);
int margin = 50;
int maxValue = result.rows - 2 * margin;
// 归一化
normalize(b_hist, b_hist, 0, maxValue, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, maxValue, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, maxValue, NORM_MINMAX, -1, Mat()); float step = 500.0 / 256.0;
// 绘制直方图
for (int i = 0; i < 255; i++) {
float bh1 = b_hist.at<float>(i, 0);
float gh1 = g_hist.at<float>(i, 0);
float rh1 = r_hist.at<float>(i, 0); float bh2 = b_hist.at<float>(i + 1, 0);
float gh2 = g_hist.at<float>(i + 1, 0);
float rh2 = r_hist.at<float>(i + 1, 0); line(result,
Point(step * i + margin, maxValue + 50 - bh1),
Point(step * (i + 1) + margin, maxValue + 50 - bh2),
Scalar(255, 0, 0), 1, 8, 0);
line(result,
Point(step * i + margin, maxValue + 50 - gh1),
Point(step * (i + 1) + margin, maxValue + 50 - gh2),
Scalar(0, 255, 0), 1, 8, 0);
line(result,
Point(step * i + margin, maxValue + 50 - rh1),
Point(step * (i + 1) + margin, maxValue + 50 - rh2),
Scalar(0, 0, 255), 1, 8, 0);
} imshow(name, result);
}

最新文章

  1. 用html5的canvas画布绘制贝塞尔曲线
  2. BZOJ 2898 模拟
  3. js 随机星星 document.createElement(); setAttribute()
  4. 能源项目xml文件 -- app-dubbo.xml
  5. 第一个Sprint冲刺第五天
  6. HTTP 简介
  7. [JavaWebService-axis]-环境搭建
  8. Visual Studio 2005 移植 - WINVER,warning C4996, error LINK1104
  9. scala目录
  10. [React Fundamentals] Component Lifecycle - Mounting Usage
  11. poj2594
  12. C# 如何从List集合当中取出子集合
  13. hadoop深入研究:(七)——压缩
  14. POJ训练计划3080_Blue Jeans(串处理/暴力)
  15. 海量数据挖掘MMDS week6: 决策树Decision Trees
  16. PAT1132: Cut Integer
  17. Python开发【笔记】:asyncio 定时器
  18. 每日scrum(4)
  19. 搭建docker管理工具(DOCKER-UI)
  20. Django—— restful 设计风格

热门文章

  1. 巨杉Tech | 微服务趋势下的数据库设计与应用简析
  2. 洛谷P1028 数的计算
  3. Consider using the `--user` option or check the permissions.
  4. yingwen
  5. Mybatis-生成逆向工程后对数据库的模糊查询详解
  6. python之pandas简介
  7. POJ3122 Pie(二分)
  8. IntelliJ IDEA 2017.3尚硅谷-----缓存和索引的清理
  9. C++内存管理(new operator/operator new/operator delete/placement new)
  10. ssh连不上的问题