How to find the nearest object to a reference point in binary image?

2 ビュー (過去 30 日間)
Meshooo
Meshooo 2014 年 11 月 16 日
コメント済み: Image Analyst 2014 年 11 月 16 日
Dear all,
I have a binary image that contains two objects(Object_A, Object_B) very close to each other as shown below. I want to find which object is closer to a reference point in order to be selected (Object_B in this example).
I was thinking to find the distance of the centroid of each object to the reference point (in this example the centroid of the reference point X = 360, Y = 330), but this will not work because in few cases the centroid of the far object could be closer to the reference point as I tried to explain in the figure below.
Is there any way to find the closest object to a reference point?
Any help and suggestion will be appreciated.
Meshoo

採用された回答

Image Analyst
Image Analyst 2014 年 11 月 16 日
It's trivial. Just use bwboundaries to get the list of (x,y) coordinates of the boundary and then put in a for loop, calculating the distance using the Pythagorean Theorem to get the boundary that is the closest. In the for loop, you can vectorize it and use min().
boundaries = bwboundaries(binaryImage);
overallMinDistance = inf;
for b = 1 : length(boundaries)
thisBoundaryX = boundaries{b}(:, 1);
thisBoundaryY = boundaries{b}(:, 2);
distances = sqrt((thisBoundaryX - refx).^2 + (thisBoundaryY - refy).^2);
[minDistance, indexOfMin] = min(distances);
if minDistance < overallMinDistance
% This boundary is the closer one.
.............you finish the rest - it's easy.
end
  2 件のコメント
Meshooo
Meshooo 2014 年 11 月 16 日
編集済み: Meshooo 2014 年 11 月 16 日
Thank you very much Image Analyst. I think it is similar to one old post that you answered some time ago. I tried but didn't work and I wrote a comment in your answer:
Is there any easier way to do that?
Thank you.
Image Analyst
Image Analyst 2014 年 11 月 16 日
No - this is as easy as it gets. Let me see your code. It should be really trivial so you probably just made a simple mistake.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by