NKN
NKN

Reputation: 6434

Reduce the calculation time for the matlab code

To calculate an enhancement function for an input image I have written the following piece of code:

Ig = rgb2gray(imread('test.png'));
N = numel(Ig);
meanTotal = mean2(Ig);
[row,cal] = size(Ig);

IgTransformed = Ig;
n = 3;
a = 1;
b = 1;
c = 1;
k = 1;
for ii=2:row-1
    for jj=2:cal-1
        window = Ig(ii-1:ii+1,jj-1:jj+1);
        IgTransformed(ii,jj) = ((k*meanTotal)/(std2(window) + b))*abs(Ig(ii,jj)-c*mean2(window)) + mean2(window).^a;
    end
end

How can I reduce the calculation time? Obviously, one of the factors is the small window (3x3) that should be made in the loop each time.

enter image description here

Upvotes: 1

Views: 66

Answers (1)

Divakar
Divakar

Reputation: 221754

Here you go -

Igd = double(Ig);
std2v = colfilt(Igd, [3 3], 'sliding', @std);
mean2v = conv2(Igd,ones(3),'same')/9;
Ig_out = uint8((k*meanTotal)./(std2v + b).*abs(Igd-cal*mean2v) + mean2v.^a);

This will change the boundary elements too, which if not desired could be set back to the original ones with few additional steps, like so -

Ig_out(:,[1 end]) = Ig(:,[1 end])
Ig_out([1 end],:) = Ig([1 end],:)

Upvotes: 2

Related Questions