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; % 注意下标 endend% 得到各灰度阶的概率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); endend D = zeros(m,n,'uint8');for i = 1:m for j=1:n D(i,j) = P(I(i,j)+1)*255+0.5; endend