2017-04-25 72 views
0

使用此代碼(爲問題創建),我向您展示我的問題示例,其中I是0和1的矩陣對。您說,獲取的圖像代表骨架的兩個「分支」。正如你所看到的,兩個分支的某個像素似乎處於相同的方向。我想要做的是每個分支的三個像素取三,比較它們的方向(角度),如果我們得到了兩個分支相同的方向,然後填寫的像素,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

是否可以做同樣的事情,但只考慮分支的某些像素?對於我的一些分支來說,係數會有所不同,因爲我們考慮了整個分支,但如果我們在分支的末端只有兩個或三個像素,那麼係數在比較中可以相等。 –

+0

EPS只是一小部分有助於找到「非常接近」的係數。目前它只在每個分支邊緣的兩個像素處完成*,所以這就是你要求的。 – user2999345

相關問題