赞
踩
直方图均衡化主要是为了增强图像的对比度,是直方图分布更加均匀
概念可以参考其他博客的介绍。
1.统计每一个灰度级的像素总数
2.计算每一个灰度级的概率(P(valueNumber)/piexelNumber,该灰度级像素除以像素总数)
3.累积概率,例如:p1=0.01,p2=0.01,p3=0.02,则累积概率为p1 p1+p2 p1+p2+p3;
4.将灰度值映射为: 灰度级*该灰度级累积概率值
原图:
直方图均衡化之后:
代码如下:
#include "opencv2/opencv.hpp" #include "opencv2/core.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include "iostream" using namespace cv; using namespace std; bool EqualizeHist(Mat gray, Mat result) { map<int, int>mp; for (int i = 0; i < gray.rows; i++) { uchar* ptr = (uchar*)gray.data + i * gray.cols; for (int j = 0; j < gray.cols; j++) { int value = ptr[j]; mp[value]++; } } map<int, double>valuePro; double sumPro = 0.0; int sumPixel = gray.cols*gray.rows; for (int i = 0; i < 256; i++) { sumPro += (1.0*mp[i]) / sumPixel; valuePro[i] = sumPro; } for (int i = 0; i < gray.rows; i++) { uchar* ptr1 = (uchar*)gray.data + i * gray.cols; for (int j = 0; j < gray.cols; j++) { int value = ptr1[j]; double p = valuePro[value]; result.at<uchar>(i, j) = value * p; } } return true; } int main() { Mat srcImage = imread("IMG4_MRF_focus.tif"); Mat image = imread("IMG4_MRF_focus.tif"); Mat imageRGB[3]; split(srcImage, imageRGB); //Mat image1[3]; for (int i = 0; i < 3; i++) { EqualizeHist(imageRGB[i], imageRGB[i]); } merge(imageRGB,3, srcImage); imshow("原图", image); imshow("直方图", srcImage); waitKey(); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。