怎样用matlab对一幅图像进行灰度直方图规定化处理

如题所述

%%用的是单映射规则(‘SML’ )
clear all
close all
f= imread('2.jpg');
I=double(f); [m,n]=size(I);
H=zeros(1,256);
for i=1:m
for j=1:n
H(I(i,j)+1)=H(I(i,j)+1)+1; %求各灰度级的像素数
end
end
s=zeros(1,256);t=zeros(1,256);
for i=1:256
s(i)=H(i)/(m*n); %求各灰度级的频率
% for j=1:i
% t(i)=t(i)+s(j); %求累计频率的和
%end
end

% 原始直方图的累积直方图
histc = cumsum(s);
% 规定直方图的累积直方图
J=imread('1.jpg'); h=rgb2gray(J); g=imhist(h);
matchhist=g/(m*n);
matchhistc = cumsum(matchhist);

N = length(matchhist);
M = double(intmax(class(f))) + 1;

% 映射的实现
tk = zeros(1,M); % 映射关系hist-->tk
%if strcmp(mapl,'SML') 以下为单映射规则(‘SML’)
tk(1) = 1;
linit = 2;
kinit = 0;
for k = 1:M %删除histc(k)=0的单元
if histc(k) == 0
kinit = kinit + 1;
else
break;
end
end

% 标记为SML为零的位置
indis0 = zeros(1,N);

for k = kinit:M
mappingL = zeros(size(matchhist));
% 为节省运算量而修改
for l = linit:N % 规定直方图从上一次最小处计算,节省运算时间
if matchhistc(l) == matchhistc(l - 1); %去掉相等为0的单元
continue;
end
mappingL(l) = abs(histc(k) - matchhistc(l));
if mappingL(l) == 0
indis0(linit) = 1;
end
end
mappingL(N) = abs(histc(k) - matchhistc(N));
indtemp1 = find(mappingL == 0);
indtemp2 = find(indis0 == 1);
mappingL(setdiff(indtemp1,indtemp2)) = inf;
[minmap,tk(k)] = min(mappingL(linit:N));
linit = tk(k) + linit - 1;
tk(k) = linit;
for k = 1:kinit - 1
tk(k) = tk(kinit);
end
end

%规定计算有意义

if N < M
for k = N:M
tk(k) = N;
end
end
histm = zeros(1,N);
for k = 1:M
histm(tk(k)) = histm(tk(k)) + H(k);
end
tk = tk - 1; % 方便灰度值的计算
% 图像的规定化
imagematch = zeros(size(f));
for p = 1:size(f,1)
for q = 1:size(f,2)
ind = f(p,q) + 1;
imagematch(p,q) = tk(ind);
end
end

subplot(321)
imshow(f)
title('原始图像')
subplot(322)
imshow(imagematch)
title('直方图规定化后图像')
subplot(323)
bar(H,'g');
xlabel('灰度值')
ylabel('统计个数');
title('原始直方图')
subplot(324)
bar(histm,'g');
xlabel('灰度值')
ylabel('统计个数');
title(['规定化后直方图'])
subplot(3,2,5),a=rgb2gray(f);g=histeq(a,matchhist);imshow(g);title('图像的规定化');
subplot(3,2,6),a=rgb2gray(f);g=histeq(a,matchhist);h=imhist(g);plot(h),title('图像的规定化直方图');
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-11-26
MATLAB是不是有这个函数?
相似回答