opencv中图像直方图常见操作之直方图均衡化及直方图计算(一)

图像直方图常见操作(二):https://blog.csdn.net/qq_37120369/article/details/104802821

图像直方图常见操作(三):https://blog.csdn.net/qq_37120369/article/details/104821396

一、直方图均衡化

1.直方图概念:

图像直方图,是指对整个图像在灰度范围内的像素值(0~255)统计出现频率次数,据此生成的直方图,称为图像直方图。

2.特征:

直方图反映了图像灰度的分布情况,是图像的统计学特征。

3.图像均衡化:

图像均衡化是一种提高图像对比度的方法,拉伸图像灰度值范围。

4.API:

equalizeHist(

InputArray src,//输入图像,必须是8-bit的单通道图像

OutputArray dst// 输出结果

)

二、直方图计算

1.相关API:

split( 

const Mat &src, //输入图像

Mat* mvbegin   // 输出的通道图像数组

//API作用:把多通道图像分为多个单通道图像

calcHist( 

const Mat* images,//输入图像指针

int images,// 图像数目

const int* channels,// 需要统计直方图的第几通道

InputArray mask,// 掩膜,计算整个图像,传入None就行,如果要对局部图像做处理,可以定义个mask传入

OutputArray hist,//输出的直方图数据

int dims,// 维数,对于灰度图像来说是1 

const int* histsize,// 每个维度中Bin(柱子)的个数

const float* ranges,//每个维度中像素范围,一般是[0,256]

bool uniform,// true by default ,直方图是否均匀

bool accumulate// false by defaut,在多个图像时,是否累计计算像素值得个数

)//API作用:计算每个像素块在图像中的数量(之所以是像素块,因为可以传入Bin改变一块中像素值个数)

2.例程:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
	Mat src = imread("C:\\Users\\Administrator\\Pictures\\zclgq.jpg");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	char INPUT_T[] = "input image";
	char OUTPUT_T[] = "histogram demo";
	namedWindow(INPUT_T, CV_WINDOW_AUTOSIZE);
	namedWindow(OUTPUT_T, CV_WINDOW_AUTOSIZE);
	imshow(INPUT_T, src);

	// 分通道显示
	vector<Mat> bgr_planes;
	split(src, bgr_planes);//把多通道图像分为多个单通道图像
	//imshow("channel 0", bgr_planes[0]);
    //imshow("channel 1", bgr_planes[1]);
    //imshow("channel 2", bgr_planes[2]);
	// 计算直方图
	int histSize = 256;
	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;
	Mat histImage(hist_w, hist_h, CV_8UC3, Scalar(0, 0, 0));
	normalize(b_hist, b_hist, 0, hist_h, NORM_MINMAX, -1, Mat());//为了能够全部在图片上显示
	//cout<<b_hist.rows<<","<<b_hist.cols<<endl;
	normalize(g_hist, g_hist, 0, hist_h, NORM_MINMAX, -1, Mat());
	normalize(r_hist, r_hist, 0, hist_h, NORM_MINMAX, -1, Mat());

	// render histogram chart
	for (int i = 1; i < histSize; i++) {
		line(histImage, Point((i - 1)*bin_w, hist_h - cvRound(b_hist.at<float>(i - 1))), 
			Point((i)*bin_w, hist_h - cvRound(b_hist.at<float>(i))), Scalar(255, 0, 0), 2, LINE_AA);
		//cvRound():返回跟参数最接近的整数值,即四舍五入
		line(histImage, Point((i - 1)*bin_w, hist_h - cvRound(g_hist.at<float>(i - 1))),
			Point((i)*bin_w, hist_h - cvRound(g_hist.at<float>(i))), Scalar(0, 255, 0), 2, LINE_AA);

		line(histImage, Point((i - 1)*bin_w, hist_h - cvRound(r_hist.at<float>(i - 1))),
			Point((i)*bin_w, hist_h - cvRound(r_hist.at<float>(i))), Scalar(0, 0, 255), 2, LINE_AA);
	}
	imshow(OUTPUT_T, histImage);

	waitKey(0);
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rookie Linux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值