REGIONPROPS从输出将是一个N×1结构阵列与一个场'Centroid'
包含1×2阵列。您可以首先使用函数VERTCAT将所有这些数组连接成一个N×2数组。然后,您可以使用函数REPMAT复制图像中心坐标(假设为1乘2阵列),以便它变为N乘2阵列。现在,您可以使用矢量运算计算距离和使用功能MIN找到的最小距离值的索引:
props = regionprops(labeledImage, 'Centroid');
centers = vertcat(props.Centroid); %# Vertically concatenate the centroids
imageCenter = [x y]; %# Your image center coordinates
origin = repmat(imageCenter,numel(props),1); %# Replicate the coordinates
squaredDistance = sum(abs(centers-origin).^2,2); %# Compute the squared distance
[~,minIndex] = min(squaredDistance); %# Find index of the minimum
注意,因为你只是想的最小距离,你可以用平方距离和避免不必要的电话SQRT。另请注意,函数BSXFUN可用作复制图像中心坐标以从对象质心中减去它们的替代方法。
是啊!很高兴,非常感谢你。 – 2011-03-22 20:42:07