码迷,mamicode.com
首页 > 其他好文 > 详细

opencv直方图均衡化

时间:2015-03-30 22:48:37      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include "highgui.h"
#include "cv.h"
#include "cxcore.h"
#include "math.h"

using namespace std;
using namespace cv;


//绘制1维直方图
Mat draw1DHistogram(Mat histogramMat) {
	double maxVal = 0, minVal = 0;
	minMaxLoc(histogramMat, &minVal, &maxVal, 0, 0);
	Mat histImage(histogramMat.rows, histogramMat.rows, CV_8U, Scalar(255));
	int hpt = static_cast<int>(0.9 * histogramMat.rows);
	for (int h = 0; h < histogramMat.rows; h++) {
		float binVal = histogramMat.at<float>(h);
		int intensity = static_cast<int>((binVal / maxVal) * hpt);
		line(histImage, Point(h, histogramMat.rows - 1),
				Point(h, histogramMat.rows - 1 - intensity), Scalar::all(0));
	}
	return histImage;
}


//一维直方图计算(采用实际图像) 实验2
void get1DHistogramExperiment2(Mat& image) {
	//计算直方图 使用的图片数量
	int nImageArrays = 1;
	//使用的直方图数组
	Mat* imageArrays = new Mat[nImageArrays];
	//加载实际图像
//	Mat image = imread("e:\\citywall1.bmp", 0);
	if (image.data == NULL) {
		printf("加载图像失败\n");
		return;
	}
	imageArrays[0] = image;
	//直方图的维数
	const int dims = 1;
	//在图像的通道序列中 本次直方图计算使用了哪些通道,本代码中使用了编号为0的通道
	int channels[dims] = { 0 };
	//直方图中每一维上的bin数,本代码是创建一维直方图 并且 分为256个bin
	int histBins[dims] = { 256 };
	//保存直方图的结果 CV_32F,dims说明矩阵的维度,histBins说明矩阵每一维上的大小
	Mat histND(dims, histBins, CV_32F, Scalar::all(0));
	//手动指定各个bin的取值范围
	//float image1Range[5]={0.0,50.0,200.0,220.0,256.0};
	//统一分割,只需要指定bin[0]的下限值和bin[histBins[dims-1]-1]的上限值即可
	float image1Range[5] = { 0.0, 256.0 };
	//各个通道的 bin划分规则
	const float* allRanges[dims] = { image1Range };
	//直方图计算
	calcHist(imageArrays, nImageArrays, channels, Mat(), histND, dims, histBins,
			allRanges, true);
	//绘制直方图
	Mat histImage = draw1DHistogram(histND);
	//显示直方图
	namedWindow("hist");
	imshow("hist", histImage);
	waitKey(0);
}
/**
 * 直方图均衡
 */
void HistogramEqual(Mat& src){
	Mat dst;
	equalizeHist(src,dst);   //直方图均衡化
	get1DHistogramExperiment2(dst);
	namedWindow("equal");
	imshow("equal",dst);
	waitKey(0);
}
int main() {
	Mat image = imread("e:\\test.bmp", CV_LOAD_IMAGE_GRAYSCALE);
	namedWindow("src");
	imshow("src",image);
	get1DHistogramExperiment2(image);
	HistogramEqual(image);


	return 0;
}

 

opencv直方图均衡化

标签:

原文地址:http://www.cnblogs.com/kakaxisir/p/4379362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!