Filter löschen
Filter löschen

Complex image processing for loop vectorization

2 Ansichten (letzte 30 Tage)
Artur MKRTCHYAN
Artur MKRTCHYAN am 19 Mai 2021
Kommentiert: Artur MKRTCHYAN am 27 Mai 2021
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 Kommentare
Rik
Rik am 21 Mai 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
Artur MKRTCHYAN am 25 Mai 2021
Of course I've already used the profiler, and that's why I want to vectorize the double loop because that's exactly what takes more time. So I'm not asking to rewrite the code, I'm looking to see if it's possible to optimize only the double loop part.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 21 Mai 2021
I would not reuse that code. I'd use the built-in imguidedfilter() function.
  3 Kommentare
Rik
Rik am 25 Mai 2021
Would it be possible to write an interface function that converts the input parameters to what imguidedfilter needs?
Artur MKRTCHYAN
Artur MKRTCHYAN am 27 Mai 2021
It seems to me that this function has been created because the basic function is not adapted for RGB images. I'm not sure that's possible to make it this way.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by