Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Performance MatLab-找到最近值的最佳方法_Performance_Matlab - Fatal编程技术网

Performance MatLab-找到最近值的最佳方法

Performance MatLab-找到最近值的最佳方法,performance,matlab,Performance,Matlab,我正在使用Matlab的图像工具箱。特别是,在对图像进行二值化和标记后,我运行 props = regionprops(labeledImage, 'Centroid'); 获取所有连接对象的质心。现在,我想找到一个更靠近一对坐标(即图像的中心)的坐标。当然,我知道我可以使用一个循环来检查每个道具[I]。质心对坐标,但这很慢,必须有一种合适的方法来完成 哪个是 提前感谢的输出将是一个N-by-1结构数组,其中一个字段“质心”包含一个1-by-2数组。您可以首先使用函数将所有这些数组连接成一个N

我正在使用Matlab的图像工具箱。特别是,在对图像进行二值化和标记后,我运行

props = regionprops(labeledImage, 'Centroid');
获取所有连接对象的质心。现在,我想找到一个更靠近一对坐标(即图像的中心)的坐标。当然,我知道我可以使用一个循环来检查每个道具[I]。质心对坐标,但这很慢,必须有一种合适的方法来完成

哪个是

提前感谢

的输出将是一个N-by-1结构数组,其中一个字段
“质心”
包含一个1-by-2数组。您可以首先使用函数将所有这些数组连接成一个N×2的数组。然后,可以使用该函数复制图像中心坐标(假定为1×2数组),使其成为N×2数组。现在,您可以使用矢量化操作计算距离,并使用函数查找具有最小距离的值的索引:

请注意,因为您只需要最小距离,所以可以使用平方距离,避免不必要的调用。还请注意,该函数可以用作复制图像中心坐标以从对象质心中减去它们的替代方法

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