opencv_19_图像直方图

1)void histogram_demo(Mat& image);

2)void ColorInvert::histogram_demo(Mat& image) {

std::vector<Mat>bgr_plane;

split(image, bgr_plane);

const int channels[1] = { 0 };

const int bins[1] = { 256 };

float hranges[2] = { 0,255 };

const float* ranges[1] = { hranges };

Mat b_hist;

Mat g_hist;

Mat r_hist;

calcHist(&bgr_plane[0], 1, 0, Mat(), b_hist, 1, bins, ranges);

calcHist(&bgr_plane[1], 1, 0, Mat(), g_hist, 1, bins, ranges);

calcHist(&bgr_plane[2], 1, 0, Mat(), r_hist, 1, bins, ranges);

int hist_w = 512;

int hist_h = 400;

int bin_w = cvRound((double)hist_w / bins[0]);

Mat histImage = Mat::zeros(hist_h, hist_w, CV_8UC3);

normalize(b_hist, b_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

normalize(g_hist, g_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

normalize(r_hist, r_hist, 0, histImage.rows, NORM_MINMAX, -1, Mat());

for (int i = 1; i < bins[0]; i++)

{

line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))), Point(bin_w * (i),hist_h-cvRound(b_hist.at<float>(i))),Scalar(255,0,0),2,8,0);

line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))), Point(bin_w * (i), hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2, 8, 0);

line(histImage, Point(bin_w * (i - 1), hist_h - cvRound(b_hist.at<float>(i - 1))), Point(bin_w * (i), hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2, 8, 0);

}

namedWindow("Histogram Demo", WINDOW_AUTOSIZE);

imshow("Histogram Demo", histImage);

}

直方图:

相关推荐
大博士.J3 小时前
MySQL实现全量同步和增量同步到SQL Server或其他关系型库
数据仓库·人工智能·python·mysql·adb
说私域4 小时前
技术革命、需求升级与商业生态迭代——基于开源AI大模型与智能商业范式的创新研究
人工智能·微信·小程序·开源·零售
Lichenpar4 小时前
AI小白的第七天:必要的数学知识(四)
人工智能·概率论·概率分布
訾博ZiBo4 小时前
AI日报 - 2025年3月21日
人工智能
LitchiCheng6 小时前
DQN 玩 2048 实战|第二期!设计 ε 贪心策略神经网络,简单训练一下吧!
人工智能·深度学习·神经网络
tortorish6 小时前
PyTorch中Batch Normalization1d的实现与手动验证
人工智能·pytorch·batch
wwwzhouhui6 小时前
dify案例分享-儿童故事绘本语音播报视频工作流
人工智能·音视频·语音识别
南太湖小蚂蚁6 小时前
自然语言处理入门4——RNN
人工智能·rnn·深度学习·自然语言处理
Ronin-Lotus6 小时前
深度学习篇---分类任务图像预处理&模型训练
人工智能·python·深度学习·机器学习·分类·模型训练·分类任务
四口鲸鱼爱吃盐6 小时前
CVPR2025 | TAPT:用于视觉语言模型鲁棒推理的测试时对抗提示调整
网络·人工智能·深度学习·机器学习·语言模型·自然语言处理·对抗样本