C++OpenCV绘制直方图

1.直方图概述

直方图是变量分布的统计图,他可以让我们能够了解数据的密度估计和概率分布。

2.原理理解

首先创建三个矩阵来处理每个输入图像通道。并用split来对通道分离,如下所示:

vector bgr;
split(image, bgr);

然后定义直方图的区间数:

int numbins = 256;

定义变量范围,并创建3个矩阵来存储每个直方图

float range[] = { 0, 256 };
const float* histRange = { range };
Mat b_hist, g_hist, r_hist;

使用calcHist函数计算直方图:

calcHist(&bgr[0], 1, 0, Mat(), b_hist, 1, &numbins, &histRange);
calcHist(&bgr[1], 1, 0, Mat(), g_hist, 1, &numbins, &histRange);
calcHist(&bgr[2], 1, 0, Mat(), r_hist, 1, &numbins, &histRange);

在计算每个通道直方图之后,还要绘制它并显示给用户。因此创建一个512*300像素的彩色图像:

int width = 512;
int height = 300;
Mat histImage(height, width, CV_8UC3, Scalar(20, 20, 20));
	

在直方图绘制到图像之前,我们会在最小值0和最大值之间标准化直方图矩阵,最大值与输出直方图图像的高度相同:

//将图像直方图的高度标准化为与输出直方图的高度一样
normalize(b_hist, b_hist, 0, height, NORM_MINMAX);
normalize(g_hist, g_hist, 0, height, NORM_MINMAX);
normalize(r_hist, r_hist, 0, height, NORM_MINMAX);

现在,我们从区间0-1绘制一条线,以此类推。之后,计算有多少像素在每个区间之间,然后通过将宽度除以区间数来计算binStep变量。从水平位置i-1到i绘制每条小线,垂直位置是相应的i中的直方图值,并使用彩色通道绘制:

int binStep = cvRound((float)width / (float)numbins);
for (int i = 1; i < numbins; i++)
{
    line(histImage, Point(binStep * (i - 1), height - cvRound(b_hist.at(i - 1))),
		Point(binStep * (i), height - cvRound(b_hist.at(i))),
		Scalar(255, 0, 0));
	line(histImage, Point(binStep * (i - 1), height - cvRound(g_hist.at(i - 1))),
		Point(binStep * (i), height - cvRound(g_hist.at(i))),
		Scalar(0, 255, 0));
	line(histImage, Point(binStep * (i - 1), height - cvRound(r_hist.at(i - 1))),
		Point(binStep * (i), height - cvRound(r_hist.at(i))),
		Scalar(0, 0, 255));
}

3.完整代码

myApi.h文件

#pragma once
#include
using namespace cv;
using namespace std;

class myFun
{
public:
	Mat drawHist(Mat &image);
};

myApi.cpp文件

#include"myApi.h"

Mat myFun::drawHist(Mat& image)
{
	vector bgr;
	split(image, bgr);
	//imshow("b", bgr[0]);
	int numbins = 256;
	float range[] = { 0, 256 };
	const float* histRange = { range };
	Mat b_hist, g_hist, r_hist;
	calcHist(&bgr[0], 1, 0, Mat(), b_hist, 1, &numbins, &histRange);
	calcHist(&bgr[1], 1, 0, Mat(), g_hist, 1, &numbins, &histRange);
	calcHist(&bgr[2], 1, 0, Mat(), r_hist, 1, &numbins, &histRange);

	int width = 512;
	int height = 300;
	Mat histImage(height, width, CV_8UC3, Scalar(20, 20, 20));
	
	//将图像直方图的高度标准化为与输出直方图的高度一样
	normalize(b_hist, b_hist, 0, height, NORM_MINMAX);
	normalize(g_hist, g_hist, 0, height, NORM_MINMAX);
	normalize(r_hist, r_hist, 0, height, NORM_MINMAX);

	int binStep = cvRound((float)width / (float)numbins);
	for (int i = 1; i < numbins; i++)
	{
		line(histImage, Point(binStep * (i - 1), height - cvRound(b_hist.at(i - 1))),
			Point(binStep * (i), height - cvRound(b_hist.at(i))),
			Scalar(255, 0, 0));
		line(histImage, Point(binStep * (i - 1), height - cvRound(g_hist.at(i - 1))),
			Point(binStep * (i), height - cvRound(g_hist.at(i))),
			Scalar(0, 255, 0));
		line(histImage, Point(binStep * (i - 1), height - cvRound(r_hist.at(i - 1))),
			Point(binStep * (i), height - cvRound(r_hist.at(i))),
			Scalar(0, 0, 255));
	}
	//imshow("histImage", histImage);
	return histImage;
}

主函数main.cpp文件

#include
#include
#include"myApi.h"

using namespace cv;
using namespace std;


int main(int argc, char** argv)
{
	Mat src = imread("F:/testImage/test.png");
	if (!src.data)
	{
		cout << "图片没有找到" << endl;
		return -1;
	}
	imshow("src", src);
	myFun mf;
	Mat hist = mf.drawHist(src);
	imshow("histImage", hist);

	waitKey(0);
	return 0;
}

4.实验结果

C++OpenCV绘制直方图_第1张图片

 C++OpenCV绘制直方图_第2张图片

 

你可能感兴趣的:(c++opencv图像处理,c++,人工智能,opencv)