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 ?
Update
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)=[];
Update 2 I followed the instruction in MATLAB help but it doesn't work for me. For SupNum=8 the following result has produced:
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
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-valuei
occurs next to another gray-valuej
. 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 groupi
occurring next to any gray-value in groupj
.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, labeli
might happen to the left of labelj
, but in that case it is unlikely thatj
also happens to the left ofi
. Therefore,glcms(i,j)
would have a non-zero count, butglcms(j,i)
would be zero. In the code below we overcome this by explicitly making the matrix symmetric.This is the code:
glcms
is now the adjacency matrix. The value atglcms(i,j)
is non-zero if superpixelsi
andj
are neighbors. The value indicates how large the boundary between the two superpixels is.To compute an adjacency list:
here I use peppers.png as an example image. The pixels in neighboring superpixel are depicted in
maskNeighb
variable. The only issue was adjusting parameters for graycomatrix. Perhaps you'll need different parameters for your image, but this should get you started. In the plot, the superpixel chosen should appear black, and the neighbors white.