opencv:绘制图像直方图
2024-09-06 22:55:03
#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);
}
最新文章
- 用html5的canvas画布绘制贝塞尔曲线
- BZOJ 2898 模拟
- js 随机星星 document.createElement(); setAttribute()
- 能源项目xml文件 -- app-dubbo.xml
- 第一个Sprint冲刺第五天
- HTTP 简介
- [JavaWebService-axis]-环境搭建
- Visual Studio 2005 移植 - WINVER,warning C4996, error LINK1104
- scala目录
- [React Fundamentals] Component Lifecycle - Mounting Usage
- poj2594
- C# 如何从List集合当中取出子集合
- hadoop深入研究:(七)——压缩
- POJ训练计划3080_Blue Jeans(串处理/暴力)
- 海量数据挖掘MMDS week6: 决策树Decision Trees
- PAT1132: Cut Integer
- Python开发【笔记】:asyncio 定时器
- 每日scrum(4)
- 搭建docker管理工具(DOCKER-UI)
- Django—— restful 设计风格
热门文章
- 巨杉Tech | 微服务趋势下的数据库设计与应用简析
- 洛谷P1028 数的计算
- Consider using the `--user` option or check the permissions.
- yingwen
- Mybatis-生成逆向工程后对数据库的模糊查询详解
- python之pandas简介
- POJ3122 Pie(二分)
- IntelliJ IDEA 2017.3尚硅谷-----缓存和索引的清理
- C++内存管理(new operator/operator new/operator delete/placement new)
- ssh连不上的问题