How to vectorize the following for...loop?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear colleagues,
I have written the following MATLAB code, with two for...loops, which compute the distance map of an image. However, the code is relatively slower, and I want to speed it up using vectorization techniques. I am still a newbie in code optimization; still learning. Any idea on how to vectorize the code, please share:
function [ res ] = computeDT( v, d1, d2 )
%%COMPUTEDT -- Compute the distance field of an input image, img
% v -- Input image
% res -- Output image
%%%%%%%Initialize the input image
v = v([1,1:end,end],[1,1:end,end]);
v(v~=0) = inf;
% Weights [0 +d1 +d2]
%%Forward pass
M1 = size(v,2); % Number of columns
M2 = size(v,1); % Number of lines (rows)
for k1=2:M1-1
for k2=2:M2-1
v(k1,k2) = min([v(k1 - 1, k2 - 1) + d2, v(k1 - 1, k2) + d1,...
v(k1 - 1, k2 + 1) + d2, v(k1, k2 - 1) + d1, v(k1, k2)]);
end
end
%%Backward pass
for k1=M1-1:-1:2
for k2=M2-1:-1:2
v(k1,k2) = min([v(k1, k2), v(k1, k2 + 1) + d1, v(k1 + 1, k2 - 1) + d2,...
v(k1 + 1, k2) + d1, v(k1 + 1, k2 + 1) + d2]);
end
end
res = v(2:end-1,2:end-1);
% save res.mat
% subimage(mat2gray(res))
end
1 commentaire
Réponses (1)
Kuifeng
le 9 Avr 2016
% maybe make some changes to the following code could help,
[rows cols] = size(ans);
v1 = v(1:rows-1, 1:cols-1);
v2 = v(2:rows, 1:cols-1);
v3 = v(1:rows-1, 2:cols);
v4 = v(2:rows, 2:cols);
result = min[v1+d1, v2+d2, v3, v4, v5]; %for example only, revise accordingly
3 commentaires
Kuifeng
le 9 Avr 2016
%for example in the 'forward pass',
[rows cols] = size(v);
v1 = v(1:rows-2, 1:cols-2);
v2 = v(1:rows-2, 2:cols-1);
v3 = v(1:rows-2, 3:cols);
v4 = v(2:rows-1, 1:cols-2);
v5 = v(2:rows-1, 2:cols-1);
result = min(min(min(min(v1 + d2, ...
v2 + d1), ...
v3 + d2), ...
v4 + d1), ...
v5);
Voir également
Catégories
En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!