分段图像中特定段的邻居

时间:2014-04-07 09:46:59

标签: matlab image-processing

考虑分割算法的输出是与输入图像大小相同的矩阵。对于检测到的每个段,矩阵的特定部分用特定的数字/索引编制索引,请看下面的内容。

我想检索特定细分的邻居。 Here描述了以不同大小拍摄每个像素的邻居。例如,3x3,5x5,7x7,e.t.c。我们是否可以通过上述链接中的时间效率方式对不同大小(近似)的邻域中的特定段执行类似的过程?

致以最诚挚的问候,

透特

enter image description here

PS:任何问题都非常感谢。

1 个答案:

答案 0 :(得分:1)

如果要查找特定区域的邻居的索引,可以执行以下操作:

%# input: lblImg - image where pix(x,y) is the index of the segment
targetSegment = 4;
%# grow the segment by 2 pixels, since I assume the boundary in between is 1 pixel wide
msk = imdilate(lblImg == targetSegment,strel('disk',2));
msk(lblImg == targetSegment | lblImg == 0) = false; %# remove original cell, and boundary

%# retrieve the list of neighbors
listOfNeighbors = unique(lblImg(msk));

当然,您可以并行地为所有段执行此操作,并创建邻接矩阵。膨胀是局部最大运算,因此它会使不同指数的标签重叠。

 dilImg = imdilate(lblImg, strel('disk',2));
 msk = dilImg ~= lblImg & lblImg > 0; %# assume indices are all positive

 %# msk contains the indices of pixels where dilation
 %# has created an overlap between segments.

 rowColIdx = unique( [dilImg(msk), lblImg(msk)], 'rows');

 %# create adjacency matrix. Due to the nature of imdilate, this will fill in 
 %# only the values below the diagonal.
 %# adjacencyMatrix = adjacencyMatrix + adjacencyMatrix.' would fix that.
 nLabels = max(lblImg(:));
 adjacencyMatrix = sparse(rowColIdx(:,1),rowColIdx(:,2),ones(size(rowColIdx,1),1),...
   nLabels, nLabels);