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

조회 수: 12 (최근 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개)

Community Treasure Hunt

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

Start Hunting!

Translated by