考虑到我有个形象

使用shape (240, 320, 4),并在将slic方法函数应用于superpixels之后。
我想在(第2段)中调整superpixels的大小。

为了有相同的形状。
如何将这些片段提升到所有的超像素大小,可以插入到边缘的零?
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时,它们有不同的大小
(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?萨纳克斯
发布于 2022-09-10 18:25:46
如果我正确理解了你的问题,你希望你的超像素大小相同。
编辑2:我刚刚看到你的pixels_per_sp是一维数组,在它的末尾放置零点是没有意义的,因为它中没有任何空间结构。我确实调整了我的答案,以检索超像素连同空间邻域信息。
所以我建议你做以下几件事:
superpixels
的数据。
# 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。
https://stackoverflow.com/questions/73667746
复制相似问题