OpenCV学习笔记(七)

一、直方图的计算与绘制

1.计算直方图:calcHist()函数

calcHist()函数用于计算一个或多个阵列的直方图

void calcHist(const Mat* images, //输入的数组需为相同的深度(CV_8U或CV_32F)和相同的尺寸
int nimages,//输入数组的个数,也就是第一个参数中存放了多少张“图像”,有几个原数组
const int* channels,//需要统计的通道(dim)索引
InputArray mask,//可选的操作掩码,若不为空需为8位,且与images[i]尺寸相同
OutputArray hist,//输出的模板直方图,一个二维数组
 int dims, //需要计算的直方图的维度,一必须为正数,且不大于CV_MAX_DIMS
 const int* histSize,//存放每个维度的直方图的取值范围
 const float**ranges,//表示每一个数组的每一维的边界阵列,可以理解为每一维数值的取值范围
 bool uniform = true,//指示直方图是否均匀的标识符,默认true
 bool accumulate = false)//累计标识符,默认false

2.寻找最值:minMaxLoc()函数

void minMaxLoc(InputArray src,//输入的单通道阵列
double* minVal,//返回最小值的指针,若无返回,此值为NULL
double*maxVal = 0,//返回的最大值的指针,若无返回,为NULL
Point* minLoc = 0,//返回最小值的指针(二维情况),若无返回,此值为NULL
Point*maxLoc = 0,//返回的最大值的指针(二维情况),若无返回,为NULL
InputArray mask = noArray())//用于选择子阵列的可选掩膜

绘制H-S直方图

#include
#include
#include
using namespace cv;
using namespace std;

 
int main()
{
	Mat srcImage, hasvImage;
	srcImage = imread("1.jpg");
	if (!srcImage.data) {
		printf("读取图片失败");
		return -1;
	}
	cvtColor(srcImage, hasvImage, COLOR_BGR2HSV);
	//将色调量化为30个等级,将饱和度量化为32个等级
	int hueBinNum = 30;//色调的直方图条数量
	int saturationBinNum = 32;//饱和度的直方图直条数量
	int histSize[] = { hueBinNum, saturationBinNum };
	//定义色调的变化范围为0到179
	float hueRanges[] = { 0,180 };
	//定义饱和度的变化范围0(黑白灰)到255(纯光谱颜色)
	float saturationRanges[] = { 0,256 };
	const float* ranges[] = { hueRanges,saturationRanges };
	MatND dstHist;
	//参数准备,calcHist函数中将计算第0通道和第1通道的直方图
	int channels[] = { 0,1 };
	//正式调用calcHist函数,进行直方图计算
	calcHist(&hasvImage,//输入的数组
		1,//数组个数为1
		channels,//通道索引
		Mat(),//不使用掩膜
		dstHist,//输出目标直方图
		2,//需要计算的直方图维度为2
		histSize,//存放每个维度的直方图尺寸的数组
		ranges,//每一维数值的取值范围数组
		true,//指示直方图是否均匀的标识符,true表示均匀的直方图
		false);//累计标识符,false表示直方图在配置阶段会被置零
	//绘制直方图准备参数
	double maxValue = 0;//最大值
	minMaxLoc(dstHist, 0, &maxValue, 0, 0);//查找数组和子数组的全局最小值
	int scale = 10;
	Mat histImg = Mat::zeros(saturationBinNum*scale, hueBinNum * 10, CV_8UC3);
	//双层循环进行直方图绘制
	for(int hue = 0; hue < hueBinNum;hue++)
		for (int saturation = 0; saturation < saturationBinNum; saturation++) {
			//直方图直条的值
			float binValue = dstHist.at(hue, saturation);
			int intensity = cvRound(binValue * 255 / maxValue);//强度
			rectangle(histImg, Point(hue*scale, saturation*scale), 
				Point((hue + 1)*scale - 1, (saturation + 1)*scale - 1), Scalar::all(intensity), FILLED);

		}
	imshow("原图", srcImage);
	imshow("H-S直方图", histImg);

	waitKey(0);
	return 0;
}
 

OpenCV学习笔记(七)_第1张图片
计算并绘制图像一维直方图

#include 
#include
using namespace cv;
using namespace std;

