Calculate minimum distance in image

2 ビュー (過去 30 日間)
Nhat Nguyen
Nhat Nguyen 2022 年 12 月 12 日
コメント済み: Nhat Nguyen 2022 年 12 月 12 日
Hi,
I have multiple sequence images like below. I want to calculate the minimum distance of 2 boundaries of black object along x axis (like the red line) and distance at a given Y-axis data.
Can you suggest me some methods?
Thank you!

採用された回答

DGM
DGM 2022 年 12 月 12 日
編集済み: DGM 2022 年 12 月 12 日
Here's one way. You can also use pdist2() if you have it.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1227937/image.png');
% binarize it somehow
mask = rgb2gray(inpict)>200;
% get only the interior boundary curves
mask = padarray(mask,[1 1],1,'both'); % pad
mask = bwperim(mask); % get perimeter
mask = mask(2:end-1,2:end-1); % crop
% get x,y coordinates of all pixels in the curves
CC = bwconncomp(mask);
[y1 x1] = ind2sub(size(mask),CC.PixelIdxList{1});
[y2 x2] = ind2sub(size(mask),CC.PixelIdxList{2});
% find distance between all points
D = sqrt((x1-x2.').^2 +(y1-y2.').^2);
[minD idx] = min(D,[],'all','linear')
minD = 61.0328
idx = 202
% find corresponding points on each curve
[r c] = ind2sub(size(D),idx);
pointlist = [x1(r) y1(r); x2(c) y2(c)];
% show the shortest distance
imshow(mask); hold on
plot(pointlist(:,1),pointlist(:,2))
Note that this finds the minimum euclidean distance rather than simply finding the minimum horizontal width of the dark region. The latter would be simpler, but I'm assuming that's not what you want.
  1 件のコメント
Nhat Nguyen
Nhat Nguyen 2022 年 12 月 12 日
Thanks you, I have found answer for my question, and your answer also very helpful as my image is asymmetric.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with Image Processing Toolbox についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by