PDA

查看完整版本 : MatLab-找到最接近的值的最佳方法


poster
2019-12-14, 20:46
我正在使用Matlab的图像工具箱。特别是,在对图像进行二值化和标记后,我运行

props = regionprops(labeledImage, 'Centroid'); 获取所有连接对象的质心。现在,我想找到一个更靠近一对坐标(即图像中心)的坐标。当然,我知道我可以使用for循环检查每个props .Centroid对坐标,但这很慢,而且一定有[I]matlaby的方式来做...

那是...?

提前致谢



回答:

REGIONPROPS (http://www.mathworks.com/help/toolbox/images/ref/regionprops.html)的输出将是一个N×1结构数组,其中一个字段'Centroid'包含一个1×2数组。您可以先使用VERTCAT (http://www.mathworks.com/help/techdoc/ref/vertcat.html)函数将所有这些数组连接成N×2数组。然后,您可以使用函数REPMAT (http://www.mathworks.com/help/techdoc/ref/repmat.html)复制图像中心坐标(假定为1×2数组),使其变为N×2数组。现在,您可以使用向量化运算来计算距离,并使用MIN (http://www.mathworks.com/help/techdoc/ref/min.html)函数找到具有最小距离的值的索引:

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 (http://www.mathworks.com/help/techdoc/ref/sqrt.html) 。还要注意,功能BSXFUN (http://www.mathworks.com/help/techdoc/ref/bsxfun.html)可以用作复制图像中心坐标以从对象质心减去它们的替代方法。



更多&回答... (https://stackoverflow.com/questions/5397373)