Finding distance between point and edge of image mask

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.

 Respuesta aceptada

I think what you are looking for is the BWDIST function.
help bwdist

4 comentarios

Kumuda Roy
Kumuda Roy el 24 de Sept. de 2017
Asking for DISTANCE between centroid and boundary points.
Set the mask to be true at the centroids; the geodesic will then calculate the distance of each point in the objects to the nearest centroid while staying strictly within objects.
Note: in the general case, the centroid of an object can be part of the background, in which case it might not be reachable from points in the object. For example,
* *
* + *
* *
*********
the centroid is at the + and is not part of the object itself.
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.
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".

Iniciar sesión para comentar.

Más respuestas (1)

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 comentarios

Joe
Joe el 16 de Jul. de 2012
Thanks, but since I need the distance for each point of the mask, bwdist is exactly what I was looking for.
I wish you'd explained that you wanted it to "every point of the mask" at first, instead of saying that you want the distance of "a point in the interior" to points on "the edge" of the mask, because I thought of bwdist() at first, until I read that you wanted it just to edge points. bwdist that is wasteful if you just want the distance of one point to all the edge points of the mask. bwdist calculates the distance from every interior point (not just one) to the nearest edge (not all points on the edge) - this is quite different than what you initially described.
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,....);
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

Iniciar sesión para comentar.

Preguntada:

Joe
el 15 de Jul. de 2012

Comentada:

el 24 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by