0
votes

This is my first post, I hope I have followed convention.

I've found a lot of success with pydicom, but am stuck on one particular application. I would like to do the following:

  1. Read in dicom to numpy array
  2. Reshape to (frames, rows, columns, pixels)
  3. Do some processing including cropping and converting to grayscale
  4. Output as new dicom file

I use

r = ds.Rows
c = ds.Columns
f = ds.NumberOfFrames
s = ds.SamplesPerPixel
imageC = np.reshape(img,(f,r,c,s), order='C')

to get the initial numpy matrix I want and do the processing. I have confirmed that these steps look reasonable.

Prior to saving the new dicom, I update the ds Rows and Columns with the new correct dimensions and set SamplesPerPixels to 1. I then reshape the numpy matrix before reassigning to PixelData with .tostring().

np.reshape(mat, (p, f, r, c), order='C')

The resulting image is nonsensical (green) in my dicom viewer. Are there any obvious logical mistakes? I can provide more code if it would be of use.

1

1 Answers

0
votes

I am rather guessing, as I have not used pydicom for witing files. Anyway, if the original image is an RGB one and you convert it to grayscale, than you should change the Media Storage SOP Class UID of the image so that the viewer can interpret it properly. Can you check the value? It is under tag (0002,0002). Here is the list. It is possible that there are more tags to change. Can you dump both files and show us differences?

By the way, from your post it seems that you import the image by ds.PixelData. Why don't you use ds.pixel_array? Then you wouldn't need to reshape.