代码之家  ›  专栏  ›  技术社区  ›  Joseph

获取每个超级像素的RGB像素值列表

  •  0
  • Joseph  · 技术社区  · 6 年前

    l具有尺寸为(224224,3)的RGB图像。l使用SLIC算法对其进行超像素分割。

    具体如下:

    img= skimageIO.imread("first_image.jpeg")
    print('img shape', img.shape) # (224,224,3)
    segments_slic = slic(img, n_segments=1000, compactness=0.01, sigma=1) # Up to 1000 segments
    segments_slic.shape
    (224,224)
    

    返回的段数为:

    np.max(segments_slic)
    Out[49]: 595
    

    从0到595。因此,我们有596个超级像素(区域)。

    让我们看看 segments_slic[0]

    segments_slic[0]
    Out[51]: 
    array([ 0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
            1,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  3,  3,  3,  3,  3,
            3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,  5,  5,
            5,  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  6,  6,  7,  7,  7,  7,
            8,  8,  8,  8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9,  9,  9,
           10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12,
           12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14,
           14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16,
           16, 16, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18,
           18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 20, 20,
           20, 20, 20, 20, 20, 20, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
           21, 21, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23,
           23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 25, 25, 25, 25,
           25, 25, 25])
    

    我想要什么?

    对于每个超级像素区域,按如下所示制作两个阵列:

    1) 数组:包含属于同一超级像素的像素的索引。

    例如

    superpixel_list[0] 包含属于超级像素0的所有像素索引。

    superpixel_list[400] 包含属于superpixel 400的像素的所有索引

    2) superpixel\u pixel\u values[0]:包含像素值(在 RGB )属于超级像素0的像素数。

    例如,假设像素0、24、29、53属于超级像素0。然后我们得到

    superpixel[0]= [[223,118,33],[245,222,198],[98,17,255],[255,255,0]]# RGB values of pixels belonging to superpixel 0
    

    高效/优化的方法是什么?(因为我有l个要循环的图像数据集)

    编辑-1

    def sp_idx(s, index = True):
         u = np.unique(s)
             if index:
         return [np.where(s == i) for i in u]
             else:
         return [s[s == i] for i in u]
         #return [s[np.where(s == i)] for i in u] gives the same but is slower
    superpixel_list = sp_idx(segments_slic)
    superpixel      = sp_idx(segments_slic, index = False)
    

    在里面 superpixel_list 我们应该得到一个包含属于同一超像素的像素索引的列表。 例如 superpixel\u列表[0] 应获取受影响像素的所有像素索引,以达到超级像素0

    然而,我得到以下信息:

    superpixel_list[0]
    Out[73]: 
    (array([ 0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,
             3,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  4,  4,  4,  5,  5,  5,
             5,  5,  5,  5,  5,  6,  6,  6,  6,  6,  6,  6,  6,  7,  7,  7,  7,
             7,  7,  7,  8,  8,  8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9, 10,
            10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13]),
     array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5,
            6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6,
            7, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 0, 1,
            2, 3, 4, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2]))
    

    例如,在superpixel[0]中,我们应该得到受supepixel 0影响的每个像素的RGB像素值,如下所示: 例如,像素0、24、29、53受超级像素0的影响,然后:

    superpixel[0]= [[223,118,33],[245,222,198],[98,17,255],[255,255,0]]
    

    但是,当我使用你的函数时,我会得到以下结果:

    superpixel[0]
    Out[79]: 
    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
           0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    

    谢谢你的帮助

    1 回复  |  直到 6 年前
        1
  •  1
  •   Daniel F    6 年前

    可以使用 np.where 以及由此产生的指数。

    def sp_idx(s, index = True):
         u = np.unique(s)
         return [np.where(s == i) for i in u]
    
    superpixel_list = sp_idx(segments_slic)
    superpixel      = [img[idx] for idx in superpixel_list]