How to vectorize the following for...loop?

조회 수: 1 (최근 30일)
Baraka Maiseli
Baraka Maiseli 2016년 4월 9일
댓글: Kuifeng 2016년 4월 10일
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
Kuifeng
Kuifeng 2016년 4월 10일
can you give an example value/matrix of v, d1, d2?

댓글을 달려면 로그인하십시오.

답변 (1개)

Kuifeng
Kuifeng 2016년 4월 9일
% 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
Kuifeng
Kuifeng 2016년 4월 9일
%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);
Baraka Maiseli
Baraka Maiseli 2016년 4월 9일
편집: Baraka Maiseli 2016년 4월 9일
Testing the code snippet (for the forward pass), as it is, I get:
Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf 0 1 Inf Inf
Inf Inf Inf 1 Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf
But the old code, which I use as reference, gives:
Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf Inf Inf Inf Inf Inf
Inf Inf Inf Inf 0 1 2 3 Inf
Inf Inf Inf Inf 1 2 3 4 Inf
Inf Inf Inf Inf 2 3 4 5 Inf
Inf Inf Inf Inf 3 4 5 6 Inf
Inf Inf Inf Inf Inf Inf Inf Inf Inf
The two implementations produce different results.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by