# matlab 直方图归一化

<

div id=”content” contentScore=”3286″>直方图规定化

Z = G – 1［T（r）］

clear all;
close all;
clc;

L=256;
NK=zeros(L,1);              %存储原图像直方图数据
Rk_CDF_Normal=zeros(L,1);    %存储直方图规定化后的图像的直方图
Rk_pre=zeros(L,1);          %存储原图像累积直方图数据
[row,col]=size(I);

%%
%获取像素灰度级
for i = 1:row
for j = 1:col
num = double(I(i,j))+1;
NK(num) = NK(num)+1;
end
end

%计算直方图概率估计
Ps = NK./numel(I);%存储原图像直方图概率数据
%计算累积直方图
for level=1:L
if level==1
Rk_pre(level)=Ps(level);
else
Rk_pre(level)=Rk_pre(level-1)+Ps(level);
end
end

%%
%规定化直方图,在这里要得到2-s图像的灰度直方图
H = rgb2gray(H);
nk_normal = zeros(L,1);
Rk_normal = zeros(L,1);
[row,col] = size(H);

%获取像素灰度级
for i = 1:row
for j = 1:col
num_normal = double(I(i,j))+1;
nk_normal(num_normal) = nk_normal(num_normal)+1;
end
end

%计算直方图概率估计
Ps_normal = nk_normal./numel(H);
for level=1:L
if level==1
Rk_normal(level)=Ps_normal(level);
else
Rk_normal(level)=Rk_normal(level-1)+Ps_normal(level);
end
end

%计算规定化累积直方图
for level=1:L
if level==1
Rk_CDF_Normal(level)=Rk_normal(level);
else
Rk_CDF_Normal(level)=Rk_CDF_Normal(level-1)+Rk_normal(level);
end
end

%%
%计算原图像与目标图像累计直方图数值的差的绝对值
double ScMin=zeros(L,L);
for y=1:L
for x=1:L
ScMin(x,y)=abs(Rk_pre(y)’-Rk_CDF_Normal(x)’);
end
end
%建立映射
HisM=zeros(L:1);
for level_x=1:L
min = 0;
minV=ScMin(1,level_x);
for level_y=1:L
if(minV>ScMin(level_y,level_x))
minV=ScMin(level_y,level_x);
min = level_y;
end
end
HisM(level_x)= min;
end

%将原图像的每个像素灰度转换为直方图均衡化后的灰度
[row,col]=size(I);
New=I;
for x = 1:row
for y = 1:col
Num = double(I(x,y))+1;
if Num==L
New(x,y)=HisM(L);
end
end
end
figure;imshow(New),title(‘规定化后图像’);

<img src=”http: