Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Convert a 2D Numpy array to CV_8UC1 type

I'm newbie with Python and Numpy.

I have read a DICOM file (*.nii.gz) into a Numpy array with the following code:

import SimpleITK as sitk
flair_file = '/content/gdrive/My Drive/Colab Notebooks/.../FLAIR.nii.gz'

images = sitk.ReadImage(flair_file)
print("Width: ", images.GetWidth())
print("Height:", images.GetHeight())
print("Depth: ", images.GetDepth())

print("Dimension:", images.GetDimension())
print("Pixel ID: ", images.GetPixelIDValue())
print("Pixel ID Type:", images.GetPixelIDTypeAsString())

It shows this output:

Width:  240
Height: 240
Depth:  48
Dimension: 3
Pixel ID:  8
Pixel ID Type: 32-bit float

And then, I convert them into a Numpy array:

images_array = sitk.GetArrayFromImage(images)
print(np.amin(images_array),np.amax(images_array))

print(images_array[24].shape, images_array[24].dtype)

With this output:

0.0 2380.6191
(240, 240) float32

I have found that I can convert it to CV_8UC1 OpenCV type, but I don't know how.

I want to convert it because I want to use it with the cv2.equalizeHist method.

What do I have to do to convert it to CV_8UC1 type?

The DICOM file has this information:

Title: FLAIR.nii.gz
Width:  230.0000 mm (240)
Height:  230.0000 mm (240)
Depth:  144.0000 mm (48)
Size:  11MB
X Resolution:  1.0435 pixels per mm
Y Resolution:  1.0435 pixels per mm
Voxel size: 0.9583x0.9583x3.0000 mm^3
ID: -2
Bits per pixel: 32 (float)
Display range: 0 - 1980.8942
Image: 1/48
No threshold
ScaleToFit: false
Uncalibrated
Path: /FLAIR.nii.gz
Screen location: 927,131 (1920x1080)
Coordinate origin:  0,0,0
No overlay
No selection