找到两个物体的触摸像素

时间:2014-04-23 12:11:31

标签: matlab image-processing computer-vision distance

假设我有矩阵,其中该矩阵的每个单元描述二维空间中的位置(例如直方图的区间)。可以说,这些单元格中的一些包含'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.我相信这应该有效,但是看起来不是很聪明和有效。

有没有人有更好的解决方案? :) 谢谢!

2 个答案:

答案 0 :(得分:1)

使用形态学操作 让M成为你的矩阵,零(没有对象),两个,表示不同对象的位置。

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

答案 1 :(得分:1)

<强>代码

%%// Label matrix
L = [
    0 0 2 0 0;
    2 2 2 1 1;
    2 2 1 1 0
    0 1 1 1 1]

[X_row,X_col] = find(L==1);
[Y_row,Y_col] = find(L==2);

X = [X_row X_col];
Y = [Y_row Y_col];

%%// You code works till this point to get X and Y

%%// Peform subtractions so that later on could be used to detect 
%%// where Y has any index that touches X

%%// Subtract all Y from all X. This can be done by getting one
%%//of them and in this case Y into the third dimension and then subtracting
%%// from all X using bsxfun. The output would be used to index into Y.
Y_touch = abs(bsxfun(@minus,X,permute(Y,[3 2 1])));

%%// Perform similar subtractions, but this time subtracting all X from Y
%%// by putting X into the third dimension. The idea this time is to index
%%// into X.
X_touch = abs(bsxfun(@minus,Y,permute(X,[3 2 1]))); %%// for X too

%%// Find all touching indices for X, which would be [1 1] from X_touch.
%%// Thus, their row-sum would be 2, which can then detected and using `all`
%%// command. The output from that can be "squeezed" into a 2D matrix using
%%// `squeeze` command and then the touching indices would be any `ones`
%%// columnwise.
ind_X = any(squeeze(all(X_touch==1,2)),1)

%%// Similarly for Y 
ind_Y = any(squeeze(all(Y_touch==1,2)),1)

%%// Get the touching locations for X and Y
touching_loc = [X(ind_X,:) ; Y(ind_Y,:)]

%%// To verify, let us make the touching indices 10
L(sub2ind(size(L),touching_loc(:,1),touching_loc(:,2)))=10

<强>输出

L =
     0     0     2     0     0
     2     2     2     1     1
     2     2     1     1     0
     0     1     1     1     1

L =
     0     0    10     0     0
     2    10    10    10     1
    10    10    10    10     0
     0    10    10     1     1