opencv:绘制图像直方图

#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);
}
上一篇:OpenCV绘制图像中RGB三个通道的直方图


下一篇:【图像处理】基于OpenCV实现图像直方图的原理