MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   MatLab-找到最接近的值的最佳方法 (https://www.labfans.com/bbs/showthread.php?t=26693)

poster 2019-12-14 20:46

MatLab-找到最接近的值的最佳方法
 
我正在使用Matlab的图像工具箱。特别是,在对图像进行二值化和标记后,我运行

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

那是...?

提前致谢



[B]回答:[/B]

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

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 请注意,由于只需要最小距离,因此可以使用平方距离,并避免不必要地调用[URL="http://www.mathworks.com/help/techdoc/ref/sqrt.html"]SQRT[/URL] 。还要注意,功能[URL="http://www.mathworks.com/help/techdoc/ref/bsxfun.html"]BSXFUN[/URL]可以用作复制图像中心坐标以从对象质心减去它们的替代方法。



[url=https://stackoverflow.com/questions/5397373]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:39

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.