int main()
{
	Mat srcImage = imread("1.jpg", 0);
	imshow("原图", srcImage);
	if (!srcImage.data) {
		printf( "fail to load image" );
		return -1;
	}
	//定义变量
	MatND dstHist;//在cv中用cvHistogram * hist = cvCreateHist
	int dims = 1;
	float hranges[] = { 0,255 };
	const float* ranges[] = { hranges };
	int size = 256;
	int channels = 0;
	//计算图像的直方图
	calcHist(&srcImage, 1, &channels, Mat(), dstHist, dims, &size, ranges);
	//cv中是cvCalcHist
	int scale = 1;
	Mat dstImage(size * scale, size, CV_8U, Scalar(0));
	//获取最大值和最小值
	double minValue = 0;
	double maxValue = 0;
	minMaxLoc(dstHist, &minValue, 0, 0);//在cv中用cvGetMinMaxHistValue
	//绘制直方图
	int hpt = saturate_cast(0.9 * size);
	for (int i = 0; i < 256; i++) {
		float binValue = dstHist.at(i);
		int realValue = saturate_cast(binValue * hpt / maxValue);
		rectangle(dstImage, Point(i * scale, size - 1), 
			Point((i + 1) * scale - 1,size - realValue), Scalar(255));
	}
	imshow("一维直方图", dstImage);

	waitKey(0);
	return 0;
}

OpenCV学习笔记(七)_第2张图片
绘制RGB三色直方图

#include 
#include
using namespace cv;
using namespace std;

int main()
{
	Mat srcImage = imread("1.jpg");
	imshow("原图", srcImage);
	if (!srcImage.data) {
		printf( "fail to load image" );
		return -1;
	}
	//参数准备
	int bins = 256;
	int hist_size[] = { bins };
	float range[] = { 0,256 };
	const float* ranges[] = { range };
	MatND redHist, grayHist, blueHist;
	int channels_r[] = { 0 };
	//计算图像的直方图红色部分
	calcHist(&srcImage, 1, channels_r, Mat(), //不使用掩膜
		redHist, 1, hist_size, ranges,true,false);
	//计算图像的直方图绿色部分
	int channels_g[] = { 1 };
	calcHist(&srcImage, 1, channels_g, Mat(), //不使用掩膜
		grayHist, 1, hist_size, ranges, true, false);
	//计算图像的直方图蓝色部分
	int channels_b[] = { 2 };
	calcHist(&srcImage, 1, channels_b, Mat(), //不使用掩膜
		blueHist, 1, hist_size, ranges, true, false);

	//绘制三色直方图
	double maxValue_red, maxValue_green, maxValue_blue;
	minMaxLoc(redHist, 0, &maxValue_red, 0, 0);
	minMaxLoc(grayHist, 0, &maxValue_green, 0, 0);
	minMaxLoc(blueHist, 0, &maxValue_blue, 0, 0);
	int scale = 1;
	int histHeight = 256;
	Mat histImage = Mat::zeros(histHeight, bins * 3, CV_8UC3);
	//开始绘制
	for (int i = 0; i < bins; i++) {
		//参数准备
		float binValue_red = redHist.at(i);
		float binValue_green = grayHist.at(i);
		float binValue_blue = blueHist.at(i);
		int intensity_red = cvRound(binValue_red * histHeight / maxValue_red);
		int intensity_green= cvRound(binValue_green * histHeight / maxValue_green);
		int intensity_blue = cvRound(binValue_blue * histHeight / maxValue_blue);
		//绘制红色分量
		rectangle(histImage,Point(i*scale,histHeight-1),
			Point((i+1)*scale-1,histHeight-intensity_red),Scalar(255,0,0));
		//绘制绿色分量
		rectangle(histImage, Point((i * +bins) * scale, histHeight - 1),
			Point((i + bins + 1) * scale - 1, histHeight - intensity_green), Scalar(0, 255, 0));
		//绘制蓝色分量
		rectangle(histImage, Point((i + scale*2)*scale, histHeight - 1),
			Point((i +bins*2+ 1) * scale - 1, histHeight - intensity_blue), Scalar(0, 0, 255));
	}
	imshow("图像的RGB直方图", histImage);

	waitKey(0);
	return 0;
}

OpenCV学习笔记(七)_第3张图片
直方图的对比:compareHist()函数
函数原型

