直方图(histogram):当图像被定义为一种数据类型,并且能够访问该图像的灰度值(像素),从而得到不同灰度的概率密度函数,这种图像就称作直方图
对直方图进行改变,从而改变图像的对比度,这种处理叫做直方图的均衡化(hisotgram equalization)
openCV中使用calcHist来计算图像直方图
void calcHist( const Mat* images, int nimages ,
const int * channels, InputArray mask ,
OutputArray hist , int dims, const int* histSize,
const float ** ranges, bool uniform = true, bool accumulate = false );
参数 | 定义 | 解释 |
const Mat* images
|
@param images Source arrays. They all should have the same depth, CV_8U or CV_32F , and the same
size. Each of them can have an arbitrary number of channels.
| 集合中第一个图像的地址,用来处理一批图像 |
int nimages
| @param nimages Number of source images. | 原图像的数量 |
const int * channels
|
@param channels List of the dims channels used to compute the histogram. The first array channels
are numerated from 0 to images[0].channels()-1 , the second array channels are counted from
images[0].channels() to images[0].channels() + images[1].channels()-1, and so on.
| 直方图的通道列表,从0~2 |
InputArray mask
|
@param mask Optional mask. If the matrix is not empty, it must be an 8-bit array of the same size
as images[i] . The non-zero mask elements mark the array elements counted in the histogram.
| 可选项mask,用来指示图像中像素的个数 |
OutputArray hist
|
@param hist Output histogram, which is a dense or sparse dims -dimensional array.
| 输出直方图 |
int dims
|
@param dims Histogram dimensionality that must be positive and not greater than CV_MAX_DIMS
(equal to 32 in the current OpenCV version).
| 指示直方图的维数 |
const int* histSize
| @param histSize Array of histogram sizes in each dimension. | 每一个维度上直方图的大小的数组 |
const float ** ranges
|
@param ranges Array of the dims arrays of the histogram bin boundaries in each dimension. When the
histogram is uniform ( uniform =true), then for each dimension i it is enough to specify the lower
(inclusive) boundary \f$L_0\f$ of the 0-th histogram bin and the upper (exclusive) boundary
\f$U_{\texttt{histSize}[i]-1}\f$ for the last histogram bin histSize[i]-1 . That is, in case of a
uniform histogram each of ranges[i] is an array of 2 elements. When the histogram is not uniform (
uniform=false ), then each of ranges[i] contains histSize[i]+1 elements:
\f$L_0, U_0=L_1, U_1=L_2, ..., U_{\texttt{histSize[i]}-2}=L_{\texttt{histSize[i]}-1}, U_{\texttt{histSize[i]}-1}\f$
. The array elements, that are not between \f$L_0\f$ and \f$U_{\texttt{histSize[i]}-1}\f$ , are not
counted in the histogram.
| 每一维度上直方图bin边界维度数组的数组 |
bool uniform = true
| @param uniform Flag indicating whether the histogram is uniform or not (see above). | 默认为真,表示直方图是均匀分布的 |
bool accumulate = false
|
@param accumulate Accumulation flag. If it is set, the histogram is not cleared in the beginning
when it is allocated. This feature enables you to compute a single histogram from several sets of
arrays, or to update the histogram in time.
| 默认为假,表示直方图默认是不累加的 |
直方图均衡化
void equalizeHist( InputArray src, OutputArray dst );
第一个参数是输入图像,第二个输出直方图均衡化后的图像
若要计算多幅图像的直方图,来比较或者计算出这些图像的联合直方图,可以使用
double compareHist( InputArray H1, InputArray H2, int method );
method用来计算两个直方图的匹配情况,opencv中提供了6种方法
HistCompMethods {
/** Correlation
/** Chi-Square
/** Intersection
/** Bhattacharyya distance
/** Alternative Chi-Square
/** Kullback-Leibler divergence
};
示例1:使用
equalizeHist( InputArray src, OutputArray dst )对一幅彩色图形进行均衡化并且显示每个通道的直方图
其也可以用来计算同一幅彩色图像不同通道的直方图
#include<iostream>
#include <stdio.h>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#define HIST_SIZE 255
#define HIST_W 512
#define HIST_H 400
using namespace std;
using namespace cv;
//计算RGB图像的颜色通道直方图
bool histogramcalculation(const Mat &Image,Mat &histImage);
int main()
{
Mat src, imageq;
Mat histImage;
//读取原始图像
src = imread("dog.png");
if (!src.data)
{
printf("Error! Can not load image!\n");
exit(1);
}
//将图像分为三个部分B,G,R
vector<Mat> bgr_planes;
split(src, bgr_planes);
//显示结果
imshow("源图像", src);
//计算原始图像的每个通道的直方图
histogramcalculation(src, histImage);
//显示每个颜色通道的直方图
imshow("彩色图像的直方图", histImage);
//均衡化图像
//直方图均衡化应用于每个通道
equalizeHist(bgr_planes[0], bgr_planes[0]);
equalizeHist(bgr_planes[1], bgr_planes[1]);
equalizeHist(bgr_planes[2], bgr_planes[2]);
//将这些均衡化的图像合并得到其均衡化图像
merge(bgr_planes, imageq);
//显示均衡化图像
imshow("均衡化图像", imageq);
//计算每个均衡化图像通道的直方图
histogramcalculation(imageq, histImage);
//显示均衡化图像的直方图
imshow("均衡化图像的直方图", histImage);
waitKey();
return 0;
}
bool histogramcalculation(const Mat &Image, Mat &histoImage)
{
bool bRtr = false;
do
{
int histSize = HIST_SIZE;
//对(B,G,R)设置范围
float range[] = { 0,256 };
const float* histRange = { range };
bool uniform = true;
bool accumulate = false;
Mat b_hist, g_hist, r_hist;
vector<Mat> bgr_planes;
split(Image, bgr_planes);
//计算各个直方图
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate);
//为B,G,R绘制直方图
int hist_w = HIST_W;
int hist_h = HIST_H;
int bin_w = cvRound((double)hist_w / histSize);
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
//将结果归一化为[ 0, histImage.rows ]
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 < histSize; 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), 8, 0);
//得到每一个举行条的左上方和右下方的点的坐标,从而绘制直方图
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(g_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h - cvRound(g_hist.at<float>(i))), Scalar(255, 0, 0), 8, 0);
line(histImage, Point(bin_w*(i - 1), hist_h - cvRound(r_hist.at<float>(i - 1))),
Point(bin_w*(i), hist_h - cvRound(r_hist.at<float>(i))), Scalar(255, 0, 0), 8, 0);
}
histoImage = histImage;
bRtr = true;
} while (false);
return bRtr;
}