方法#1
隨着a
爲二進制圖像,你可以做這樣的事情 -
[~,lowermost] = max(cumsum(sum(a,2)));
lowermostpt = [lowermost,find(a(lowermost,:),1,'first')]
[~,rightmost] = max(cumsum(sum(a,1)));
rightmostpt = [find(a(:,rightmost),1,'first'),rightmost]
[~,topmost] = max(sum(a,2)==1);
topmostpt = [topmost,find(a(topmost,:),1,'first')]
[~,leftmost] = max(sum(a,1)==1);
leftmostpt = [find(a(:,leftmost),1,'first'),leftmost]
爲了提高效率,最好將總和存儲一次稍後重複使用。
採樣運行 -
a =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
lowermostpt =
12 10
rightmostpt =
8 14
topmostpt =
4 10
leftmostpt =
8 6
方法2從圖像處理工具箱使用bwboundaries
-
idx = cell2mat(bwboundaries(a))
[~,p1] = min(idx(:,1))
topmostpt = idx(p1,:)
[~,p2] = max(idx(:,1))
lowermostpt = idx(p2,:)
[~,p3] = min(idx(:,2))
leftmostpt = idx(p3,:)
[~,p4] = max(idx(:,2))
rightmostpt = idx(p4,:)
份額你迄今爲止的所作所爲。 – Sachith
@Sachith更新了我目前所在的位置。 –
這是很好的。這將幫助其他人瞭解你迄今爲止做了什麼,並且你會得到最好的答案。 – Sachith