![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180184/image.jpeg)
How to find minimum distance between two cylindrical objects from binary image?
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have this binary image
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/167676/image.jpeg)
in which I have two cylindrical objects and I want to find out the minimum distance between these. How to do that? I have read this answer but I cannot understand this. Can anyone help to calculate minimum distance in a more simple way? Thanks.
0 个评论
采纳的回答
Akira Agata
2017-12-16
By using bwperim, regionprops and pdist2 functions, you can obtain the minimum distance and corresponding points on each object, like:
% Import your image and binarize
I = imread('aa.jpg');
BW = imbinarize(rgb2gray(I));
% Measure the minimum distance
BW2 = bwperim(BW,8);
stats = struct2table(regionprops(BW2,'PixelList'));
dist = pdist2(stats.PixelList{1},stats.PixelList{2});
[dmin,idx] = min(dist(:));
[r,c] = ind2sub(size(dist),idx);
pt1 = stats.PixelList{1}(r,:);
pt2 = stats.PixelList{2}(c,:);
% Show the result
imshow(BW)
hold on
plot([pt1(1) pt2(1)],[pt1(2) pt2(2)],'r-o','LineWidth',2)
disp(sprintf('Min. distance: %f [pixel]',dmin))
The minimum distance in pixel was measured to be:
Min. distance: 476.822818 [pixel]
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/180184/image.jpeg)
4 个评论
mridul ghosh
2018-5-6
编辑:mridul ghosh
2018-5-6
Sir, pls tell what modification is required in your code in case the distances between more than two objects are needed in an image..
Akira Agata
2018-5-6
If your image has N objects, the minimum distance and corresponding points between i th and j th objects can be calculated by modifying the code as follows (note: 1 <= i,j <= N and i ~= j).
dist = pdist2(stats.PixelList{i},stats.PixelList{j});
[dmin,idx] = min(dist(:));
[r,c] = ind2sub(size(dist),idx);
pt1 = stats.PixelList{i}(r,:);
pt2 = stats.PixelList{j}(c,:);
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!