1

bit of a simple question perhaps but I am not making progress and would appreciate help.

I have a list of size 422. Within index 0 there are 135 file paths to .dcm images. For example '~/images/0001.dcm','~/images/0135.dcm' Within index 1, there are 112 image paths, index 2 has 110, etc.

All images are of size 512 x 512. I am looking to re-size them to be 64 x 64.

This is my first time working with both image and .dcm data so I'm very unsure about how to resize. I am also unsure how to access and modify the files within the 'inner' list, if you will.

Is something like this way off the mark?

IMG_PX_SIZE = 64 
result = []
for i in test_list:
    result_inner_list = []        
    for image in i:
    # resize all images at index position i and store in list
        new_img = cv2.resize(np.array(image.pixel_array (IMG_PX_SIZE,IMG_PX_SIZE))
        result_inner_list.append(new_img)
    # Once all images at index point i are modified, append them these to a master list.
    result.append(result_inner_list)
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
  • what do you want to do after ? If you resize like that you should verify some dicom field, image position, pixel spacing etc ... the best way I guess is to reopen the dicom sequences in software such as itk-snap or slicer 3d etc ... and check if images are superposed. I am not sure to have understood the index organization. why can't you use dicom tags to sort the different sequences ? – p.deman Jun 07 '18 at 08:52
  • @p.deman Thanks for the reply - the data was downloaded from a medical data hosting site. It's my first time working with .dcm files so I am a bit shakey on the technical details of the format. Essentially I have a list of size 422 which represents 422 patients. For each of these patients, there is about 90 - 140 paths to their respective .dcm files from their 3d scan. I am training a CNN and would like tor resize the images from 512 x 512 to perhaps 64 x 64. In essence, I have a list, and within each index, is another list of paths to DCM which I would like to iteratively open and resize. – Ciaran De Ceol Jun 07 '18 at 09:08
  • all files of all patients are in the same folder or you have subfolders per patient? if all files are in the same folder I would suggest to do a first run to split correctly the sequences according to the tags. If Dicom are anonymized you can use the tag: Series Instance UID and study ID for example. and you generate a list of file paths per sequences. to resize, don't forget to modify the dicom tags corresponding if you resave as dicom files. such as rows, columns, pixel spacing, maybe image position, I think this will not be done by cv2.resize – p.deman Jun 07 '18 at 09:10
  • it depends of the complexity of what you want to do. you could always use the "index" that you mentioned but it's not the "proper way" when using dicom. we use the tags. – p.deman Jun 07 '18 at 09:16
  • @p.deman I wasn't aware of this. Ultimately there really is no harm in keeping them in the 512X512 format but that seems rather large to me seen as, for example, one patient may be of size 512 x 512 x 135 (i.e.135 slices of the 3d image). To me,this seems rather large for training a neural network. – Ciaran De Ceol Jun 07 '18 at 09:28
  • maybe these links are usefull to you: https://github.com/ben-heil/DICOM-CNN and https://pythonprogramming.net/3d-convolutional-neural-network-machine-learning-tutorial/ I have not much experience in neural network so I can't tell for the image size. if you don't resave as dicom, you don't care of the dicom tags pixelspacing etc ... these are mainly for "registration/visualisation" of series together with conventional dicom viewer – p.deman Jun 07 '18 at 09:40

1 Answers1

1

You seem to be struggling with two issues:

  • accessing the file paths
  • resize

For you to win, better separate these two tasks, sample code below

IMG_PX_SIZE = 64 

def resize(image):
    # your resize code here similar to:        
    # return v2.resize(np.array(image.pixel_array(IMG_PX_SIZE,IMG_PX_SIZE))
    pass

def read(path):
    # your file read operation here
    pass


big_list = [['~/images/0001.dcm','~/images/0135.dcm'],
            ['~/images/0002.dcm','~/images/0136.dcm']]

resized_images = [[resize(read(path)) for path in paths] for paths in big_list]
Evgeny
  • 4,173
  • 2
  • 19
  • 39
  • Thanks for that answer. I have one query though about your code. In the function `def read(path) :`, what exactly is the purpose? is the `for path in paths] for paths in big_list` portion of this code `[[resize(read(path)) for path in paths] for paths in big_list]` not accessing the required elements? – Ciaran De Ceol Jun 07 '18 at 09:42
  • if understand your task correctly you, you have a list of fielnames, which are strings like '~/images/0001.dcm'. You have to read ththe fiel inself into array of pixels, that is the purpose of `read()` function. as noted above you shoudl be able to get desired result for one fielname first, eg `resize(read('~/images/0001.dcm'))`, check it works as you intended, maybe even with some `assert` statements and then turn to the task of how to make this working operation work on all paths in list (or list of lists). sorry it sounds long – Evgeny Jun 07 '18 at 09:48
  • Unfortunately I am still receiving errors trying to collapse these images down in size. Attribute or Type Errors depending on what way I am trying to resize. I am not familiar with .dcm but it seems to be behaving different from standard .jpeg for example – Ciaran De Ceol Jun 07 '18 at 11:16
  • Essentially just your own code! `ds = pydicom.read_file(path)` in the read function. ` ds.Rows, ds.Columns = 64,64` in the write function within a ` try` statement with an ` except` for ` attribute error` . I'm not sure is it possible to resize .dcm this way. – Ciaran De Ceol Jun 07 '18 at 11:59
  • This is a little hard to do in remote debugging, you either need someone who is familiar with file format (ask new question), or a sample `.dcm ` file is needed to test your code (rpovide a link) – Evgeny Jun 07 '18 at 12:10
  • Absolutely no pressure whatsoever, but there is a batch of .dcm files available here if you want to take a look. Again - no pressure. And thank you. https://drive.google.com/open?id=1lq-c6NqrT3LIJIN3M9BA6Dn2tmupztWC – Ciaran De Ceol Jun 07 '18 at 12:22