我正在使用Matlab的图像工具箱。特别是,在对图像进行二值化和标记后,我运行
props = regionprops(labeledImage, 'Centroid'); 获取所有连接对象的质心。现在,我想找到一个更靠近一对坐标(即图像中心)的坐标。当然,我知道我可以使用for循环检查每个props [i] .Centroid对坐标,但这很慢,而且一定有
matlaby的方式来做...
那是...?
提前致谢
回答:
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 = [xy]; %# 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可以用作复制图像中心坐标以从对象质心减去它们的替代方法。
更多&回答...