图像中超像素的相邻和非相邻超像素 [英] Adjacent and non-adjacent superpixels for an superpixel in an image
问题描述
将图像分割成N个超像素后,我需要指定与一个超像素相邻或不相邻的超像素,并确定所有超像素的这种关系.
After segmenting an image into N superpixels, I need to specify the superpixels that are adjacent or non-adjacent to one superpixel and determine this relationship for all superpixels.
[L,NumLabels] = superpixels(A,200);
如何为每个超像素指定相邻的超像素?
How can I specify the adjacent superpixels for each of superpixels ?
更新
我尝试了@Cris Luengo引入的解决方案.但是出现了以下错误:
I have tried the solution @Cris Luengo introduced. However the following errors arised :
B=imread('H.jpg');
[L,N] = superpixels(B,200);
glcms=graycomatrix(L);
k=glcms(:,50); %SupNum=50
[r,~]=find(k>0);
aa=find(r==50);
r(aa)=[];
更新2 我按照MATLAB帮助中的说明进行操作,但对我不起作用. 对于SupNum = 8,产生了以下结果:
Update 2 I followed the instruction in MATLAB help but it doesn't work for me. For SupNum=8 the following result has produced:
推荐答案
在 graycomatrix
是解决此问题的好方法.但是,这些答案是不完整的.
In answers to this question on MATLAB Answers it is hinted that graycomatrix
is a good way to solve this problem. However, those answers are incomplete.
graycomatrix
需要几个参数来完成我们需要做的事情.它计算灰度值共生矩阵.这是一个矩阵,表示在单元格(i,j)
中,灰度值i
与另一个灰度值j
相邻出现的频率.可以在此函数中定义下一个"关系.默认情况下,graycomatrix
返回一个8x8矩阵,它将矩阵中的所有灰度值归为8个bin,并查找组i
中任何出现在组j
中任何灰度值旁边的灰度值.
graycomatrix
requires several arguments to do what we need it to do. It computes a gray-value co-occurrence matrix. This is a matrix that says, in cell (i,j)
, how often a gray-value i
occurs next to another gray-value j
. The "next to" relationship can be defined in this function. By default, graycomatrix
returns an 8x8 matrix, where it bins all gray-values in the image into 8 bins, and looks for any gray-value in group i
occurring next to any gray-value in group j
.
因此,我们需要在此共生矩阵中将超像素图像中的每个标签都分开(有N
个不同的标签或灰度值).我们还需要将"next to"关系指定为[1,0]
或[0,1]
,即水平或垂直彼此相邻的两个像素.当指定两个相邻"关系时,我们以3D矩阵的形式返回两个共现矩阵.还要注意,共生矩阵不是对称的,在我们的超像素图像中,标签i
可能会出现在标签j
的左边,但是在那种情况下j
也不太可能也会出现在j
的左边c3>.因此,glcms(i,j)
的计数将为非零,而glcms(j,i)
的计数将为零.在下面的代码中,我们通过显式使矩阵对称来克服此问题.
So we need to keep each label in our superpixel image separate in this co-occurrence matrix (there are N
different labels, or gray-values). We also need to specify the "next to" relationship to be either [1,0]
or [0,1]
, i.e. two pixels next to each other horizontally or vertically. When specifying two "next to" relations, we get two co-occurrence matrices back, in the form of a 3D matrix. Note also that the co-occurrence matrix is not symmetric, in our superpixel image, label i
might happen to the left of label j
, but in that case it is unlikely that j
also happens to the left of i
. Therefore, glcms(i,j)
would have a non-zero count, but glcms(j,i)
would be zero. In the code below we overcome this by explicitly making the matrix symmetric.
这是代码:
B = imread('kobi.png'); % using one of MATLAB's standard images
[L,N] = superpixels(B,200);
glcms = graycomatrix(L,'NumLevels',N,'GrayLimits',[1,N],'Offset',[0,1;1,0]);
glcms = sum(glcms,3); % add together the two matrices
glcms = glcms + glcms.'; % add upper and lower triangles together, make it symmetric
glcms(1:N+1:end) = 0; % set the diagonal to zero, we don't want to see "1 is neighbor of 1"
glcms
现在是邻接矩阵.如果超像素i
和j
是相邻像素,则glcms(i,j)
处的值不为零.该值指示两个超像素之间的边界有多大.
glcms
is now the adjacency matrix. The value at glcms(i,j)
is non-zero if superpixels i
and j
are neighbors. The value indicates how large the boundary between the two superpixels is.
要计算邻接表:
[I,J] = find(glcms); % returns coordinates of non-zero elements
neighbors = [J,I]
这篇关于图像中超像素的相邻和非相邻超像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!