【问题标题】:How do I move from image segmentation to performing operations on my ROI after performing Felzenszwalb segmentation in scikit?在 scikit 中执行 Felzenszwalb 分割后,如何从图像分割转移到对 ROI 执行操作?
【发布时间】:2022-01-05 19:08:52
【问题描述】:

在 Python 中,我使用背景去除工具和 scikit 的 Felzenszwalb 算法来分割我的图像

from rembg.bg import remove
import numpy as np
import io
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

input_path = 'in.jpg'
output_path = 'out.png'

f = np.fromfile(input_path)
result = remove(f)
img = Image.open(io.BytesIO(result)).convert("RGBA")

#importing needed libraries
import skimage.segmentation
from matplotlib import pyplot as plt

#performing segmentation
res1 = skimage.segmentation.felzenszwalb(img, scale=500)

##uncomment to print results if desired
#fig = plt.figure(figsize=(12, 5))
#ax1 = fig.add_subplot(121)
#ax1.imshow(res1); ax1.set_xlabel("k=500")
#fig.suptitle("Graph based image segmentation")
#plt.tight_layout()

这为我提供了几种不同颜色的分段图像图。输出 res1 是一个 ndarray “指示段标签的整数掩码”(来自 scikit 网站)。我现在想对这些区域中的每一个进行一些颜色分析。我将如何使用res1 依次访问每个蒙版(具有均匀或透明背景)并执行我的颜色分析?

【问题讨论】:

    标签: python scikit-learn image-segmentation


    【解决方案1】:

    当您使用 Numpy 时,您可以使用 np.where 访问它

    在你用res1得到的区域中,选择你要分析的区域(你可以用np.unique(res1)列出区域)。例如对于掩码“2”,请执行以下操作:

    masked_img_2 = np.where(res1==2,img,0)
    plt.imshow(masked_img_2) # if you want to visualize the result with matplotlib
    

    这意味着:“在 res1 等于 '2' 的地方保持 img,在其他地方将其设置为零” https://numpy.org/doc/stable/reference/generated/numpy.where.html

    【讨论】:

      猜你喜欢
      • 2011-05-03
      • 1970-01-01
      • 2012-05-15
      • 2020-02-10
      • 2015-04-28
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      相关资源
      最近更新 更多