简体   繁体   中英

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.

在此处输入图片说明

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],:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM