Clear Filters
Clear Filters

Calculate minimum distance in image

4 views (last 30 days)
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!

Accepted Answer

DGM
DGM on 12 Dec 2022
Edited: DGM on 12 Dec 2022
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 Comment
Nhat Nguyen
Nhat Nguyen on 12 Dec 2022
Thanks you, I have found answer for my question, and your answer also very helpful as my image is asymmetric.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!