2017-04-25 6 views
0

このコードでは、問題の例を示します。Iは、0と1の行列ペアです。あなたが見ることができるように、2つのブランチの特定のピクセルは同じ方向にあるようです。私は何をしたい各ブランチの3つの画素により3を取ると、その方向(角度)を比較であり、我々は二つのブランチに同じ方向を得れば、その後1ブランチの特定のピクセルの向きを見つける方法は?

I=zeros(20,20); 
I(1,15)=1; I(2,14)=1; I(3,13)=1; I(1,16)=1; 

I(4,12)=1; 
I(7,9)=1; 
I(8,8)=1; 
I(9,7)=1; 
I(9,6)=1; 
I(9,5)=1; 
I(10,4)=1; 

CC=bwconncomp(I); 
I=labelmatrix(CC); %Labeling of branches 

imagesc(I) % figure of problem 
I(5,11)=1; 
I(6,10)=1; 

figure, imagesc(I) % figure of solution that I search 

答えて

1

でピクセルを区切るスペースを埋めますあなたの答えのための

% generate branches image 
I=zeros(20,20); 
I(1,15)=1; I(2,14)=1; I(3,13)=1; I(1,16)=1; 
I(4,12)=1; 
I(7,9)=1; 
I(8,8)=1; 
I(9,7)=1; 
I(9,6)=1; 
I(9,5)=1; 
I(10,4)=1; 
% find connected components 
CC=bwconncomp(I); 
% get xy coords of connected components 
[Y,X] = cellfun(@(ind) ind2sub(size(I),ind),CC.PixelIdxList,'UniformOutput',0); 
% get 1st degree polynomial coeffs for each component edges 
p1 = cellfun(@(x,y) polyfit(x(1:2),y(1:2),1),X,Y,'UniformOutput',0); 
p2 = cellfun(@(x,y) polyfit(x(end-1:end),y(end-1:end),1),X,Y,'UniformOutput',0); 
% compare polynomial coefficients 
D = pdist2(cell2mat(p1'),cell2mat(p2')); 
% find "close" coefficient values 
D = D + eye(size(D)); 
EPS = 1e-3; 
[idx1,idx2] = find(D < EPS); 
A = zeros(size(I)); 
[xg,yg] = meshgrid(1:20); 
for ii = 1:numel(idx1) 
    % chosen poly coeffs 
    p = p1{idx1(ii)}; 
    % relevant xy values 
    yy = polyval(p,xg); 
    xx = [X{idx1(ii)}(1)+1:X{idx2(ii)}(end)-1 X{idx2(ii)}(end)+1:X{idx1(ii)}(1)-1]; 
    % fill missing pixels 
    A = A + (CC.NumObjects + 1 + ii)*((abs(yy - yg) < EPS) & ismember(xg,xx)); 
end 
subplot(121); 
I = labelmatrix(CC); %Labeling of branches 
imagesc(I) % figure of problem 
subplot(122); 
I2 = double(I) + A; 
imagesc(I2) % figure of solution that I search 

enter image description here

+0

感謝:私は、各ブランチのエッジの第一次多項式の係数を比較しました。 EPSは何ですか? –

+0

同じことをすることも可能ですが、ブランチの特定のピクセルだけを考慮に入れていますか?私の枝のいくつかについては、枝全体を考慮に入れているので係数は異なりますが、枝の終わりに2〜3ピクセルしか取れない場合、係数は比較で等しくなります。 –

+0

EPSは「非常に近い」係数を見つけるのに役立ちます。現在、各ブランチエッジ*の2つのピクセルについてのみ*が行われているため、これはあなたが求めているものです。 – user2999345

関連する問題