Search code examples
pythonopencvimagingpydicommedical-imaging

adding an overlay on a DICOM image using open CV


I am trying to create a layer on a DICOM image, below code works fine for jpg/png images but not for DICOM.

import cv2
import numpy as np
import pydicom as dicom

ds=dicom.dcmread('D0009.dcm')
img=ds.pixel_array
blank = np.zeros(shape=(img.shape[0],img.shape[1],3), dtype=np.uint8)
font = cv2.FONT_HERSHEY_SIMPLEX  
cv2.putText(blank,  
            text='Logo',  
            org=(img.shape[1]//8, img.shape[0]//2),   
            fontFace=font,  
            fontScale= 2,color=(163,163,163),  
            thickness=11,  
            lineType=cv2.LINE_4)  
blend=cv2.addWeighted(img,0.7,blank,1, 0, dtype = cv2.CV_32F)  
cv2.imshow('sample image dicom',blend)
cv2.waitKey()

any help would be apreciated


Solution

  • I was able to get this working by normalizing the value range of the DICOM image and converting the DICOM image from greyscale to RGB image. Replace your line

    img=ds.pixel_array
    

    with these lines:

    img = np.array(ds.pixel_array, dtype='float32')
    img /= np.max(img)
    img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)