0

I have a dicom file from which I read images. The images I read, however, has incorrect colormap. Ideally, the image should look like:

However, the following code only gives me

If I only take the red component, I get the image below, which is not correct and cannot be adjusted to the ideal result in any colormap I tried.

or

root = tk.Tk()
root.withdraw()
path = filedialog.askopenfilename()
ds = dicom.dcmread(path, force = True) # reads a file data set
video = ds.pixel_array #reads a sequence of RGB images
plt.imsave(some_path, video[0], format='png') #gives image [2]

What have I done wrong?

温泽海
  • 216
  • 3
  • 16
  • I have tried all possible colormaps as indicated. There is a mistake in my process of dicom reading. Please reopen the question. – 温泽海 Jan 01 '22 at 19:58
  • I want to but I really don't know how. I want to just upload my little dicom file as an attachment but I don't know how to do it. – 温泽海 Jan 01 '22 at 20:02
  • I have updated the post. The image after "or" is the image if I set colormap to be grey. As I said it does not work (I need exactly the colormap in the first image). I must have made some mistake in reading the image than in making plt plotting it. Observe that the first image has the right side and left side in somehow "different" colormaps. It must be read that way rather than saved that way. – 温泽海 Jan 01 '22 at 20:16
  • Some related posts mention that some image formats have `bgr` instead of `rgb`. You might try to exchange red and blue in your image. – JohanC Jan 01 '22 at 20:22
  • See also [Trying to input a RGB fMRI DICOM image, ...](https://stackoverflow.com/questions/70427525/trying-to-input-a-rgb-fmri-dicom-image-modify-it-and-save-it-as-a-grayscale-di) which seems to indicate that reading a Dicom image might be black magic. – JohanC Jan 01 '22 at 20:30
  • Thank you! You just gave me the most helpful hint! A lot of progress is made. It turns out that the image turns out to be in LAB format and I am supposed convert it to RGB first. – 温泽海 Jan 01 '22 at 21:40

1 Answers1

3

This really looks like YCbCr data, is the Photometric Interpretation something like YBR_FULL? If so then as mentioned in the documentation you need to apply a colour space conversion, which in pydicom is:

from pydicom import dcmread
from pydicom.pixel_data_handlers import convert_color_space

ds = dcmread(...)
rgb = convert_color_space(ds.pixel_array, "YBR_FULL", "RGB")
scaramallion
  • 1,251
  • 1
  • 6
  • 14