二十三、直方图计算
1、直方图概念
- 对于图像像素值、图像梯度、每个像素的角度等一切图像的属性值,都可以建立直方图,不过基于图像像素灰度直方图是最常见的。
- 直方图常见的几个属性:
dims
表示维度,对灰度图像来说只有一个通道值dims=1
bins
表示在维度中子区域大小划分,bins=256
代表划分为256个级别
range
表示值的范围,灰度范围为0~255之间
2、API学习
split
将三通道的图像分为三个单通道的图像,存放在单通道图像数组中
split(
const Mat &src,//输入图像
Mat* mvbegin//输出单通道图像数组
)
calcHist
直方图计算
计算直方图,获得每一个通道每一个像素值对应的频次,存放在对应的数组中
calcHist(
const Mat* images,//输入图像指针
int images,//图像数目
const int* channels,//通道数
InputArray hist,//输出的直方图数据
int dims,//维数
const int* histsize,//直方图级数
const float* ranges,//值域范围
bool uniform,//true by default
bool accumulate//false by default
)
3、步骤
- 分通道
split
将3通道图像分为多个单通道图像存放在单通道数组中。
vector<Mat> bgr_planes;//定义一个存放图片的数组
split(src, bgr_planes);//将图片src分通道存放到bgr_planes数组中
//imshow("single channel", bgr_planes[1]);做一个小测试,输出其中一个通道的图片
- 计算直方图
calcHist
获得每一个通道每一个像素值对应的频次,存放在对应的数组中。需要对3个通道单独计算,单独存放,这样就得到了三个通道的直方图信息。
//计算直方图,获得每一个像素值对应的频次,存放在对应的数组中(b_hist,g_hist,r_hist)
int histSize = 256;//像素值的个数,bin的大小
float range[] = { 0, 256 };//像素值的范围
const float *histRanges = { range };
Mat b_hist, g_hist, r_hist;//定义存放rgb三张单通道图片的像素值频次信息的数组(直方图)
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRanges, true, false);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRanges, true, false);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRanges, true, false);
- 归一化
normalize
将像素值的频次做归一化,因为像素值的个数有可能超过图像的高度,需要把像素值的频次归一化到图像的高度范围内。
//归一化
int hist_h = 400;//输出的直方图图像的高度
int hist_w = 512;//输出的直方图图像的宽度
int bin_w = hist_w / histSize;//代表划分的每一个bin的宽度
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));//创建一个三通道直方图图像
normalize(b_hist, b_hist, 0, hist_h, NORM_MINMAX, -1, Mat());//归一化,因为像素值的个数有可能超过图像的高度,需要把像素值的频次归一化到0-400的范围内
normalize(g_hist, g_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
- 绘制直方图
line
连接直方图每一个bin的左角点,构成了折线图
for (int i = 1; i < histSize; i++) {//每次循环连接相邻两个直方图的左顶点
line(histImage, Point((i - 1)*bin_w, hist_h - round(b_hist.at<float>(i - 1))), Point((i)*bin_w, hist_h - round(b_hist.at<float>(i))), Scalar(255,0,0), 2, LINE_AA);
//第一个点为上一个直方图bin的左顶点,第二个点为本个直方图bin的左顶点
line(histImage, Point((i - 1)*bin_w, hist_h - round(g_hist.at<float>(i - 1))), Point((i)*bin_w, hist_h - round(g_hist.at<float>(i))), Scalar(0, 255, 0), 2, LINE_AA);
line(histImage, Point((i - 1)*bin_w, hist_h - round(r_hist.at<float>(i - 1))), Point((i)*bin_w, hist_h - round(r_hist.at<float>(i))), Scalar(0, 0, 255), 2, LINE_AA);
}
示例代码:(直方图计算)
#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>
using namespace cv;
using namespace std;
int main(int argc, char* argv) {
Mat src, dst;
src = imread("添加图片路径");
if (!src.data) {
cout << "could not load image..." << endl;
return -1;
}
char INPUT_WIN[] = "input image";
char OUTPUT_WIN[] = "histogram demo";
namedWindow(INPUT_WIN, WINDOW_AUTOSIZE);
namedWindow(OUTPUT_WIN, WINDOW_AUTOSIZE);
imshow(INPUT_WIN, src);
//分通道显示
vector<Mat> bgr_planes;
split(src, bgr_planes);
//计算直方图
int histSize = 256;//像素值的个数,bin的大小
float range[] = { 0, 256 };//像素值的范围
const float *histRanges = { range };
Mat b_hist, g_hist, r_hist;
calcHist(&bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRanges, true, false);
calcHist(&bgr_planes[1], 1, 0, Mat(), g_hist, 1, &histSize, &histRanges, true, false);
calcHist(&bgr_planes[2], 1, 0, Mat(), r_hist, 1, &histSize, &histRanges, true, false);
//归一化
int hist_h = 400;//输出的直方图图像的高度
int hist_w = 512;//输出的直方图图像的宽度
int bin_w = hist_w / histSize;//代表划分的每一个bin的宽度
Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));//创建一个三通道直方图图像
normalize(b_hist, b_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
normalize(g_hist, g_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
normalize(r_hist, r_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
//绘制直方图
for (int i = 1; i < histSize; i++) {//每次循环连接相邻两个直方图的左顶点
line(histImage, Point((i - 1)*bin_w, hist_h - round(b_hist.at<float>(i - 1))), Point((i)*bin_w, hist_h - round(b_hist.at<float>(i))), Scalar(255,0,0), 2, LINE_AA);
line(histImage, Point((i - 1)*bin_w, hist_h - round(g_hist.at<float>(i - 1))), Point((i)*bin_w, hist_h - round(g_hist.at<float>(i))), Scalar(0,255,0), 2, LINE_AA);
line(histImage, Point((i - 1)*bin_w, hist_h - round(r_hist.at<float>(i - 1))), Point((i)*bin_w, hist_h - round(r_hist.at<float>(i))), Scalar(0,0,255), 2, LINE_AA);
}
imshow(OUTPUT_WIN, histImage);
waitKey(0);
return 0;
}
输出结果显示: