Home > Software engineering >  trying to show image of a mammograms with pydicom
trying to show image of a mammograms with pydicom

Time:03-20

plt.imshow(datasetP2.pixel_array, cmap=plt.cm.bone)

tried this code on a file provided to me and ended up with this error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_28228/3373097283.py in <module>
----> 1 plt.imshow(datasetP2.pixel_array, cmap=plt.cm.bone)

~\anaconda3\lib\site-packages\pydicom\dataset.py in __getattr__(self, name)
    834             return {}
    835         # Try the base class attribute getter (fix for issue 332)
--> 836         return object.__getattribute__(self, name)
    837 
    838     @property

~\anaconda3\lib\site-packages\pydicom\dataset.py in pixel_array(self)
   1880             :class:`numpy.ndarray`.
   1881         """
-> 1882         self.convert_pixel_data()
   1883         return cast("numpy.ndarray", self._pixel_array)
   1884 

~\anaconda3\lib\site-packages\pydicom\dataset.py in convert_pixel_data(self, handler_name)
   1442             self._convert_pixel_data_using_handler(handler_name)
   1443         else:
-> 1444             self._convert_pixel_data_without_handler()
   1445 
   1446     def _convert_pixel_data_using_handler(self, name: str) -> None:

~\anaconda3\lib\site-packages\pydicom\dataset.py in _convert_pixel_data_without_handler(self)
   1554             .format(", ".join([str(hh) for hh in available_handlers]))
   1555         )
-> 1556         raise last_exception  # type: ignore[misc]
   1557 
   1558     def _do_pixel_data_conversion(self, handler: Any) -> None:

~\anaconda3\lib\site-packages\pydicom\dataset.py in _convert_pixel_data_without_handler(self)
   1534         for handler in available_handlers:
   1535             try:
-> 1536                 self._do_pixel_data_conversion(handler)
   1537                 return
   1538             except Exception as exc:

~\anaconda3\lib\site-packages\pydicom\dataset.py in _do_pixel_data_conversion(self, handler)
   1561         # Use the handler to get a 1D numpy array of the pixel data
   1562         # Will raise an exception if no pixel data element
-> 1563         arr = handler.get_pixeldata(self)
   1564         self._pixel_array = reshape_pixel_array(self, arr)
   1565 

~\anaconda3\lib\site-packages\pydicom\pixel_data_handlers\numpy_handler.py in get_pixeldata(ds, read_only)
    281     px_keyword = [kw for kw in keywords if kw in ds]
    282     if len(px_keyword) != 1:
--> 283         raise AttributeError(
    284             "Unable to convert the pixel data: one of Pixel Data, Float "
    285             "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```

CodePudding user response:

As the exception message says, it's likely that your dataset has no Pixel Data element in it. If you're iterating through a folder you're probably picking up a dataset without any image data. Try doing something like this:

from pathlib import Path
from pydicom import dcmread

directory = Path("/path/to/directory")
for p in directory.glob("**/*"):
    ds = dcmread(p)
    if "PixelData" not in ds:
        print(f"Dataset found with no Pixel Data: {p}")
        continue

    arr = ds.pixel_array

CodePudding user response:

The medical imagers that I'm familiar with produce 16 bit/pixel gray value images.

Generally convert from the ImageMagick suite can read them and save them as e.q. png, so you can at least view them and check for good data.

Note that on a conventional PC you will loose color resolution because PC hardware generally isn't set up in 16 bit/pixel grayscale.

  • Related