2015-02-05 70 views
1

我在matlab中有两个'for'循环代码片段: 'I'是一个已经预先分配的二进制映像。从MATLAB文件交换Vectorize'for'循环调用其他函数

enter image description here

... 
    [x,y] = find(bwmorph(I,'endpoints')); 
    n=numel(x); 
    m=numel(x)-1; 
    n=m+1; 
    r=i+1; 
    for i= 1:m 
     for j = r:n 
      I=linept(I, x(i), y(i), x(j), y(j)); 
     end; 
    end; 
    ... 

的linept功能给出below.Its:

function result=linept(matrix, X1, Y1, X2, Y2) 
result = matrix; 
a=max(1, X1);b=sign(X2 - X1);c=max(1, X2); 
for x=a:b:c 
    y = round(f(x, X1, Y1, X2, Y2)); 
    if y > 0 
     result(x, y) = 1; 
    end 
end 
d=max(1, Y1);e=sign(Y2 - Y1);g=max(1, Y2); 
for y=d:e:g 
    x = round(f2(y, X1, Y1, X2, Y2)); 
    if x > 0 
     result(x, y) = 1; 
    end 
end 

function y=f(x, X1, Y1, X2, Y2) 
a = (Y2 - Y1)/(X2 - X1); 
b = Y1 - X1 * a; 
y = a * x + b; 

function x=f2(y, X1, Y1, X2, Y2) 
if X1==X2 
    x = X1; 
else 
    a = (Y2 - Y1)/(X2 - X1); 
    b = Y1 - X1 * a; 
    x = (y - b)/a; 
end 

由于许多 '的' 循环和函数调用,这个代码是很运行它运行速度很快,只需要很少的端点,但是边缘数量多时需要很多时间。如果图像大小减小,它会稍微快一点。我试图对它进行矢量化并预先分配有些变量,但没有太大的改进。可以任意ne帮我关于如何矢量化循环调用函数的代码。谢谢

+0

你可以分享'func2'和'func3'功能代码吗?另外,'n = m + 1中的'm'和'r = i + 1中的'i'在开始时是什么?类似于'a,b,c' ......你可以添加他们所代表的东西吗? – Divakar

+0

是的,我现在将在'func'中编辑现在的问题 – Matte

+0

'a,b,c,d,e,g'? – Divakar

回答

2

这是一个很大的问题!

简要的讨论和解决方案的代码

好了,接下来上市的是大量使用bsxfun在各种各样的地方照顾连接与所有其他点的所有点的量化方法,通过expansions这基本上是如何bsxfun经营。其中的代码使用示例输入数据进行演示。看看 -

%// Create demo input data 
img = false(20); 
img(2,5:15) = 1; 
img(12,5:15) = 1; 
figure,imagesc(img), colormap gray, title('Starting Binary image') 

%// Find endpoints 
[X,Y] = find(bwmorph(img,'endpoints')); 

%// Make a new binary image with only endpoints in it 
I = false(size(img)); 
I(sub2ind(size(I),X,Y)) = 1; 
figure,imagesc(I), colormap gray, title('Endpoints detected') 

%-------- Vectorized code starts here ... 
[nrows,ncols] = size(I); %// Parameters 
npts = numel(X); 

twopts = nchoosek(1:npts,2); 
slopes = (Y(twopts(:,1)) - Y(twopts(:,2)))./(X(twopts(:,1)) - X(twopts(:,2))); 

%// Find the connecting indices with X-Y as they are, to work with 
%// slopes within [-1 1] 
stage1 = abs(slopes)<=1; 
[colID,rowID] = connecting_idx(X,Y,twopts(stage1,:),slopes(stage1)); 
valid = colID>0 & rowID>0 & colID<=ncols & rowID<=nrows; 
I((colID(valid)-1)*nrows + rowID(valid))=1; 

%// Find the connecting indices with X-Y interchanged, to work with 
%// slopes outside [-1 1] 
[rowID,colID] = connecting_idx(Y,X,twopts(~stage1,:),1./slopes(~stage1)); 
valid = colID>0 & rowID>0 & colID<=ncols & rowID<=nrows; 
I((colID(valid)-1)*nrows + rowID(valid))=1; 
figure,imagesc(I),colormap gray,title('Final output : Endpoints connected') 

相关的功能代码(代码库中最重要的部分) - 后

function [y_all,x_all] = connecting_idx(X,Y,twopts,slopes) 

%// Find XY indices that connects the X, Y anchor points given the two points 
%// combinations and the corresponding slopes 

X_twopts = reshape(X(twopts),size(twopts)); 
Y_twopts = reshape(Y(twopts),size(twopts)); 

[sortedX_pairs1,sorted_idx1] = sort(X_twopts,2); 
X_starts1 = sortedX_pairs1(:,1); 
Y_pairs = Y_twopts; 
Y_starts = Y_pairs(sorted_idx1==1); 
offsets = Y_starts - slopes.*X_starts1; 
max_X_len = max(diff(sortedX_pairs1,[],2)); 

x_all = bsxfun(@plus,X_starts1,[0:max_X_len]); 
x_lens = diff(sortedX_pairs1,[],2); 
mask = bsxfun(@gt,x_lens+1,0:max_X_len); 
y_all = round(bsxfun(@plus,bsxfun(@times,x_all,slopes),offsets)); 

y_all = y_all(mask); 
x_all = x_all(mask); 

return; 

调试图像的代码运行 -

enter image description here

enter image description here

enter image description here

+0

非常感谢!我现在要通过代码 – Matte

+0

请详细解释一下你在这一部分做了些什么:'stage1 = abs(slopes)<= 1; (col1,rowID] = connected_idx(X,Y,twopts(stage1,:),slopes(stage1)); valid = colID> 0&rowID> 0&colID <= ncols&rowID <= nrows; I((colID(valid)-1)* nrows + rowID(valid))= 1;' – Matte

+0

@Matt好的'slopes'是一个数组,用于存储所有点的斜率。通过'stage1',我选择在[-1 1]范围内具有斜率的对,然后找到这些对之间的连接。在下一步中,如果您看到,我正在使用'〜stage1'来选择其余的对并查找它们的连接索引。因此,在从两个步骤获得连接索引之后,我们将输入矩阵中的那些设置为一个。 – Divakar