Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

numpy - 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:projectsopencv- 
pythonopencvmodulesimgprocsrccolor.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).
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

CV_64F means the numpy array 'dtype' is 64bit floating-point opencv only works with 'float32' (32-bit floating point) where image range for imshow is 0.0-1.0 or 'uint8' (unsigned 8-bit) 0-255

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

for float32, that is fine because 1 is max for imshow range

if you use uint8, that means your trying to display pixels of value 1/255 which will be barely visible, so you can multiply by 255 to bring those pixels to max and appear as bright white pixels


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...