Shortest path in grayscale image

3 ビュー (過去 30 日間)
Zuzana Vavrova
Zuzana Vavrova 2017 年 5 月 25 日
回答済み: George Abrahams 2023 年 12 月 18 日
How can I find the path with least intenzity between two points (A and B) like on image:
  1 件のコメント
John D'Errico
John D'Errico 2017 年 5 月 25 日
Not a question about MATLAB, unless this is a doit4me.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2017 年 5 月 25 日
Or, if you know the locations of A and B you can use Dynamic Programming or something like A*

その他の回答 (1 件)

George Abrahams
George Abrahams 2023 年 12 月 18 日
Hi Zuzana. Late to the party, but one way would be to use my bwgraph function on File Exchange. See the example below.
% im is the image intensity, mask is the allowed pixels.
im = imresize( im2gray( imread( 'eyeScan.jpg' ) ), [ 249 249 ] );
mask = logical( insertShape( zeros( size( im ) ), ...
'FilledCircle', [ 125 125 118 ], 'Color', 'white', ...
'Opacity', 1, 'SmoothEdges', false) );
mask = mask(:,:,1);
% Construct the graph of connected non-zero pixels, use im as weights.
G = bwgraph( mask, NodeWeights=im );
% Calculate the linear indices of the start and end pixels, which are
% used to reference the respective node in G.
sz = size( im );
source = sub2ind( sz, 152, 86 );
target = sub2ind( sz, 132, 221 );
% Find the shortest path between the 2 nodes.
P = shortestpath( G, source, target );
% Calculate the respective pixels for each node in the path.
[ Pi, Pj ] = ind2sub( sz, P );
% Plot the image and path.
figure
imshow( im, [] )
hold on
plot( Pj, Pi, 'r-', 'LineWidth', 1 )

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by