Fuse (blend) two 2D images to RGB image in SimpleITK

(Ibraheem) #1

Dear all,

I am trying to write a python code that extracts specific 2D slices from a 3D image and its segmentation then generates RGB image that shows both image and its segmentation in the extracted slice.
So far everything works but I am wondering if I can enhance the for loops part in the fusion section e.g. by numpy indexing or something similar.

import numpy as np, SimpleITK as sitk
fnmA = "img.png" 
readerImg = sitk.ImageFileReader() ; readerImg.SetFileName(fnmA) ;
img =        readerImg.Execute();
a = sitk.GetArrayFromImage(img);

fnmB = "seg.png"    
readerSeg = sitk.ImageFileReader() ; readerSeg.SetFileName(fnmB) ; 
seg =  readerSeg.Execute();
b= sitk.GetArrayFromImage(seg);

maxJ=a.shape[0] ; print("max J: " +str(maxJ))
maxK=a.shape[0] ; print("max K: " +str(maxK))
img2d = sitk.Image([c.shape[1],c.shape[0]], sitk.sitkVectorUInt8, 3)
for j in range(0,maxJ):
     for k in range (0,maxK):
          v= [int(a[j][k]),int(a[j][k]),int(a[j][k])]
          vc=[150,80,100]
          img2d.SetPixel(k,j,v)           
          if b[j][k]== 0:  
             img2d.SetPixel(k,j,vc)
          #endif
      #endfor k
 #endfor j
 writerImg = sitk.ImageFileWriter()
 fnm ="rgb.png" 
 writerImg.SetFileName(fnm) 
 writerImg.Execute(img2d)

rgb

(Ziv Yaniv) #2

Hello @ibr,

This is not the way to go. There are a variety of filters in SimpleITK that will allow you to combine an image and segmentation for visualization purposes, the most straightforward is LabelOverlayImageFilter. Please take a look at this Jupyter notebook which illustrates various filters and approaches for visualizing results from segmentation and registration using SimpleITK.

Hopefully one of these is what you are looking for. If not, let us know what is missing.

3 Likes
(Ibraheem) #3

Thanks a lot. This was helpful. Here is the modified code:

import numpy as np, SimpleITK as sitk
fnmA = "img.png" 
readerImg = sitk.ImageFileReader() ; readerImg.SetFileName(fnmA) ;
img =        readerImg.Execute();

fnmB = "seg.png"    
readerSeg = sitk.ImageFileReader() ; readerSeg.SetFileName(fnmB) ; 
seg =  readerSeg.Execute();

pink= [255,105,180] ;     green = [0,255,0] ;     gold = [255,215,0]
rgb = sitk.LabelOverlay(img, seg,opacity=0.5, backgroundValue= -1.0, colormap=pink+green+gold)   

writerImg = sitk.ImageFileWriter()
fnm ="rgb.png" 
writerImg.SetFileName(fnm) 
writerImg.Execute(rgb)

rgb

2 Likes