1

I am trying to apply some blur using a low pass filter to a dicom image, however my resulting dicom image is not correct (see image below) (all data below is publicly available)

from scipy import fftpack
import numpy as np
import imageio
from PIL import Image, ImageDraw
import numpy as np
import pydicom

def test(matrix):
    image1_np = matrix #read_xray2("./CT000000.dcm")
    #fft of image
    fft1 = fftpack.fftshift(fftpack.fft2(image1_np))

    #Create a low pass filter image
    x,y = image1_np.shape[0],image1_np.shape[1]
    #size of circle
    e_x,e_y=50,50
    #create a box
    bbox=((x/2)-(e_x/2),(y/2)-(e_y/2),(x/2)+(e_x/2),(y/2)+(e_y/2))

    low_pass=Image.new("L",(image1_np.shape[0],image1_np.shape[1]),color=0)

    draw1=ImageDraw.Draw(low_pass)
    draw1.ellipse(bbox, fill=1)

    low_pass_np=np.array(low_pass)

    #multiply both the images
    filtered=np.multiply(fft1,low_pass_np)

    #inverse fft
    ifft2 = np.real(fftpack.ifft2(fftpack.ifftshift(filtered)))
    ifft2 = np.maximum(0, np.minimum(ifft2, 255))

    return ifft2

dicom = pydicom.dcmread("./CT000000.dcm")
dicom.PixelData = test(dicom.pixel_array)
dicom.save_as(r"./result.dcm")

Original Image original image

Resulting Image resulting image

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • Does this help? https://stackoverflow.com/a/37056792/758174 – Pierre D Aug 11 '22 at 12:44
  • @PierreD I guess that no, because the library PIL can't read a dicom file and I didn't find a low pass filter in this library. – Henrique Vital Aug 11 '22 at 12:50
  • Oh, but you could easily get your numpy array into a PIL image (see e.g. [here](https://stackoverflow.com/a/10967471/758174)). That will also reveal any alignment problems, which would be good to catch early (e.g. colormap not quite what's expected --that can make applying FFT from first principles much trickier). Once in PIL, you should have a much easier time trying various filters and see what works best for you. – Pierre D Aug 11 '22 at 12:54
  • My code in reality is a little weird, if I save the resulting matrix as jpg it works (image with blur), but if I save as dcm file no. – Henrique Vital Aug 11 '22 at 12:55
  • Rule of thumb: try to nail the conversion to/from PIL into whatever you need. Then work in PIL for all filtering and other operations. This will make your life much easier. – Pierre D Aug 11 '22 at 12:56
  • I suspect that you need to modify some of the DICOM image-related parameters before saving. If there is any change in bit-depth or resolution, pydicom does not adjust those simply on setting the pixel data, you would have to do that yourself (although it should have at least warned if the size of pixel data didn't match the expected size from those parameters). – darcymason Aug 11 '22 at 19:08
  • I fixed the problem using the GaussianBlur of CV2 library (low pass filter) – Henrique Vital Aug 11 '22 at 20:41
  • dicom.PixelData = cv2.GaussianBlur(dicom.pixel_array, (7, 7), 0) – Henrique Vital Aug 11 '22 at 20:41

1 Answers1

1

I fixed the code using the GaussianBlur of cv2 library

dicom = pydicom.dcmread("./CT000000.dcm")
dicom.PixelData = cv2.GaussianBlur(dicom.pixel_array, (7, 7), 0)

#save the image
dicom.save_as(r"./result.dcm")