matlab直方图均衡,使用向量优化

matlab自带有histeq函数对图像进行直方图均衡

自己写了一个,改成向量化形式,效率提高了一点,但是比自带的还是差很多,差不多9倍

function D = my_histeq(I)
[m,n] = size(I);
S = zeros(1,256,'double');
% 得到0-255各个灰阶的计数
i = 1:m;j = 1:n;
S(I(i,j)+1) = S(I(i,j)+1) + 1;
% 得到各灰度阶的概率
S = S./(m*n);
% 得到概率累计分布函数
P = zeros(1,256,'double');
P(1) = S(1);
k = 2:256;
P(k) = P(k-1) + S(k);
% 投影到灰度阶 D
= zeros(m,n,'uint8'); D(i,j) = P(I(i,j)+1)*255+0.5;

原来使用for循环的

function D = my_histeq(I)
[m,n] = size(I);
S = zeros(1,256,'double');
% 得到0-255各个灰阶的计数
for i = 1:m
     for j = 1:n
         S(I(i,j)+1) = S(I(i,j)+1) + 1;  % 注意下标
     end
end
% 得到各灰度阶的概率
S = S./(m*n);
% 得到概率累计分布函数
P = zeros(1,256,'double');
for i = 1:256
     if i==1
         P(i) = S(i);
     else
         P(i) = P(i-1) + S(i);
     end
end
D
= zeros(m,n,'uint8'); for i = 1:m for j=1:n D(i,j) = P(I(i,j)+1)*255+0.5; end end

 

posted @ 2016-11-17 21:29  柳如风  阅读(286)  评论(0编辑  收藏  举报
https://www.vultr.com/?ref=7677570