gpt4 book ai didi

python - 使用skimage python从图像中选择具有特定度量的区域(正方形)

转载 作者:行者123 更新时间:2023-12-05 03:23:42 28 4
gpt4 key购买 nike

我正在尝试从图像(肺部图像)中提取 3 个区域,这些区域在软组织中显示,每个区域都是具有特定高度和宽度的正方形,例如宽高各10mm,如下图,

Lungs image with extracted 3 areaes of 10mm

如图所示,该区域也是均匀的,这意味着它只包含相同的颜色(在这种情况下是软组织)

如何使用像 skimage 或 cv2 这样的 python 库来完成它?

最佳答案

以下代码将提取红色矩形注释内的感兴趣区域 (ROI)。根据需要调整颜色。

import requests
from io import BytesIO

import numpy as np
import scipy.ndimage as ndimage
from PIL import Image

# replace with your image loading routines
img = Image.open(BytesIO(requests.get('/image/7k5VO.png').content))
np_img = np.asarray(img)

red_parts = (np_img == (255, 0, 0, 255)).all(axis=-1) # find red parts of RGBA image
filled_rects = ndimage.binary_fill_holes(red_parts) # fill the rects to get a binary mask of ROI
rects_inner = filled_rects ^ red_parts # remove the red border
labelled_rects, num_rects = ndimage.label(rects_inner) # label/number each individual rect

ROIs = []
for i in range(1, num_rects + 1): # 0 is background
current_mask = labelled_rects == i
x, y = np.where(current_mask) # indices where current_mask is True (= area of interest)
sub_image = np_img[x.min():x.max(), y.min():y.max()] # extract image data inside rects
sub_image = sub_image[1:-1, 1:-1] # remove remaining red border (might be an image compression artefact, needs testing)
ROIs.append(sub_image)
pil_imgs = [Image.fromarray(roi) for roi in ROIs]

这为您提供了以下标记的皮下脂肪组织图像:

enter image description here enter image description here enter image description here

关于python - 使用skimage python从图像中选择具有特定度量的区域(正方形),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72460884/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com