直方图均衡化

直方图均衡化


clc, clear, close all

img = rgb2gray(imread('/Users/jh/Wallpapers/2.jpg'));
imshow(img);


% 计算直方图
[m, n] = size(img);

s1 = zeros(1, 256);

for i = 1:m
   for j = 1:n
       s1(img(i, j) + 1) = s1(img(i, j) + 1) + 1; % 统计图像中0-255的个数
   end
end

figure;
bar(0:255, s1); % 绘制直方图

bmap = zeros(1, 256);

for i = 1:256
    temp = 0;
    for j = 1:i
      temp = temp + s1(j); % 计算累加
    end
    bmap(i) = floor(temp * 255 / (m * n)); % 累加和 / (m * n) * 255
end

y = zeros(m, n);

for i = 1:m
   for j = 1:n
      y(i, j) = bmap(img(i, j) + 1); % 将bmap的值对应的赋给y, y为最终的图
   end
end

y = uint8(y); % 像素中不能有负数, 使用uint而不是int
figure;

imshow(y);
s1 = zeros(1, 256); 

for i = 1:m
   for j = 1:n
       s1(y(i, j) + 1) = s1(result_imgf(i, j) + 1) + 1; % 计算均值化之后的直方图
   end
end

figure;
bar(0:255, s1);
posted @ 2019-03-27 12:05  gogogo11  阅读(391)  评论(0编辑  收藏  举报