Distance between object and point

18 views (last 30 days)
r_com3
r_com3 on 21 Dec 2019
Answered: Image Analyst on 21 Dec 2019
Hello,
In the image below I need to find a nearest point to each of the corners (green marks) of the red bounding box for each object. This point must belong to the object (white shape).
bound.jpg
So for each object, I need to locate 4 points, which are closest to the green corners of the bounding around the object.
Anyone has idea how to perform it?
Thanks in advane for any help
  1 Comment
r_com3
r_com3 on 21 Dec 2019
I found a PixelList in regionprops.
If i get it right, in PixelList i have coordinates of each pixel belonging to object.
Is checking distance for each pixel and corner of bounding box a good idea?
Thanks for help

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 21 Dec 2019
No. Use bwboundaries() instead. It will be faster since you have far fewer points to check the distance from. If you have x and y upper/lower left/right corner point locations, then do something like (untested):
boundaries = bwboundaries(binaryImage);
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
x = thisBoundary(:, 2);
y = thisBoundary(:, 1);
% Find distances to upper left point (xul, yul)
distances = sqrt((xul - x).^2 + (yul - y).^2);
[minDistance, indexOfMinDistance] = min(distances)
% Save the upper left boundary point as xb(k, 1) and yb(k, 1)
xb(k, 1) = x(indexOfMinDistance);
yb(k, 1) = y(indexOfMinDistance);
% Now repeat for upper right.
% Find distances to upper right point (xur, yur)
distances = sqrt((xur - x).^2 + (yur - y).^2);
[minDistance, indexOfMinDistance] = min(distances)
% Save the upper left boundary point as xb(k, 2) and yb(k, 2)
xb(k, 2) = x(indexOfMinDistance);
yb(k, 2) = y(indexOfMinDistance);
% Now repeat for lower right.
% Find distances to lower right point (xlr, ylr)
distances = sqrt((xlr - x).^2 + (ylr - y).^2);
[minDistance, indexOfMinDistance] = min(distances)
% Save the lower right boundary point as xb(k, 3) and yb(k, 3)
xb(k, 3) = x(indexOfMinDistance);
yb(k, 3) = y(indexOfMinDistance);
% Now repeat for lower left.
% Find distances to lower left point (xll, yll)
distances = sqrt((xll - x).^2 + (yll - y).^2);
[minDistance, indexOfMinDistance] = min(distances)
% Save the lower left boundary point as xb(k, 2) and yb(k, 4)
xb(k, 4) = x(indexOfMinDistance);
yb(k, 4) = y(indexOfMinDistance);
end

Community Treasure Hunt

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

Start Hunting!