How can I make an algorithm with loops to run faster?
1 view (last 30 days)
Show older comments
Prasanna Royyuru
on 21 Mar 2014
Commented: Prasanna Royyuru
on 23 Mar 2014
Hi,
I am developing an image reconstruction algorithm by taking in 2 copies of the same image in order to generate a higher quality image. I am using the concept of deblocking for the same.
I have a function which takes almost 3-4 minutes to compute. Any way I can optimize the following code without compromising on the quality.
function [blur_image] = multi_deblocking (jpg_1, jpg_2)
[Height,Width] = size(jpg_1);
%The Smoothing Windows
Smooth_Windows = 4;%4
w_size = Smooth_Windows + 1;
Half_Smooth_Windows = Smooth_Windows/2;
mse_inverse = 0;
window_i = 0;
interm_block = 0;
% ==== for jpg_2
Res_im_2 = (jpg_2+jpg_1)/2;
for j = w_size:1:Width-8-w_size+1
for i = w_size:1:Height-8-w_size+1
MSE_Total = zeros(1,1,'double');
Average_Results = zeros(7,7,'double');
Total_Num = zeros(1,1,'double');
Shifted_Block = zeros(7,7,'double');
for p=-Half_Smooth_Windows:1:Half_Smooth_Windows;
for q=-Half_Smooth_Windows:1:Half_Smooth_Windows;
% ===spatial local filtering
if p>=-1 && p<=1 && q>=-1 && q<=1
for m=1:1:7
for n=1:1:7
Shifted_Block(m+1,n+1) = jpg_2(i+p+m-3,j+q+n-3);
MSE_Total = MSE_Total + (jpg_2(i+p+m-3,j+q+n-3) - jpg_2(i+m-3,j+n-3)).^2;
end
end
MSE_Total = MSE_Total/49;
window_i = window_i +1;
tune_ratio = 800; %600
mse_inverse = mse_inverse + exp(-MSE_Total/tune_ratio);
interm_block = interm_block + exp(-MSE_Total/tune_ratio)* Shifted_Block(4,4);
Shifted_Block = zeros(7,7,'double');
MSE_Total = zeros(1,1,'double');
end
% === inter-image local filtering
if abs(p) + abs(q) <= 1 %p>=-1 & p<=1 & q>=-1 & q<=1
for m=1:1:7
for n=1:1:7
Shifted_Block(m+1,n+1) = jpg_1(i+p+m-3,j+q+n-3);
MSE_Total = MSE_Total + (jpg_1(i+p+m-3,j+q+n-3) - jpg_2(i+m-3,j+n-3)).^2;
end
end
MSE_Total = MSE_Total/49;
window_i = window_i +1;
tune_ratio = 600; %600
mse_inverse = mse_inverse + exp(-MSE_Total/tune_ratio);
interm_block = interm_block + exp(-MSE_Total/tune_ratio)* Shifted_Block(4,4);
Shifted_Block = zeros(7,7,'double');
MSE_Total = zeros(1,1,'double');
end
end
end
if window_i > 0
interm_block = interm_block/mse_inverse;
Res_im_2(i,j) = interm_block;
mse_inverse = 0;
window_i = 0;
interm_block =0;
end
end
end
blur_image = uint8(Res_im_2);
end
0 Comments
Accepted Answer
Jan
on 23 Mar 2014
Vectorize your code. Example:
% Before the loops over m and n:
Shifted_Block(2:8, 2:8) = jpg_2(i+p+(1:7)-3, j+q+(1:7)-3);
This reduces the runtime by 50% already. Applying the same method for MSE will decrease the computing time also.
It looks strange, that you allocate Shifted_Block as 7x7 array, but write to the elements 2 to 8.
It wastes time to create a new array in each iteration by zeros(7,7,'double'). The elements are overwritten at all, so this line can be removed without any difference except for saving processing time.
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!