1 Star 1 Fork 0

ProudRabbit/learn-opencv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
19二维直方图.cpp 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
ProudRabbit 提交于 2021-08-31 11:49 . opecv_demo
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat img = imread("./Image/4.png"); // 将一张图片以灰度图加载进来
if (img.empty()) {
cout << "图像加载失败" << endl;
return -1;
}
Mat hsv, hs_hist;
cvtColor(img, hsv, COLOR_BGR2HSV);
int hbins = 30, sbins = 32;
int hist_bins[] = { hbins,sbins };
float h_range[] = { 0,180 };
float s_range[] = { 0,256 };
const float* hs_ranges[] = { h_range,s_range };
int hs_channels[] = { 0,1 };
calcHist(&hsv, 1, hs_channels, Mat(), hs_hist, 2, hist_bins, hs_ranges, true, false);
double maxVal = 0;
minMaxLoc(hs_hist, 0, &maxVal, 0, 0);
int scale = 10;
Mat hist2d_img = Mat::zeros(sbins * scale, hbins * scale, CV_8UC3);
for (int h = 0; h < hbins; h++)
{
for (int s = 0; s < sbins; s++)
{
double binVal = hs_hist.at<float>(h, s);
int intensity = cvRound(binVal * 255 / maxVal);
rectangle(hist2d_img, Point(h * scale, s * scale),
Point((h + 1) * scale - 1, (s + 1) * scale - 1),
Scalar::all(intensity), FILLED);
}
}
namedWindow("原图", WINDOW_NORMAL);
imshow("原图", img);
// 显示直方图
namedWindow("图像直方图", WINDOW_AUTOSIZE);
applyColorMap(hist2d_img, hist2d_img, COLORMAP_JET);
imshow("图像直方图", hist2d_img);
waitKey(0); // 等待按键
destroyAllWindows(); // 摧毁所有的窗口
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/proudrabbit/learn-opencv.git
[email protected]:proudrabbit/learn-opencv.git
proudrabbit
learn-opencv
learn-opencv
master

搜索帮助