简体   繁体   中英

Select area (square) with spicific measure from an image using skimage python

I'm trying to extract 3 areas from the image (lungs image) these areas are indicated in the soft tissues, each area will be a square of specific height and width eg 10mm for width and height, as shown in the image below,

提取 3 个 10mm 区域的肺图像

as seen in the image also the area is Homogeneous which mean it is only include the same color (soft tissues in the case)

How to do it using python lib like skimage or cv2?

The following code will extract the region of interest (ROI) within the red rectangular annotations. Adjust the color as needed.

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('https://i.stack.imgur.com/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]

Which gives you the following images of the marked subcutaneous fatty tissue:

在此处输入图像描述 在此处输入图像描述 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM