donut
donut

Reputation: 636

cv2.imshow shows binary image

I have a code in which the matrix hip_dir represents a RGB image. If I check its shape, python returns (1080, 1920, 3). If I write

cv2.imwrite('hip_dir.jpg',hip_dir)

I get the correct image saved in the folder. However, when I do

cv2.imshow('img0',hip_dir)
cv2.waitKey(0)
cv2.destroyAllWindows()

I get the same image, in black and white (there are some blue pixels, however, making this even stranger).

How can I use imshow correctly?

Edit:

Using the command cv2.cvtColor results in the following error:

error: OpenCV(4.1.0) c:\projects\opencv- 
python\opencv\modules\imgproc\src\color.simd_helpers.hpp:94: error:         
(-2:Unspecified error) in function '__cdecl cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0xe227985e::Set<3,4,-1>,struct cv::impl::A0xe227985e::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
Unsupported depth of input image:
 'VDepth::contains(depth)'
where
 'depth' is 6 (CV_64F)

Edit2: I discovered that the reason for this error to occur is simple because the matrix is in np.float64. Having it converted to np.float32, however, does not change the imshow result.

Upvotes: 0

Views: 6352

Answers (2)

donut
donut

Reputation: 636

So guys, I discovered what was wrong: the correct way to disply the RGB colors in the imshow command is forcing the correspondent matrix of the image to be in np.uint8 format. I tested the other formats, and none of them worked properly. I don't know why, though.

Upvotes: 1

mark
mark

Reputation: 91

OpenCV expects the image to be in BGR format, try applying

converted = cv2.cvtColor(hip_dir,cv2.COLOR_BGR2RGB)

Upvotes: 1

Related Questions