简体   繁体   English

在 scikit 中执行 Felzenszwalb 分割后,如何从图像分割转移到对 ROI 执行操作?

[英]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.

相关问题 使用 scikit-image 旋转 ROI 后,如何用特定颜色填充图像的 rest? - How do I fill in the rest of an image with a certain color after rotating an ROI with scikit-image? 如何在执行操作后从numpy数组中“删除”掩码? - How to “remove” mask from numpy array after performing operations? 为什么在通过mahotas转换后通过scikit图像对二值图像执行直方图均衡化而得到黑色图像? - Why performing an histogram equalization by scikit image to a binary image i got a black image after mahotas conversion? 如何使用skimage.segmentation.felzenszwalb获取围绕轮廓的框? - How to get box around contour using skimage.segmentation.felzenszwalb? 在 Django 模型中执行算术运算后,如何使用获得的新值更新模型中整数的值? - After performing arithmetic operations within a django model, how can I update the value of an integer in my model using the new value I obtained? 如何重新索引我的数据框并在执行重新索引的同时对该数据应用一些操作或转换 - How do I reindex my dataframe and also apply some of the operations or transformation on that data at the same time i am performing reindexing scikit图像分割和粒度分析 - Segmentation and Granulometry with scikit-image 在SeriesGroupBy上执行apply()后如何“重新组合”我的系列? - How do I “re-group” my Series after performing an apply() on a SeriesGroupBy? 如何在3D数组中返回Scikit图像分割? - How to return Scikit-image Segmentation in 3D array? 图像分割后如何校正边界 - How can I correct the boundaries after image segmentation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM