I'm trying to display a DICOM image in opencv-python.I am using the pydicom library,And then adding API's to create a full fledged DICOM viewer with DOTNET, that runs python(C# calls python with process instance of course!!). I am unable to convert or see the uncompressed DICOM image. whenever i try to load or modify the pixel_array. I get error messges.
import dicom
import cv2
import numpy
df=dicom.read_file("IM-0001-0002.dcm")
df.pixel_array
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
df.pixel_array
File "C:\Python27\lib\site-packages\dicom\dataset.py", line 394, in pixel_array
return self._get_pixel_array()
File "C:\Python27\lib\site-packages\dicom\dataset.py", line 376, in _get_pixel_array
raise NotImplementedError("Pixel Data is compressed in a format pydicom does not yet handle. Cannot return array")
NotImplementedError: Pixel Data is compressed in a format pydicom does not yet handle. Cannot return array
Please suggest me good way to convert the image making cv2.imshow() function o display the image
Thanks in Advance!!
Try pydicom
One reason for the error can be: the .dcm file used may contain the format that is not supported (e.g. JPEG 2000 is not supported by pillow in case of pydicom). This issue can be solved. I was having the same issue (I am using pydicom instead of dicom) I guess you will get some direction from the solution that solved my problem:
1st Platforma Information:
I am using: pydicom to read .dcm files, Python 3.6, Anaconda and Ubuntu, 15 GB RAM
Solution:
pip install -U pydicom
.Information can be found here: (link: https://pydicom.github.io/pydicom/dev/getting_started.html)
Anaconda is necessary. Why? Please check the official doc of pydicom (https://pydicom.github.io/pydicom/dev/getting_started.html) its mentioned "To install pydicom along with image handlers for compressed pixel data, we encourage you to use Miniconda or Anaconda"
If you are using Ubuntu directly open Terminal. If you are using Windows then on Anaconda Navigator go to Environment from here start terminal. Execute the following commands on it:
pip install -U git+https://github.com/pydicom/pydicom.git
conda install pydicom --channel conda-forge
conda install -c conda-forge gdcm
Cross Check:
Now restart the notebook and then try to execute your code using pydicom. It will display the output.
Also, you can use Matplotlib to display as follows:
I hope it will help you.
You can use the python GDCM binding to decompress the file first, see for example here
Since pydicom do not support compressed dicom files, you will have to decompress it first. You can use GDCM to do that.
you have to convert in RGB before, look at that for a monochrome dicom file: https://github.com/twanmal/dicom_monochrome_to_opencv