8

I want to read some DICOM files, so I'm testing pydicom for my work, which I think is considerably useful.

Now I want to load existing DICOM files, replace the pixel data array with another pixel array (e.g. pre-processing or literally another DICOM pixel array) and most of all, I want to read it again with any DICOM viewer application.

For this test, I used the tutorial code below. This code loads a test data file. The size of image is 64*64. The code below does sub-sampling from the original data. After that, the size of image is 8*8, and the result is saved to after.dcm.

But when I read the file using a DICOM viewer app (I used 'Dicompass'), the size of DICOM image is still 64*64. What is it that I'm missing?

I referred to the pydicom documentation (http://pydicom.readthedocs.io/en/stable/getting_started.html, https://pydicom.github.io/pydicom/stable/index.html) to solve my problem.

# authors : Guillaume Lemaitre <g.lemaitre58@gmail.com>
# license : MIT

import pydicom
from pydicom.data import get_testdata_files

print(__doc__)

# FIXME: add a full-sized MR image in the testing data
filename = get_testdata_files('MR_small.dcm')[0]
ds = pydicom.dcmread(filename)

# get the pixel information into a numpy array
data = ds.pixel_array
print(data)

print('The image has {} x {} voxels'.format(data.shape[0],
                                        data.shape[1]))
data_downsampling = data[::8, ::8]
print('The downsampled image has {} x {} voxels'.format(
    data_downsampling.shape[0], data_downsampling.shape[1]))

# copy the data back to the original data set
ds.PixelData = data_downsampling.tostring()
# update the information regarding the shape of the data array
ds.Rows, ds.Columns = data_downsampling.shape

# print the image information given in the dataset
print('The information of the data set after downsampling: \n')
print(ds)
print(ds.pixel_array)
print(len(ds.PixelData))
ds.save_as("after.dcm")
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
user8697183
  • 123
  • 1
  • 2
  • 6
  • Works for me with python 3.6 and pydicom v1.0.1rc1. I'm able to view it in Dicompass. It has 8x8 pixels displayed and the dicom tags viewer shows Rows and Columns as 8. Are you sure you are not trying to view a previous version of the file or something like that? – darcymason Feb 20 '18 at 04:48
  • Thank you, darcymason. I didn't understand `Dicompass` tool enough. Do you have licence for Dicompass? My Dicompass tool doesn't allow me to show two DICOM images consecutively whose patient info is same each other. – user8697183 Feb 20 '18 at 08:17

1 Answers1

5

The code looks OK. But, you are not overwriting original file.

You load the file with:

filename = get_testdata_files('MR_small.dcm')[0]
ds = pydicom.dcmread(filename)

where original file name is "MR_small.dcm".

Then you save the file with:

ds.save_as("after.dcm")

where destination file name is different. That means, original file is still unchanged.

You should either load "after.dcm" in your DICOM viewer to test

OR

You should overwrite the file (pydicom.filewriter.dcmwrite) while saving it.


Not a part of your problem, but if you are creating copy of original image with change in pixel data, it is recommended that you also modify instance specific information in dataset like some UIDs and InstanceNumber (0020,0013), SOPInstanceUID (0008,0018) etc.

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • Thank you for your kind answer. you are right, the code has no problem as you said. the problem was on me. I am aware that I didn't understand DICOM tool enough. when showing a dicom file using Dicompass, there's no problem. but after that, if trying to show other DICOM file whose patient info is same with the first one, second command is ignored. when I tried to open the two DICOM files respectively, it's been finally confirmed that the pixel array has been changed well.. I'm embarrassed and thank you.. – user8697183 Feb 20 '18 at 08:11