Detecting shortest path on binary image

조회 수: 7 (최근 30일)
sooljex
sooljex 2019년 11월 29일
편집: sooljex 2019년 11월 29일
I'm trying to detect the shortest path from certain point on the image P in certain radius of searching, as shown on the picture. I was using linear equation y=ax+b,
x=400;
y=600;
switch direction
case 'right'
while(impixel(image,x,y)==[0,0,0])
x=x+1;
%y=600 stays the same
end
case 'left'
while(impixel(image,x,y)==[0,0,0])
x=x-1;
end
%(...)
end
which is easy to use with horizontal line (a=0,b=x), but problem appears when I want to rotate the line with point P as origin. Since the pixel P is not the origin (0,0) of the image, its not enough to just change the 'a' parameter, but also somehow calculate 'b' for full linear equation.
Maybe there is some helpfull command that allows to detect the clostest binary object in given direction? If not, I'd appreciate any suggestions how can I calculate a and b parameters for the line equations.
pic.jpg
  댓글 수: 2
KALYAN ACHARJYA
KALYAN ACHARJYA 2019년 11월 29일
The question is:
Are you looking for the distance between yellow point and nearest white pixel towards right hand side?
Right?
sooljex
sooljex 2019년 11월 29일
yes

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

채택된 답변

Image Analyst
Image Analyst 2019년 11월 29일
Steve Eddins has a whole blog series on this. I suggest you read it: Exploring shortest paths
To detect the boundary point closest to (x1, y1) you'll need to do this:
mask = bwareafilt(mask, 1); % Make sure there is only one blob.
boundaries = bwboundary(mask);
boundaries = boundaries{1}; % Extract from cell. Data is [rows, columns], not [x, y]
% Find rows
yRows = boundaries(:, 1);
% Find columns (x)
xColumns = boundaries(:, 2);
% Find distances from (x1, y1) to all other points.
distances = sqrt((xColumns - x1).^2 + (yRows - y1).^2);
% Find the closest
[minDistance, indexOfMin] = min(distances);
% Find the coordinates
xColumnClosest = xColumns(indexOfMin)
yRowClosest = yRows(indexOfMin)
  댓글 수: 1
sooljex
sooljex 2019년 11월 29일
편집: sooljex 2019년 11월 29일
This definitely seem to be working, thanks! Other part of my question was- can I somehow limit the area (give direction) in which it will try detect an object? (for example; give a cone shaped area of searching like on first picture)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by