假设我有一个矩阵,其中该矩阵的每个单元描述了二维空间中的一个位置(例如直方图的一个容器)。比方说,其中一些单元格包含“1”,一些单元格包含“2”,分别表示对象号 1 和 2 所在的位置。
我现在想找到那些描述两个对象之间“接触点”的单元格。我如何有效地做到这一点?
这是一个简单的解决方案:
X = locations of object number 1 (x,y)
Y = locations of object number 2 (x,y)
distances = pdist2(X,Y,'cityblock');
位置 (x,y) 和 (u,v) 接触,当且仅当距离的相应条目为 1 时。我相信这应该可行,但似乎不是很聪明和高效。
谁有更好的解决方案? :) 谢谢!
最佳答案
使用形态学运算。
令 M
为矩阵,其中 0(无对象)、1 和 2 表示不同对象的位置。
M1 = M == 1; % create a logical mask of the first object
M2 = M == 2; % logical mask of second object
dM1 = imdilate( M1, [0 1 0; 1 1 1; 0 1 0] ); % "expand" the mask to the neighboring pixels
[touchesY touchesX] =...
find( dM1 & M2 ); % locations where the expansion of first object overlap with second one
关于matlab - 定位两个物体的接触像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23243864/