【MATLAB】直方图均衡化Matlab实现(非histeq内置函数)

直方图均衡化Matlab实现(非histeq内置函数)

直方图均衡化常用于图像增强,可以提高图像对比度,扩展图像动态范围。
算法步骤:

  1. 统计直方图
  2. 累计直方图
  3. 映射
close all
clear all
clc

srcImage = imread('lena.jpg');
srcImage = rgb2gray(srcImage);
grayImage = srcImage;
[height,width] = size(grayImage);

% 进行像素灰度统计
NumPixel = zeros(1,256);
for i = 1:height
    for j = 1:width
        NumPixel(grayImage(i,j)+1) = NumPixel(grayImage(i,j)+1)+1;
    end
end

% 计算灰度分布密度
ProbPixel = zeros(1,256);
for i = 1:256
    % 像素总数:512*512
    ProbPixel(i) = NumPixel(i) / (height * width * 1.0);
end

% 计算累计直方图
CumuPixel = zeros(1,256);
for i = 1:256
    if i == 1
        CumuPixel(i) = ProbPixel(i);
    else
        CumuPixel(i) = CumuPixel(i-1)+ProbPixel(i); 
    end
end

% 累计分布取整
CumuPixel2 = uint8(255 .* CumuPixel + 0.5);

% 对灰度值进行映射(均衡化)
for i = 1:height
    for j = 1:width
       grayImage(i,j) = CumuPixel2(grayImage(i,j)+1);
    end
end

figure();
subplot(2,2,1),imshow(srcImage);
subplot(2,2,2),imshow(grayImage);
subplot(2,2,3),imhist(srcImage);
subplot(2,2,4),imhist(grayImage);

效果图:
直方图均衡化

猜你喜欢

转载自blog.csdn.net/weixin_45355387/article/details/118102979
今日推荐