3

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 e.g. 10mm for width and height, as shown in the image below,

Lungs image with extracted 3 areaes of 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?

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
NeX'T'iME
  • 176
  • 3
  • 14

1 Answers1

3

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:

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

asdf
  • 1,006
  • 1
  • 9
  • Great. One suggestion, you convert the image to another color space like HSV or LAB to easily segment red color – Jeru Luke Jun 10 '22 at 20:22
  • 1
    Yeah sure, especially to segment color on a grayscale image HSV might be very useful. But since the color is plain old red, RGB seems to be enough. – asdf Jun 10 '22 at 22:35
  • @asdf is it possible to get any area in the homogeneous color, but not have it manually selected like in the image? randomly select areas (sqares) from the soft tissue (https://prnt.sc/kLv99spgDeG6 soft tissue area) – NeX'T'iME Jun 12 '22 at 11:37
  • To do that you would probably need the original CT data with Hounsfield density values, because you lose a lot of information in a windowed PNG image. Then you could select the density range of subcutaneous fatty tissue to generate a mask of that range. This won't be perfect, since there might be interindividual differences or conditions like inflammation or anasarca that alter the density range. To demonstrate the process on your PNG (as said, not optimal!): `cv2.inRange(ndimage.gaussian_filter(np_img[..., 0], sigma=3.), 220, 230).astype('bool')` gives you https://i.stack.imgur.com/u4BnW.png – asdf Jun 12 '22 at 18:18
  • @asdf I have the original data CT (as DICOM ext) but I don't know how to handle such a thing like that if there is a link or a video or any way to a tutorial on how to do it or something similar that would be cool!! – NeX'T'iME Jun 12 '22 at 19:44
  • I don't know about good tutorial videos, but when you know what you want to do you usually find it with a google query. What I usually do is: Convert the DICOM-files to Nifti (with tools such as `dcm2niix` or pypi's `dicom2nifti`), which are much more pleasant to work with (gives you 3D arrays for normal CT scans). You can load Niftis using the excellent `nibabel` python library (they also have great tutorials on their website). For image processing I mostly use `scipy.ndimage`, `skimage`, `cv2` and `numpy`, which will give you plenty of tools to work with. It's mostly learning by doing! – asdf Jun 12 '22 at 19:59
  • @asdf thank you so much, I do search for them and try the best I can – NeX'T'iME Jun 12 '22 at 20:14
  • Good luck then! :) – asdf Jun 12 '22 at 20:26