Ask Your Question
0

Convert a 2D Numpy array to CV_8UC1 type

asked 2020-01-29 08:26:51 -0600

Elric gravatar image

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
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2020-01-29 08:55:41 -0600

berak gravatar image

once you have a numpy array, it's as simple as:

u8 = images_array.astype(np.uint8)
edit flag offensive delete link more

Comments

As easy as that. Thanks a lot!

Elric gravatar imageElric ( 2020-01-29 09:19:46 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-01-29 08:26:51 -0600

Seen: 15,044 times

Last updated: Jan 29 '20

Related questions