Where is my image data matrix?

418 views
Skip to first unread message

xtsl

unread,
Aug 4, 2021, 4:51:59 PM8/4/21
to pydicom
Hi All,
Where is my image data matrix?
My code is as following:

import os
import xlsxwriter 
import pydicom 
import glob
import pandas as pd
from pydicom import dcmread
import numpy

#os.chdir(r'Y:\pharmascan\huang\186RNL\01_019\baseline')

rootdir = r'Y:\pharmascan\huang\186RNL\01_019\baseline'

#patients = defaultdict(list)

for file in os.listdir(rootdir):
    d = os.path.join(rootdir, file)
    if os.path.isdir(d):
        #print(os.getcwd()) #list present directory
        os.chdir(d)        #change prenent directory
        #print(d)
        #dcm_path = d+'\*.dcm'
        for filename in glob.glob("*.dcm"):  # or whatever your pattern is
            #print(filename)
            ds=dcmread(filename, stop_before_pixels=True)
            #print(ds)
            len(ds.paxel_array)

The error message is as following when I run my above code.--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-26-bb29837531c1> in <module> 26 ds=dcmread(filename, stop_before_pixels=True) 27 #print(ds) ---> 28 len(ds.paxel_array) ~\Anaconda3\lib\site-packages\pydicom\dataset.py in __getattr__(self, name) 833 return {} 834 # Try the base class attribute getter (fix for issue 332) --> 835 return object.__getattribute__(self, name) 836 837 @property AttributeError: 'FileDataset' object has no attribute 'paxel_array'

How to extract my image data matrix?

Thanks,

Qiang

Reese Haywood

unread,
Aug 4, 2021, 4:54:33 PM8/4/21
to pyd...@googlegroups.com
Looks like a typo

len(ds.paxel_array) should be len(ds.pixel_array)
> --
> You received this message because you are subscribed to the Google Groups "pydicom" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to pydicom+u...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/pydicom/e187b9f4-6a25-42d7-9ed4-ffedc86c7c17n%40googlegroups.com.



--
*********************************************************
Please direct all e-mails to
reese....@gmail.com

Visit me at Clinical Medical Physics.
clinicalmedicalphysics.org
*********************************************************

xtsl

unread,
Aug 5, 2021, 11:51:09 AM8/5/21
to pydicom
Hi Reese,

I revised it. There is still problem. 


import os
import xlsxwriter 
import pydicom 
import glob
import pandas as pd
from pydicom import dcmread
import numpy

#os.chdir(r'Y:\pharmascan\huang\186RNL\01_019\baseline')

rootdir = r'Y:\pharmascan\huang\186RNL\01_019\baseline'

#patients = defaultdict(list)

for file in os.listdir(rootdir):
    d = os.path.join(rootdir, file)
    if os.path.isdir(d):
        #print(os.getcwd()) #list present directory
        os.chdir(d)        #change prenent directory
        #print(d)
        #dcm_path = d+'\*.dcm'
        for filename in glob.glob("*.dcm"):  # or whatever your pattern is
            #print(filename)
            ds = dcmread(filename, stop_before_pixels=True)
            print(len(ds.pixel_array))
            #first_array = ds.pixel_array.astype(numpy.float32)




the error message is as following:


