代码拉取完成,页面将自动刷新
#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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。