I'm trying to do histogram equalization to all images read from a *.nii.gz file.
I have tried this 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())
With this output:
Width: 240
Height: 240
Depth: 48
Dimension: 3
Pixel ID: 8
Pixel ID Type: 32-bit float
But when I try to do histogram equalization with OpenCV I get errors:
images_array = sitk.GetArrayFromImage(images)
gray = cv2.cvtColor(images_array[24], cv2.COLOR_BGR2GRAY)
Output:
error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function 'cv::impl::{anonymous}::CvtHelper<VScn, VDcn, VDepth, sizePolicy>::CvtHelper(cv::InputArray, cv::OutputArray, int) [with VScn = cv::impl::{anonymous}::Set<3, 4>; VDcn = cv::impl::{anonymous}::Set<1>; VDepth = cv::impl::{anonymous}::Set<0, 2, 5>; cv::impl::{anonymous}::SizePolicy sizePolicy = (cv::impl::<unnamed>::SizePolicy)2u; cv::InputArray = const cv::_InputArray&; cv::OutputArray = const cv::_OutputArray&]'
> Invalid number of channels in input image:
> 'VScn::contains(scn)'
> where
> 'scn' is 1
So, I have tried this other code:
images_array = sitk.GetArrayFromImage(images)
#gray = cv2.cvtColor(images_array[24], cv2.COLOR_BGR2GRAY)
output = cv2.equalizeHist(images_array[24])
But I get this error:
error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/histogram.cpp:3429: error: (-215:Assertion failed) _src.type() == CV_8UC1 in function 'equalizeHist'
How can I do histogram equalization to those DICOM images (maybe not using OpenCV, and using instead SimpleITK)?
UPDATE:
When I run this command:
print(images_array[24].shape, images_array[24].dtype)
I get this:
(240, 240) float32
print(images_array[24].shape, images_array[24].dtype)
? – HansHirsecv2.COLOR_BGR2GRAY
, because it is already gray and does not have 3 channels. And you can't docv2.equalizeHist
because it is not 8 bit greyscale... Please print what HansHirse recommend to see what is happening. – api55