[英]How do I move from image segmentation to performing operations on my ROI after performing Felzenszwalb segmentation in scikit?
In Python, I am using a background removal tool and scikit's Felzenszwalb algorithm to segment my image via在 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()
which gives me a plot of the segmented image in a few different colors.这给了我一个 plot 在几个不同的 colors 中的分段图像。 The output res1
is an ndarray "integer mask indicating segment labels" (from the scikit website). output res1
是一个 ndarray “指示段标签的整数掩码”(来自 scikit 网站)。 I now want to perform some color analysis on each of these regions.我现在想对这些区域中的每一个进行一些颜色分析。 How would I use res1
to access each mask in turn (with either a homogeneous or transparent background) and perform my color analysis?我将如何使用res1
依次访问每个蒙版(具有均匀或透明背景)并执行我的颜色分析?
As you are using Numpy, you can access it with np.where
当您使用 Numpy 时,您可以使用np.where
访问它
Among the regions you obtained with res1, choose the one you want to analyse (you can list the regions with np.unique(res1)
).在您使用 res1 获得的区域中,选择您要分析的区域(您可以使用np.unique(res1)
列出区域)。 For example for mask '2', do:例如对于掩码“2”,请执行以下操作:
masked_img_2 = np.where(res1==2,img,0)
plt.imshow(masked_img_2) # if you want to visualize the result with matplotlib
this means: "keep img where res1 is equal to '2', elsewhere set it to Zero" https://numpy.org/doc/stable/reference/generated/numpy.where.html这意味着:“在res1等于 '2' 的地方保持 img,在其他地方将其设置为零”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.