AttributeError Traceback (most recent call last) <ipython-input-42-0141790e2c29> in <module> 25 #print(filename) 26 ds = dcmread(filename, stop_before_pixels=True) ---> 27 print(len(ds.pixel_array)) 28 #first_array = ds.pixel_array.astype(numpy.float32) ~\Anaconda3\lib\site-packages\pydicom\dataset.py in __getattr__(self, name) 833 return {} 834 # Try the base class attribute getter (fix for issue 332) --> 835 return object.__getattribute__(self, name) 836 837 @property ~\Anaconda3\lib\site-packages\pydicom\dataset.py in pixel_array(self) 1714 :class:`numpy.ndarray`. 1715 """ -> 1716 self.convert_pixel_data() 1717 return self._pixel_array 1718 ~\Anaconda3\lib\site-packages\pydicom\dataset.py in convert_pixel_data(self, handler_name) 1433 self._convert_pixel_data_using_handler(handler_name) 1434 else: -> 1435 self._convert_pixel_data_without_handler() 1436 1437 def _convert_pixel_data_using_handler(self, name: str) -> None: ~\Anaconda3\lib\site-packages\pydicom\dataset.py in _convert_pixel_data_without_handler(self) 1543 .format(", ".join([str(hh) for hh in available_handlers])) 1544 ) -> 1545 raise last_exception 1546 1547 def _do_pixel_data_conversion( ~\Anaconda3\lib\site-packages\pydicom\dataset.py in _convert_pixel_data_without_handler(self) 1523 for handler in available_handlers: 1524 try: -> 1525 self._do_pixel_data_conversion(handler) 1526 return 1527 except Exception as exc: ~\Anaconda3\lib\site-packages\pydicom\dataset.py in _do_pixel_data_conversion(self, handler) 1552 # Use the handler to get a 1D numpy array of the pixel data 1553 # Will raise an exception if no pixel data element -> 1554 arr = handler.get_pixeldata(self) 1555 self._pixel_array = reshape_pixel_array(self, arr) 1556 ~\Anaconda3\lib\site-packages\pydicom\pixel_data_handlers\numpy_handler.py in get_pixeldata(ds, read_only) 275 px_keyword = [kw for kw in keywords if kw in ds] 276 if len(px_keyword) != 1: --> 277 raise AttributeError( 278 "Unable to convert the pixel data: one of Pixel Data, Float " 279 "Pixel Data or Double Float Pixel Data must be present in " AttributeError: Unable to convert the pixel data: one of Pixel Data, Float Pixel Data or Double Float Pixel Data must be present in the dataset

Reese Haywood

unread,
Aug 5, 2021, 12:16:06 PM8/5/21
to pyd...@googlegroups.com
Hmm, without the data, I can't test. You are stopping the dataset read
before the pixel array. Try removing the stop_before_pixels from the
dcmread.

This is how I have done it in a previous application.

https://gist.github.com/reesehaywood/6094543fec3dff375e6840ab72c27af6

Reese
> To view this discussion on the web visit https://groups.google.com/d/msgid/pydicom/c378b25e-53dd-486c-aa12-a92cb881fd1cn%40googlegroups.com.

xtsl

unread,
Aug 5, 2021, 3:51:41 PM8/5/21
to pydicom
Hi Reese,

It could run  after  remove the stop_before_pixels from the
dcmread.

import os
import xlsxwriter 
import pydicom 
import glob
import pandas as pd
from pydicom import dcmread
import numpy
from os.path import dirname, join
from pprint import pprint

#os.chdir(r'Y:\pharmascan\huang\186RNL\01_019\baseline')

rootdir = r'Y:\pharmascan\huang\186RNL\01_019\baseline'

#patients = defaultdict(list)

for file in os.listdir(rootdir):
    d = os.path.join(rootdir, file)
    if os.path.isdir(d):
        print(os.getcwd()) #list present directory
        os.chdir(d)        #change prenent directory
        #print(d)
        #dcm_path = d+'\*.dcm'
        for filename in glob.glob("*.dcm"):  # or whatever your pattern is
            #print(filename)
            #ds = dcmread(filename, stop_before_pixels=True)
            ds = dcmread(filename)
            #for element in ds:
                #print(element)
            #print(len(ds.pixel_array))
            print(ds.pixel_array.shape)

There is still problem.
My data is 3D. It has 32 slices.

The results are as following:
Y:\pharmascan\huang\186RNL\01_019\baseline\Series1 
(320, 240) 
(320, 240) 
(320, 240)
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240)
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240) 
(320, 240)

Obviously,   ds = dcmread(filename) and print(ds.pixel_array.shape) ran 32 times.
How let ds = dcmread(filename) run only time and there is a 320*240*32 image data in ds?

Thanks,

Shiliang

Reese Haywood

unread,
Aug 5, 2021, 4:13:21 PM8/5/21
to pyd...@googlegroups.com
I don't think pydicom reads 3D data sets directly. Someone else would
have to answer that. I have only ever used it for reading 2D arrays,
then manually putting them into a 3D array. I think you can follow the
pattern in the GIST I linked before.

Reese
> To view this discussion on the web visit https://groups.google.com/d/msgid/pydicom/1e42778e-0530-4e14-b72e-80b02eccbf8cn%40googlegroups.com.

xtsl

unread,
Aug 5, 2021, 4:31:36 PM8/5/21
to pydicom
Hi Reese,

Thank you very much.

Qiang

Reply all
Reply to author
Forward
0 new messages