Image contrast estimation. How to speed up this code?

207 views Asked by At

I have to create a contrast map about images. The method is easy: estimate the contrast (C=SD/Mean) in a 5x5 (sliding) window. Move the window pixel by pixel trough the entire picture and save the results in a new matrix.

I created this script to solve the problem, but it is too slow. The base picture is a 1024x1024 16-bit .tif image. The process is ~40-50 sec long, but other programs solve it in less than 3 secs. How can I speed up my script?

My original script:

M=imread('image.tif');

for S=1:2 SM1_b=size(M,1); SM2_b=size(M,2);

    M(:,SM2_b+1)=M(:,SM2_b);
    M(SM1_b+1,:)=[M(SM1_b,1:SM2_b) M(SM1_b,SM2_b)];

end

M=[M(1,1:SM2_b+1);M(1,1:SM2_b+1);M]; M=[M(1:(size(M,1)),1) M(1:(size(M,1)),1) M]; clear S SM1_b SM2_b

KPic=zeros(1024);

for i=1:1024 for j=1:1024

    KPic(i,j)=std2(M(i:i+4,j:j+4))/mean2(M(i:i+4,j:j+4));
    end
end
1

There are 1 answers

0
rangerdanger On

One way could be building an image of the mean using a window size of 5x5, then a local standard deviation filter with a window size of 5x5. After this is done you can calculate the result fairly quickly.

M = imread('image.tif');
h = fspecial('average', 5);
mean_im = filter2(M, h);
std_im = stdfilt(M, 5);
KPic = std_im ./ mean_im;