2015-05-27 24 views
1

我有一個圖像畫線連接質心

enter image description here

我處理後找到質心,它有四個質心。 我的目標是我想用線連接它們並測量這個區域之間的角度。要明確質心和我的目標,您可以打開enter image description here

這是我的代碼實現了質心

I = imread('22c.jpg'); 
Ibw = im2bw(I); 
Ibw = imfill(Ibw,'holes'); 

Ilabel = bwlabel(Ibw); 
stat = regionprops(Ilabel,'centroid'); 
imshow(I); hold on; 
for x = 1: numel(stat) 
    plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro'); 
end 

的問題是,我仍然感到困惑做下(連接各重心和測量的角度)。我需要你的幫助,感謝

+1

你的任務不清楚。 「測量這個區域之間的角度」是什麼意思? –

+0

圖片中您也在廣場內圈了圈,但是當我運行您的代碼時,我沒有看到那個圈子。你是怎麼得到這個的? –

+0

這個問題和你[上一個問題](http://stackoverflow.com/q/30462831/2545927)有什麼區別? – kkuilla

回答

1

這裏是一個file exchange linkbresenham.m

改變了你的代碼來獲取所有的4個重心

%// read your input image 
im = imread('http://i.stack.imgur.com/xeqe8.jpg'); 

BW = im>220; 

CC = bwconncomp(BW); 
stat = regionprops(CC,'Centroid'); 

figure; imshow(BW); hold on 
for x = 1: numel(stat) 
    plot(stat(x).Centroid(1),stat(x).Centroid(2),'ro'); 
end 

這裏是輸出:

enter image description here

進一步實施:

%// putting all the Centroid coordinates into corresponding x,y variable 
x = [stat(1).Centroid(1),stat(2).Centroid(1),stat(3).Centroid(1),stat(4).Centroid(1)]; 
y = [stat(1).Centroid(2),stat(2).Centroid(2),stat(3).Centroid(2),stat(4).Centroid(2)]; 

%// obtain row and col dim 
[r,c] = size(BW); 

%// get all x,y values connecting the centroid points 
[xAll{1},yAll{1}] = bresenham(x(1),y(1),x(4),y(4)); 
[xAll{2},yAll{2}] = bresenham(x(2),y(2),x(3),y(3)); 
[xAll{3},yAll{3}] = bresenham(x(3),y(3),x(4),y(4)); 

%// change row and col subs to linear index 
for ii = 1:3 
    idx{ii} = sub2ind(size(BW),yAll{ii},xAll{ii}); 
end 

%// change grayscale image to 3D (as you want red line) 
out = repmat(im,[1,1,3]); 

%// obtaining corresponding index of all 3 slices 
for ii = 1:3 
    idxall{ii} = bsxfun(@plus, idx{ii},[0:2].*(r*c)); 
end 

%// keep only the index of 1st slice to 255 and changing rest to 0 to obtain a red line. 
%// Similar process for blue line except keep the index in the 3rd slice to 255 
out(cat(1,idxall{:})) = 0; 
out(idx{1}) = 255; 
out(idx{2}) = 255; 
out(idx{3}+2*(r*c)) = 255; 

%// see what you have obtained 
figure; imshow(out);hold on 
for x = 1: numel(stat) 
    plot(stat(x).Centroid(1),stat(x).Centroid(2),'bo'); 
end 

結果:

注:行可能看起來帶點由於圖片的尺寸較大,但其持續

enter image description here

最後一個數字放大看實線:

enter image description here

進一步說:

您可能必須採取@Spektre的意見,發現傾斜使用atan2角度。有關更多說明,請參閱his answer

+0

@Arsene我認爲Arsene我們得到了4分(2個質心和2個骨骼邊緣峯值),所以他有2條線/軸而不是1條。在這個解決方案中需要兩條線/軸之間的角度。問題在於質心不精確,並不一定代表骨骼軸上的點,這使得它不能用於此任務(除非骨骼是對稱的並且水平或垂直對齊)角度計算很容易,正如我在他之前的問題中所寫的那樣(通過使用2 x'atan2或atanxy'如'ang = | atan2(dir1)-atan2(dir2)|'或單個'acos(dot(dir1,dir2))' – Spektre

+0

@Spektre感謝您的建議,找到解決方案,但是你對角度的建議和Salai的答案可以結合起來作爲比較技術。謝謝 – ArseneWe

+0

@ArseneWe如果你發現我們的答案很有幫助,請考慮接受我們的答案,如果可能的話,如你所說,你可以發表你自己的答案。結合我們的答案,並告訴我們你的結果:) –