I need help with displaying the image. Image shape is (86, 512, 512). I have tried using solutions from other similar questions posted here on stackoverflow, but nothing worked so far.

Code:

import nibabel as nib
from google.colab.patches import cv2_imshow

img = nib.load('/path/to/image/imaging.nii.gz')
img = img.get_fdata()
cv2_imshow(img)

Error:

<ipython-input-39-61f19508a742> in <module>()
     05 img = img.get_fdata()
---> 06 cv2_imshow(img)

/usr/local/lib/python3.7/dist-packages/google/colab/patches/__init__.py in cv2_imshow(a)
     26       a = cv2.cvtColor(a, cv2.COLOR_BGRA2RGBA)
     27     else:
---> 28       a = cv2.cvtColor(a, cv2.COLOR_BGR2RGB)
     29   display.display(PIL.Image.fromarray(a))
     30 

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<3, 4>; 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 512

If I try passing the Nifti1Image object directly without the code line img = img.get_fdata(), it throws:

AttributeError: 'Nifti1Image' object has no attribute 'clip'

I've also tried resizing the image using cv2.resize(), but still the same result.

What is going wrong? How can I fix it?

Thanks a lot for your time.

0

There are 0 answers