首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对超像素进行采样,使其具有全部的形状?

如何对超像素进行采样,使其具有全部的形状?
EN

Stack Overflow用户
提问于 2022-09-09 21:40:34
回答 1查看 68关注 0票数 0

考虑到我有个形象

使用shape (240, 320, 4),并在将slic方法函数应用于superpixels之后。

我想在(第2段)中调整superpixels的大小。

为了有相同的形状。

如何将这些片段提升到所有的超像素大小,可以插入到边缘的零?

代码语言:javascript
复制
 num_segments = 400
 img = img_as_float(imread('1.png'))
 #if len(img.shape) > 2 and img.shape[-1] == 4:
     # img = img[:, :, :3]
 segments = slic(img, compactness=30, n_segments=num_segments)
 superpixels_ids = np.unique(segments)
 for id in superpixels_ids:
    pixels_per_sp = img[segments == id]
    print(pixels_per_sp.shape)

 plt.figure()
 plt.imshow(mark_boundaries(img, segments))
 plt.show()

当绘制pixels_per_sp.shape时,它们有不同的大小

代码语言:javascript
复制
(199, 3)
(203, 3)
(195, 3)
(232, 3)
(211, 3)
(211, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(202, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(210, 3)
(180, 3)
(198, 3)
(196, 3)

如果我能在每个超像素的边缘加上零,我想把超像素的升级成精确的1大小的

另一个问题:有什么方法可以让我保持RGBA的形象,并且仍然使用slic?萨纳克斯

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-10 18:25:46

如果我正确理解了你的问题,你希望你的超像素大小相同。

编辑2:我刚刚看到你的pixels_per_sp是一维数组,在它的末尾放置零点是没有意义的,因为它中没有任何空间结构。我确实调整了我的答案,以检索超像素连同空间邻域信息。

所以我建议你做以下几件事:

superpixels

  • create

  • 计算仍然适合于所有超像素的数组的最小大小,然后用零(Paddings)

  • 来初始化它,填充超级像素

的数据。

代码语言:javascript
复制
# compute max shape
max_shape = np.zeros((len(img.shape[:-1]),), dtype=int)

for id in superpixels_ids:
    # get indices of superpixel's pixels
    pixels_indices = np.argwhere(segments == id)

    # get bounding box of superpixel
    xymin = np.min(pixels_indices , axis=0)
    xymax = np.max(pixels_indices , axis=0)

    # update max shape
    max_shape = np.max(np.stack([(xymax - xymin)+1, max_shape]), axis=0)


# create your superpixels with zero paddings

new_super_pixels = np.zeros((len(superpixels_ids), ) + tuple(max_shape) + (img.shape[-1],), dtype=img.dtype)

for i, id in enumerate(superpixels_ids):
    # get indices of superpixel's pixels
    pixels_indices = np.argwhere(segments == id)

    # get bounding box of superpixel (again)
    xymin = np.min(pixels_indices, axis=0)

    # broadcast superpixel data in our new_super_pixels array (use index instead of id)
    new_super_pixels[i][tuple((pixels_indices-xymin).T)] = img[tuple(pixels_indices.T)]

现在,您得到了具有相同形状的超像素和填充的零的数组。

这对你有帮助吗?

编辑:

如果您想要使用一些插值方法来升级样本,您可能需要查看我的另一个question/contribution

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73667746

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档