Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: OpenCV depth of image unsupported (CV_64F)

So, I'm trying to show a binary picture with only black and white using this code:

import cv2
import numpy as np

x_img = cv2.imread("lenac.tif")

x_img_g = cv2.cvtColor(x_img, cv2.COLOR_BGR2GRAY)

y = x_img_g > 128

cv2.imshow("", y*1.0)
cv2.waitKey(0)
cv2.destroyAllWindows()

But I'm getting this error:

>Traceback (most recent call last):
File "ex5.py", line 11, in <module>
cv2.imshow("", y*1.0)
cv2.error: OpenCV(4.0.0) c:\projects\opencv- 
python\opencv\modules\imgproc\src\color.hpp:261: error: (-2:Unspecified 
error) >in function '__cdecl cv::CvtHelper<struct 
cv::Set<1,-1,-1>,struct cv::Set<3,4,-1>,struct 
cv::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).
like image 404
Guilhermeffable Avatar asked Mar 12 '19 18:03

Guilhermeffable


2 Answers

try cv2.imshow("", y.astype('float32')) or cv2.imshow("", y.astype('uint8') * 255)

CV_64F means the numpy array dtype is float64 (64-bit floating-point).

opencv imshow only works with float32 (32-bit floating point) where the range for the pixel values is 0.0-1.0 or uint8 (unsigned 8-bit) where the range is 0-255

Since y was a bool, converting it to a number means converting True to 1

for float32, that is fine because 1 is the maximum for the imshow range

if you use uint8, that means you're trying to display pixels of value 1/255 (since the maximum imshow range for uint8 is 255) which will be barely visible, so you can multiply by 255 to bring those pixels to max and appear as bright white pixels

like image 167
Ta946 Avatar answered Nov 01 '22 18:11

Ta946


Just change the type of NumPy array since the image is nothing but NumPy array

cv2.imshow("image", image.astype(np.uint8))

It worked fine for me.

like image 2
Asadullah Avatar answered Nov 01 '22 19:11

Asadullah