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

时间:2022-10-31 13:19:56

l have an RGB image of dimension (224,224,3). l applied superpixel segmentation on it using SLIC algorithm.

我有一个维度(224,224,3)的RGB图像。 l使用SLIC算法对其进行超像素分割。

As follow :

如下 :

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)

Number of returned segments are :

返回的段数是:

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

From 0 to 595. So, we have 596 superpixels (regions).

从0到595.所以,我们有596个超像素(区域)。

Let's take a look at segments_slic[0]

我们来看看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])

What l would like to get ?

我想得到什么?

for each superpixel region make two arrays as follow:

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

1) Array : contain the indexes of the pixels belonging to the same superpixel.

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

For instance

superpixel_list[0] contains all the indexes of the pixels belonging to superpixel 0 .

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

superpixel_list[400] contains all the indexes of the pixels belonging to superpixel 400

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

2)superpixel_pixel_values[0] : contains the pixel values (in RGB) of the pixels belonging to superpixel 0.

2)superpixel_pixel_values [0]:包含属于超像素0的像素的像素值(RGB)。

For instance, let's say that pixels 0, 24 , 29, 53 belongs to the superpixel 0. Then we get

例如,假设像素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

What is the efficient/optimized way to do that ? (Because l have l dataset of images to loop over)

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

EDIT-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)

In superpixel_list we are supposed to get a list containing the index of pixels belonging to the same superpixel. For instance superpixel_list[0] is supposed to get all the pixel indexes of the pixel affected to superpixel 0

在superpixel_list中,我们应该得到一个包含属于同一个超像素的像素索引的列表。例如,superpixel_list [0]应该获得受超像素0影响的像素的所有像素索引

however l get the following :

但我得到以下内容:

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]))

Why two arrays ?

为什么两个阵列?

In superpixel[0] for instance we are supposed to get the RGB pixel values of each pixel affected to supepixel 0 as follow : for instance pixels 0, 24 , 29, 53 are affected to superpixel 0 then :

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

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

However when l use your function l get the following :

但是当我使用你的函数时,我得到以下内容:

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])

Thank you for your help

谢谢您的帮助

1 个解决方案

#1


1  

Can be done using np.where and the resulting indices.

可以使用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]

#1


1  

Can be done using np.where and the resulting indices.

可以使用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]