matlab自带有histeq函数对图像进行直方图均衡
自己写了一个,改成向量化形式,效率提高了一点,但是比自带的还是差很多,差不多9倍
function D = my_histeq(I)
[m,n] = size(I);
S = zeros(,,'double');
% 得到0-各个灰阶的计数
i = :m;j = :n;
S(I(i,j)+) = S(I(i,j)+) + ;
% 得到各灰度阶的概率
S = S./(m*n);
% 得到概率累计分布函数
P = zeros(,,'double');
P() = S();
k = :;
P(k) = P(k-) + S(k);
% 投影到灰度阶
D = zeros(m,n,'uint8');
D(i,j) = P(I(i,j)+)*+0.5;
原来使用for循环的
function D = my_histeq(I)
[m,n] = size(I);
S = zeros(,,'double');
% 得到0-各个灰阶的计数
for i = :m
for j = :n
S(I(i,j)+) = S(I(i,j)+) + ; % 注意下标
end
end
% 得到各灰度阶的概率
S = S./(m*n);
% 得到概率累计分布函数
P = zeros(,,'double');
for i = :
if i==
P(i) = S(i);
else
P(i) = P(i-) + S(i);
end
end
D = zeros(m,n,'uint8');
for i = :m
for j=:n
D(i,j) = P(I(i,j)+)*+0.5;
end
end