直方图均衡化Matlab实现(非histeq内置函数)
直方图均衡化常用于图像增强,可以提高图像对比度,扩展图像动态范围。
算法步骤:
统计直方图累计直方图映射
close allclear allclcsrcImage = imread('lena.jpg');srcImage = rgb2gray(srcImage);grayImage = srcImage;[height,width] = size(grayImage);% 进行像素灰度统计NumPixel = zeros(1,256);for i = 1:heightfor j = 1:widthNumPixel(grayImage(i,j)+1) = NumPixel(grayImage(i,j)+1)+1;endend% 计算灰度分布密度ProbPixel = zeros(1,256);for i = 1:256% 像素总数:512*512ProbPixel(i) = NumPixel(i) / (height * width * 1.0);end% 计算累计直方图CumuPixel = zeros(1,256);for i = 1:256if i == 1CumuPixel(i) = ProbPixel(i);elseCumuPixel(i) = CumuPixel(i-1)+ProbPixel(i); endend% 累计分布取整CumuPixel2 = uint8(255 .* CumuPixel + 0.5);% 对灰度值进行映射(均衡化)for i = 1:heightfor j = 1:widthgrayImage(i,j) = CumuPixel2(grayImage(i,j)+1);endendfigure();subplot(2,2,1),imshow(srcImage);subplot(2,2,2),imshow(grayImage);subplot(2,2,3),imhist(srcImage);subplot(2,2,4),imhist(grayImage);
效果图:
如果觉得《【MATLAB】直方图均衡化Matlab实现(非histeq内置函数)》对你有帮助,请点赞、收藏,并留下你的观点哦!