Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
我正在使用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可以用作复制图像中心坐标以从对象质心减去它们的替代方法。 更多&回答... |
![]() |
![]() |