Clear Filters
Clear Filters

Finding distance between point and edge of image mask

14 views (last 30 days)
I'm trying to find how far a certain point is from the edge of an ellipse-like shape. The shape is stored as a standard image mask, and the point is in the interior of the shape. What would be the best way to quickly find the shortest distance between the point and mask edge?
Note: I'll need to calculate this distance for each point in the mask's interior.
Thanks.

Accepted Answer

Teja Muppirala
Teja Muppirala on 15 Jul 2012
I think what you are looking for is the BWDIST function.
help bwdist
  4 Comments
Image Analyst
Image Analyst on 24 Sep 2017
Kumuda, you must have overlooked my code below where I give the distances for a similar situation. In short, the distances between all points on the boundary, gotten from using bwboundaries(), are these:
distances = sqrt((x-xOfCentroid).^2+(y-yOfCentroid).^2);
If you need more filled out demo, then start your own question, since it's somewhat different than Joe's original question here.
Walter Roberson
Walter Roberson on 24 Sep 2017
The sqrt() formula Image Analyst gives is for the Euclidean distance, which might be what you want. But since you posted in a question about the distance transform, which deals with geodesic distances, my answer might be what you want instead. There are a lot of different ways of measuring "distance".

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 15 Jul 2012
I'd call bwboundaries() and then use the Pythagorean theorem to calculate the distance from the point in question to each of the points on the boundary. Write back if you can't figure out the code. Here's a snippet from my image segmentation tutorial in my File Exchange to get you started:
hold on;
boundaries = bwboundaries(binaryImage);
numberOfBoundaries = size(boundaries);
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'g', 'LineWidth', 2);
end
Another option is to call bwdist() but that might take longer because it does the calculation for all points, not just the one point you're interested in.
  4 Comments
Ryan
Ryan on 16 Jul 2012
On a similar note (to help me differentiate these two methodologies), which would be the method to take a zero matrix with random points as ones and determine the distance from each point to every other point in the matrix?
A = im2bw(rand(500),graythresh(rand(500)));
distances = ___?????___(A,....);
Image Analyst
Image Analyst on 17 Jul 2012
See this demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Make some random noise.
randomNoise = rand(100);
subplot(2,2,1);
imshow(randomNoise, []);
axis on;
title('Noise', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Threshold to get some randomly located points.
isolatedPoints = randomNoise > 0.999;
subplot(2,2, 2);
imshow(isolatedPoints, []);
axis on;
title('Original Point Set', 'FontSize', fontSize);
% Create another plot for the lines.
subplot(2, 3, 5);
imshow(isolatedPoints, []);
axis on;
hold on;
title('Original Point Set with Lines', 'FontSize', fontSize);
% Find the points row and column locations.
[rows columns] = find(isolatedPoints)
if isempty(rows)
return;
end
numberOfPoints = length(rows);
% Initialize some things:
distances = zeros(numberOfPoints, numberOfPoints);
% Calculate distances and draw lines between the points.
for p1 = 1 : numberOfPoints
x1 = columns(p1);
y1 = rows(p1);
for p2 = 1 : numberOfPoints
x2 = columns(p2);
y2 = rows(p2);
% Plot the line between them
plot([x1 x2], [y1, y2]);
% Calculate the distance between them
distances(p1, p2) = sqrt((x2-x1)^2+(y2-y1)^2);
end
end
% Print out to command window.
distances

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!