0

The colormaps in the code do not take any effect. I end up with getting a blue tinted image as shown. I then converted to the RGB colorspace with convert_color_space(). The colorspace changes however, the colormaps still don't work. How do I go about this? Here is the MWE.

import matplotlib.pyplot as plt
import pydicom
import numpy as np

filename = ("89474521")
ds = pydicom.dcmread(filename)

# rgbarray = convert_color_space(ds.pixel_array, "YBR_FULL", "RGB") #to the RGB space (optional)

plt.imshow(ds.pixel_array[20],cmap="gray")
plt.show()

The input file is of the shape (24, 768, 1024, 3). So I have 24 slices but reading the 20th slice here for the example.

Some relevant ds information:

Dataset.file_meta -------------------------------
(0002, 0000) File Meta Information Group Length  UL: 198
(0002, 0001) File Meta Information Version       OB: b'\x00\x01'
(0002, 0002) Media Storage SOP Class UID         UI: Ultrasound Multi-frame Image Storage
(0002, 0003) Media Storage SOP Instance UID      UI: 1.2.840.113654.2.70.1.10.403324.30000021031806133177600000162
(0002, 0010) Transfer Syntax UID                 UI: JPEG Baseline (Process 1)
(0002, 0012) Implementation Class UID            UI: 1.2.40.0.13.1.1.1
(0002, 0013) Implementation Version Name         SH: 'dcm4che-1.4.35'
-------------------------------------------------
(0028, 0002) Samples per Pixel                   US: 3
(0028, 0004) Photometric Interpretation          CS: 'YBR_FULL_422'
(0028, 0006) Planar Configuration                US: 0
(0028, 0008) Number of Frames                    IS: '24'
(0028, 0009) Frame Increment Pointer             AT: (0018, 1065)
(0028, 0010) Rows                                US: 768
(0028, 0011) Columns                             US: 1024
(0028, 0100) Bits Allocated                      US: 8
(0028, 0101) Bits Stored                         US: 8
(0028, 0102) High Bit                            US: 7
(0028, 0103) Pixel Representation                US: 0
(0028, 0301) Burned In Annotation                CS: 'NO'
(0028, 0303) Longitudinal Temporal Information M CS: 'MODIFIED'
(0028, 2110) Lossy Image Compression             CS: '01'
(0028, 2112) Lossy Image Compression Ratio       DS: [14, 1.5]
(0028, 2114) Lossy Image Compression Method      CS: ['ISO_10918_1', 'RGB_TO_YBR']

enter image description here

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
Thangam
  • 169
  • 3
  • 11

1 Answers1

0

The matplotlib colormap only really makes sense with a scalar image, it sounds like you have a colour image so imshow just displays it directly.

You could try to take a single channel of your image, like:

plt.imshow(ds.pixel_array[20][0], cmap="gray")
plt.show()

Or convert the RBG colour into brightness (see this question):

def rgb2gray(rgb):
    # applies the formula to convert RBG into brightness
    return np.dot(rgb, [0.2989, 0.5870, 0.1140])

img = ds.pixel_array[20]
img_gray = rgb2gray(img)    

plt.imshow(gray, cmap="gray")
plt.show()
Robbie
  • 4,672
  • 1
  • 19
  • 24