2

I am trying to superimpose a CT .nii image and its mask in another color (possibly red). This is easily achievable for example with imageJ, thanks to the "Merge Channels" functionality. What I would like to obtain looks like this:

this

given my original image:

original image

and its mask:

its mask

So basically I need to "convert to red" my mask and superimpose it on my grayscale image.

I've looked into SimpleITK (this is how I made the contour mask) but I can't proceed forward from here. Can anyone help?

Bilal
  • 3,191
  • 4
  • 21
  • 49
Sala
  • 480
  • 4
  • 19
  • Are you open to answers using PIL? OpenCV? ImageMagick? – Mark Setchell Jul 15 '22 at 11:20
  • @MarkSetchell OpenCV does not support .nii images – Sala Jul 15 '22 at 14:30
  • 1
    As you posted a JPEG and a PNG, I assume you are able to convert `.nii` images to that format which can then be processed by **OpenCV**. Your question and title imply the problem is with *"merging"* rather than *"opening"*. Please click [edit] and share your latest/greatest code. Thank you. – Mark Setchell Jul 15 '22 at 16:00
  • No, those were just screenshots. Nifti images have slices, so they are 3D and obviously they can't be visualized on stack overflow. I attached the screenshots just to clarify what I needed – Sala Jul 16 '22 at 17:12
  • @sala separate the slices [example](https://stackoverflow.com/questions/61833375/loading-a-nifti-through-nibabel-and-using-the-shape-function/61834369#61834369), convert them to `PNG` format, Gray to BGR color conversion, then simply superimpose the mask as a red Chanel. – Bilal Jul 16 '22 at 20:36
  • I don't have all the time in the universe to wait... :| If I go slice by slice it takes ages but I was looking for something more pythonic – Sala Jul 16 '22 at 23:11
  • This is easily done in SimpleITK. Please see the toolkit's jupyter notebook repository, specifically [05_Results_Visualization.ipynb](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/master/Python/05_Results_Visualization.ipynb). – zivy Jul 21 '22 at 17:38

1 Answers1

1

Here's a SimpleITK script that overlays a mask on an image:

import SimpleITK as sitk

img = sitk.ReadImage("ct.jpg")
mask = sitk.ReadImage("mask.png")

# Extract one channel since the images are actually RGB
img = sitk.VectorIndexSelectionCast(img, 0)
mask = sitk.VectorIndexSelectionCast(mask, 0)

mask = mask>200

red = [255, 0, 0]

color_overlay = sitk.LabelMapOverlay( sitk.Cast(mask, sitk.sitkLabelUInt8),
    img, opacity=0.5, colormap=red )

sitk.WriteImage(color_overlay, "overlay.png")

And here's resulting output image: enter image description here

Note that the script first extracts one channel from image and mask to create grayscale images (since your examples were RGB). Also, it does a threshold on the mask to create a binary mask (0s and 1s).

Dave Chen
  • 1,905
  • 1
  • 12
  • 18