minimum distance between 2 objects
조회 수: 1 (최근 30일)
이전 댓글 표시
I plotted boundries with bwboundries and smoothed them with the sgolay filter then plotted these boundries over RGB image. Could you kindly help me in finding the shortest distance between these two boundaries (the shortest distance of the area in-between the red and yellow lines)?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1200393/image.png)
댓글 수: 0
채택된 답변
Walter Roberson
2022년 11월 20일
Trace the boundaries of the two objects. https://www.mathworks.com/help/images/boundary-tracing-in-images.html
Then with the coordinates of each point for each of the two objects, use pdist2() and then min()
If you do not have Statistics and Machine Learning Toolbox for pdist2() you can use
D = sqrt((x1(:) - x2(:).').^2 + (y1(:) - y2(:).').^2);
댓글 수: 0
추가 답변 (1개)
Image Analyst
2022년 11월 20일
If xy is an N by 2 array of (x,y) coordinates, then (untested)
allDistances = pdist2(xy1, xy2);
minDistance = min(allDistances(:))
% Find index of closest pair
[row, column] = find(allDistances == minDistance);
% Draw a line between them
x1 = xy1(row, 1);
y1 = xy1(row, 2);
x2 = xy2(col, 1);
y2 = xy2(col, 2);
hold on;
line([x1, x2], [y1, y2], 'LineWidth', 2, 'Color', 'b')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Statistics and Machine Learning Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!