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);

}

直方图:

相关推荐
奋斗者1号几秒前
机器学习超参数优化全解析
人工智能·机器学习
暴龙胡乱写博客9 分钟前
OpenCV --- 图像预处理(六)
人工智能·opencv·计算机视觉
IT杨秀才14 分钟前
LangChain框架入门系列(3):数据连接
人工智能·后端·langchain
IT杨秀才15 分钟前
LangChain框架入门系列(2):Hello LangChain
人工智能·后端·langchain
易安说AI16 分钟前
全网精选!14个爆火MCP资源库,让你的AI Agent能力翻倍!
人工智能
安步当歌17 分钟前
【目标检测】对YOLO系列发展的简单理解
图像处理·yolo·目标检测·计算机视觉
KuaFuAI18 分钟前
DeepSeek开源引爆AI Agent革命:应用生态迎来“安卓时刻”
人工智能·开源·agent·deepseek
luoganttcc19 分钟前
开源的自动驾驶模拟器
人工智能·开源·自动驾驶
何双新20 分钟前
L1-7、Prompt 的“调试技巧”
人工智能·prompt
何双新25 分钟前
L1-4、如何写出清晰有目标的 Prompt
大数据·人工智能·prompt