2015-02-05 93 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