필터 지우기
필터 지우기

Finding distance between point and edge of image mask

조회 수: 14 (최근 30일)
Joe
Joe 2012년 7월 15일
댓글: Walter Roberson 2017년 9월 24일
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.

채택된 답변

Teja Muppirala
Teja Muppirala 2012년 7월 15일
I think what you are looking for is the BWDIST function.
help bwdist
  댓글 수: 4
Image Analyst
Image Analyst 2017년 9월 24일
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 2017년 9월 24일
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".

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2012년 7월 15일
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
Ryan
Ryan 2012년 7월 16일
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 2012년 7월 17일
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

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by