所有形心坐标组合之间的成对距离-Matlab [英] Pairwise distance between all centroid coordinate combinations - Matlab

查看:202
本文介绍了所有形心坐标组合之间的成对距离-Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的脚本根据图像的质心来标记图像中的对象.我如何在所有形心坐标组合之间预成两两的距离(不重叠)并将结果返回到表中?我当时正在考虑使用pdist2,但我不知道如何构造for循环.

The script below label objects in an image based on their centroids. How can I preform a pairwise distance between all the centroid coordinate combinations (without overlapping) and return the results in a table? I was thinking about using pdist2 but I do not know how to construct the for loop.

谢谢.

脚本:

clc;
clear all;
I = imread('E:/Elli.png');
imshow(I);
BW  =imbinarize(I);
BW = imfill(BW, 'holes');
BW = bwlabel(BW); 

s = regionprops(BW,'Area', 'BoundingBox', 'Eccentricity', 'MajorAxisLength', 'MinorAxisLength', 'Orientation', 'Perimeter','Centroid');


imshow(BW)
hold on
for k = 1:numel(s)
    c = s(k).Centroid;
    text(c(1), c(2), sprintf('%d', k), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'middle');
end
hold off 

推荐答案

使用

Rather than using pdist2, which finds the distances between points in two lists, use pdist. pdist takes all of the points in a single list and finds the distance between each pair.

在您的示例中,s.Centroid提供了逗号分隔的点列表,因此我们需要使用

In your example, s.Centroid gives a comma-separated list of points, so we need to coerce it into an array of points using vertcat:

point_list = vertcat(s.Centroid);

现在我们可以将此列表提供给pdist:

Now we can feed this list to pdist:

dist_list = pdist(point_list);

pdist返回从质心i到质心j的距离列表,以使i < j:

pdist returns a list of distances from centroid i to centroid j such that i < j:

{1->2}, {1->3}, {1->4}, ... {1->n}, {2->3}, {2->4}, ..., {2->n}, {3->4}, etc.

在您的示例中,有28个质心,这将使您总共有378个距离.

In your example with 28 centroids, this will give you a total of 378 distances.

如果要以矩阵形式而不是元素(i,j)是质心i到质心j的距离,则可以使用

If you want that in a matrix form instead where element (i,j) is the distance from centroid i to centroid j, you can use squareform:

dist_mat = squareform(dist_list);

对于28个质心,这将为您提供完整的28x28距离矩阵.

For 28 centroids, this will give you the full 28x28 matrix of distances.

这篇关于所有形心坐标组合之间的成对距离-Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