我有一张二值图像,其中包含我通过 bwconncomp
识别的多个感兴趣区域。我试图找到连接每个这些区域的最短点。我正在考虑在一个循环中使用越来越大的内核大小的扩张,代码类似于下面,当连接的组件数量下降时暂停循环,然后可能通过质心的相当大的变化识别那些已经连接的组件并使用迭代次数两个给大概的距离?我觉得应该有更好的方法来做到这一点?
distancebetweenROIS=[];
M11=tempBimage;
for c=1:50
TT=bwconncomp(M11);
seDil=strel('disk',c);
M11=imdilate(tempBimage,seDil);
YY=bwconncomp(M11);
if length(TT.PixelIdxList)>length(YY.PixelIdxList)
distancebetweenROIS(end+1)=c*2;
end
end
最佳答案
使用 bwdist
和 bwlabel
,您可以找到任何特征到所有其他特征的最短距离。您所要做的就是循环遍历这些功能。
%// labeledImage is 1 on feature #1, 2 on feature #2, etc
labeledImage = bwlabel(yourBinaryImage);
nLabels = max(labeledImage(:));
%// find the distance between each label and all other labels
distMat = zeros(nLabels, nLabels);
for iLabel = 1:nLabels
%// distance transform - every pixel is the distance to the nearest
%// non-zero pixel, i.e. the location of label iLabel
dist = bwdist(labeledImage==iLabel);
%// use accumarray b/c we can
%// get rid of the zeros in labeledImage, though, as there is no index 0
distMat(:,iLabel) = accumarray(dist(labeledImage>0),labeledImage(labeledImage>0),[],@min);
end
请注意,该距离相当于“如果我从特征 X 开始并从一个像素跳到另一个像素到特征 Y,我至少需要多少跳”。如果您需要质心之间的距离,regionprops(yourBinaryImage,'Centroids')
后接 pdist2
是更好的方法。
关于matlab - 找到感兴趣区域之间的最短长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34916387/