Complex image processing for loop vectorization
Show older comments
Hello everyone, I am new to MatLab. I would like to reuse this code but I was wondering if there is a way to vectorize the double for loop ?
link to paper : Guided Image Filtering
function q = guidedfilter_color(I, p, r, eps)
% GUIDEDFILTER_COLOR O(1) time implementation of guided filter using a color image as the guidance.
%
% - guidance image: I (should be a color (RGB) image)
% - filtering input image: p (should be a gray-scale/single channel image)
% - local window radius: r
% - regularization parameter: eps
if ~(size(I,3) == 3)
error('The guidance image input should have 3 channels');
end
[hei, wid] = size(p);
if r<2*min(hei, wid), r = round(min(hei, wid)/4); end;
N = boxfilter(ones(hei, wid), r); % the size of each local patch; N=(2r+1)^2 except for boundary pixels.
mean_I = zeros(size(I));
for ii =1:size(I,3)
mean_I(:,:,ii) = boxfilter(I(:, :, ii), r) ./ N;
end
mean_p = boxfilter(p, r) ./ N;
mean_Ip = zeros(size(I));
for ii =1:size(I,3)
mean_Ip(:,:,ii) = boxfilter(I(:, :, ii).*p, r) ./ N;
end
% covariance of (I, p) in each local patch.
cov_Ip = zeros(size(I));
for ii =1:size(I,3)
cov_Ip(:,:,ii) = mean_Ip(:,:,ii) - mean_I(:,:,ii) .* mean_p;
end
% variance of I in each local patch: the matrix Sigma in Eqn (14).
% Note the variance in each local patch is a 3x3 symmetric matrix:
% rr, rg, rb
% Sigma = rg, gg, gb
% rb, gb, bb
var_I_rr = boxfilter(I(:, :, 1).*I(:, :, 1), r) ./ N - mean_I(:,:,1) .* mean_I(:,:,1);
var_I_rg = boxfilter(I(:, :, 1).*I(:, :, 2), r) ./ N - mean_I(:,:,1) .* mean_I(:,:,2);
var_I_gg = boxfilter(I(:, :, 2).*I(:, :, 2), r) ./ N - mean_I(:,:,2) .* mean_I(:,:,2);
var_I_rb = boxfilter(I(:, :, 1).*I(:, :, 3), r) ./ N - mean_I(:,:,1) .* mean_I(:,:,3);
var_I_gb = boxfilter(I(:, :, 2).*I(:, :, 3), r) ./ N - mean_I(:,:,2) .* mean_I(:,:,3);
var_I_bb = boxfilter(I(:, :, 3).*I(:, :, 3), r) ./ N - mean_I(:,:,3) .* mean_I(:,:,3);
a = zeros(hei, wid, 3);
for y=1:hei
for x=1:wid
Sigma = [var_I_rr(y, x), var_I_rg(y, x), var_I_rb(y, x);
var_I_rg(y, x), var_I_gg(y, x), var_I_gb(y, x);
var_I_rb(y, x), var_I_gb(y, x), var_I_bb(y, x)];
%Sigma = Sigma + eps * eye(3);
cov_Ip1 = [cov_Ip(y, x,1), cov_Ip(y, x,2), cov_Ip(y, x,3)];
a(y, x, :) = cov_Ip1 * inv(Sigma + eps * eye(3)); % Eqn. (14) in the paper;
end
end
b = mean_p - a(:, :, 1) .* mean_I(:,:,1) - a(:, :, 2) .* mean_I(:,:,2) - a(:, :, 3) .* mean_I(:,:,3); % Eqn. (15) in the paper;
q = (boxfilter(a(:, :, 1), r).* I(:, :, 1)...
+ boxfilter(a(:, :, 2), r).* I(:, :, 2)...
+ boxfilter(a(:, :, 3), r).* I(:, :, 3)...
+ boxfilter(b, r)) ./ N; % Eqn. (16) in the paper;
end
4 Comments
Rik
on 19 May 2021
Vectorization of code will only speed up your code if there is a way to process the data as an array. A good example of this is a convolution, which you could implement by looping over every dimension, but for which there are mathematical shortcuts that Matlab implements.
Sometimes array processing will cause a slowdown because of memory requirements.
If you're looking for a speed increase: a change in algorithm has a much larger potential impact.
Artur MKRTCHYAN
on 21 May 2021
Rik
on 21 May 2021
I don't have a ready-made solution for you. You could start yourself by running the profiler to look where your code spends the most time.
Artur MKRTCHYAN
on 25 May 2021
Answers (1)
Image Analyst
on 21 May 2021
0 votes
I would not reuse that code. I'd use the built-in imguidedfilter() function.
3 Comments
Artur MKRTCHYAN
on 25 May 2021
Rik
on 25 May 2021
Would it be possible to write an interface function that converts the input parameters to what imguidedfilter needs?
Artur MKRTCHYAN
on 27 May 2021
Categories
Find more on Image Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!