//版本一
double compareHist(InputArray H1,InputArray H2,int method)
//版本二
double compareHist(const SparseMat&H1,const SparseMat&H2,int method)

直方图对比

#include 
#include
using namespace cv;
using namespace std;

int main()
{
	//声明存储基准图像和另外两张对比图像的矩阵(RGB,HSV)
	Mat srcImage_base, hsvImage_base;
	Mat srcImage_test1, hsvImage_test1;
	Mat srcImage_test2, hsvImage_test2;
	Mat hsvImage_halfDown;
	//载入基准图像(srcImage_base)和两张测试图像srcImage_test1,srcImage_test2,并显示
	srcImage_base = imread("1.jpg", 1);
	srcImage_test1 = imread("2.jpg", 1);
	srcImage_test2 = imread("3.jpg", 1);
	//显示载入的3张图像
	imshow("基准图像", srcImage_base);
	imshow("测试图像1", srcImage_test1);
	imshow("测试图像2", srcImage_test2);
	//将图像由BGR色彩空间转换到HSV色彩空间
	cvtColor(srcImage_base, hsvImage_base, COLOR_BGR2HSV);
	cvtColor(srcImage_test1, hsvImage_test1, COLOR_BGR2HSV);
	cvtColor(srcImage_test2, hsvImage_test2, COLOR_BGR2HSV);
	//创建包含基准图像下半部分的半身图像(HSV格式)
	hsvImage_halfDown = hsvImage_base(Range(hsvImage_base.rows / 2, 
		hsvImage_base.rows - 1), Range(0, hsvImage_base.cols - 1));
	//初始化计算直方图需要的实参
	//对hue通道使用30个bin,对saturation通道使用32个bin
	int h_bins = 50, s_bins = 60;
	int histSize[] = { h_bins,s_bins };
	//hue的取值范围从0到256,saturation取值范围0-180
	float h_ranges[] = { 0,256 };
	float s_ranges[] = { 0,180 };
	const float* ranges[] = { h_ranges,s_ranges };
	//使用第0和第1通道
	int channels[] = { 0,1 };
	//创建存储直方图的MatND类的实例
	MatND baseHist;
	MatND halfDownHist;
	MatND testHist1;
	MatND testHist2;
	//计算基准图像,两张测试图像,半身基准图像的HSV直方图
	calcHist(&hsvImage_base, 1, channels, Mat(), baseHist, 2, histSize, ranges, true, false);
	normalize(baseHist, baseHist, 0, 1, NORM_MINMAX, -1, Mat());
	
	calcHist(&hsvImage_halfDown, 1, channels, Mat(), halfDownHist, 2, histSize, ranges, true, false);
	normalize(halfDownHist, halfDownHist, 0, 1, NORM_MINMAX, -1, Mat());

	calcHist(&hsvImage_test1, 1, channels, Mat(), testHist1, 2, histSize, ranges, true, false);
	normalize(testHist1, testHist1, 0, 1, NORM_MINMAX, -1, Mat());

	calcHist(&hsvImage_test2, 1, channels, Mat(), testHist2, 2, histSize, ranges, true, false);
	normalize(testHist2, testHist2, 0, 1, NORM_MINMAX, -1, Mat());
	//按顺序使用4种对比标准将基准图像的直方图与其余各直方图进行对比
	for (int i = 0; i < 4; i++) {
		//进行图像直方图的对比
		int compare_method = i;
		double base_base = compareHist(baseHist, baseHist, compare_method);
		double base_half = compareHist(baseHist, halfDownHist, compare_method);
		double base_test1 = compareHist(baseHist, testHist1, compare_method);
		double base_test2 = compareHist(baseHist, testHist2, compare_method);
		//输出结果
		printf("方法[%d]的匹配结果如下:\n\n【基准图-基准图】:%f,【基准图-半身图】:%f,【基准图-测试图1】:%f,【基准图-测试图2】:%f\n===========\n",
			i,base_base,base_half,base_test1,base_test2);
	}
	printf("检测结束");
	waitKey(0);
	return 0;
}

OpenCV学习笔记(七)_第4张图片

你可能感兴趣的:(OpenCV学习笔记,OpenCV学习笔记,OpenCV,C++)