直方图均衡化作用:提高图像对比度,拉伸灰度值范围。
代码示例
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include
using namespace cv;
using namespace std;
int main(int, char** argv)
{
Mat src, dst;
char* source_window = "Source Image";
char* equal_window = "Equalization Image";
src = imread("../data/source.jpg");
if (src.empty()) { return -1; };
cvtColor(src, src, COLOR_BGR2GRAY); // 灰度图像
equalizeHist(src, dst); // 直方图均衡化
imshow(source_window, src);
imshow(equal_window, dst);
waitKey(0);
return 0;
}
运行结果
void cv::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
)
说明:
src:原图像 nimages:原图像数量 channels:通道
mask:掩模 hist:输出直方图 dims:直方图维度
histSize:直方图每一维的个数 ranges:直方图每一维的范围
uniform:true则直方图范围尺寸均匀分布 accumulate:false则调用该函数前直方图先清零
代码示例
#include "opencv2/highgui.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include
using namespace std;
using namespace cv;
int main( )
{
Mat src, dst;
src = imread("../data/lena.jpg");
if (src.empty()) { return -1; }
vector bgr_planes; // 矩阵向量
split(src, bgr_planes); // 将多通道图像分为多个单通道图像
int histSize = 256;
float range[] = { 0,256 };
const float* histRange = { range }; // 必须加const,否则报错
bool uniform = true; // 直方图是否分布均匀
bool accumulate = false; // 如果为true,在开始分配时直方图不会被清除
Mat b_hist, g_hist, r_hist;
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);
int hist_w = 512, hist_h = 400; // 直方图的横纵范围
int bin_w = cvRound((double)hist_w / histSize); // 每个柱状的宽度
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));
normalize(b_hist, b_hist, 9, 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), 2, 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(0, 255, 0), 2, 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(0, 0, 255), 2, 8, 0);
}
imshow("calcHist Demo", histImage);
waitKey(0);
return 0;
}
运行结果
double cv::compareHist ( InputArray H1,
InputArray H2,
int method
)
说明:
method:
相关Correlation ( CV_COMP_CORREL )
卡方Correlation ( CV_COMP_CORREL )
交集Intersection ( CV_COMP_INTERSECT )
巴特查里亚距离Bhattacharyya distance ( CV_COMP_BHATTACHARYYA )
引入了函数cv::compareHist( ),代码与上面类似,在此不赘述,具体查看链接:
https://docs.opencv.org/master/d8/dc8/tutorial_histogram_comparison.html