【问题标题】:Adjacent and non-adjacent superpixels for an superpixel in an image图像中超像素的相邻和非相邻超像素
【发布时间】:2019-07-24 09:11:50
【问题描述】:

将图像分割成 N 个超像素后,我需要指定与一个超像素相邻或不相邻的超像素,并确定所有超像素的这种关系。

[L,NumLabels] = superpixels(A,200);

如何为每个超像素指定相邻的超像素?

更新

我已经尝试过@Cris Luengo 介绍的解决方案。但是出现了以下错误:

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,产生了以下结果:

【问题讨论】:

标签: matlab image-processing image-segmentation matlab-coder superpixels


【解决方案1】:

在对this question on MATLAB Answers 的回答中暗示graycomatrix 是解决此问题的好方法。但是,这些答案并不完整。

graycomatrix 需要几个参数来做我们需要它做的事情。它计算一个灰度值共生矩阵。这是一个矩阵,表示在单元格(i,j) 中,灰度值i 出现在另一个灰度值j 旁边的频率。 “next to”关系可以在这个函数中定义。默认情况下,graycomatrix 返回一个 8x8 矩阵,它将图像中的所有灰度值分箱到 8 个分箱中,并在组 i 中查找与组 j 中的任何灰度值相邻的任何灰度值。

所以我们需要在这个共现矩阵中保持我们超像素图像中的每个标签分开(有N不同的标签,或灰度值)。我们还需要将“next to”关系指定为[1,0][0,1],即水平或垂直相邻的两个像素。当指定两个“next to”关系时,我们得到两个共现矩阵,以 3D 矩阵的形式。另请注意,共现矩阵不是对称的,在我们的超像素图像中,标签i 可能出现在标签j 的左侧,但在这种情况下j 不太可能也出现在@ 的左侧987654337@。因此,glcms(i,j) 将具有非零计数,但 glcms(j,i) 将为零。在下面的代码中,我们通过明确地使矩阵对称来克服这个问题。

这是代码:

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 现在是邻接矩阵。如果超像素ij 是邻居,glcms(i,j) 的值非零。该值表示两个超像素之间的边界有多大。

计算邻接表:

[I,J] = find(glcms);     % returns coordinates of non-zero elements
neighbors = [J,I]

【讨论】:

    【解决方案2】:

    这里我使用 peppers.png 作为示例图像。相邻超像素中的像素在maskNeighb 变量中描述。唯一的问题是调整 graycomatrix 的参数。也许您的图像需要不同的参数,但这应该可以帮助您入门。在绘图中,选择的超像素应显示为黑色,而相邻的超像素应显示为白色。

    B = imread('peppers.png');
    % make superpixels
    [L,N] = superpixels(B,200);
    % find neighbors for all superpixels
    glcms = graycomatrix(L,'NumLevels',N,'GrayLimits',[],'Symmetric',true);
    % find superpixels k neighboring superpixel number 50
    supNum = 50;
    k=find(glcms(:,supNum));  
    k(k == supNum) = [];
    % find pixels that are in superpixel 50
    maskPix = L == supNum;
    % find pixels that are in neighbor superpixels k
    maskNeighb = ismember(L,k);
    % plot
    maskPix3 = repmat(maskPix,1,1,3);
    maskNeighb3 = repmat(maskNeighb,1,1,3);
    Bneigbors = B;
    Bneigbors(maskPix3) = 0;
    Bneigbors(maskNeighb3) = 255;
    figure;
    imshow(Bneigbors)
    

    【讨论】:

    • 我不知道'Symmetric' 标志,这很好。但是您缺少指定 'Offset' 数组,默​​认情况下它只查看水平邻居。如果两个超像素之间只有水平边界,那么它们的像素在垂直方向上是邻居,而不是水平方向,并且此代码不会将超像素识别为邻居。
    • 顺便问一下,这个问题已经提出了一个多星期,我们同时输入答案的机会是多少? :)
    猜你喜欢
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2013-08-27
    • 2020-11-08
    • 2023-03-10
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多