python:不支持图像的OpenCV深度(CV_64F) [英] 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).
推荐答案
尝试cv2.imshow("", y.astype('float32'))
或cv2.imshow("", y.astype('uint8') * 255)
CV_64F表示numpy数组'dtype'为64位浮点 opencv仅适用于"float32"(32位浮点),其中imshow的图像范围为0.0-1.0或"uint8"(无符号8位)0-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
由于y是布尔值,所以转换为数字意味着将True转换为1
Since y was a bool, converting to a number means converting True to 1
对于float32,这很好,因为显示范围最大为1
for float32, that is fine because 1 is max for imshow range
如果使用uint8,则意味着您尝试显示几乎不可见的值1/255的像素,因此您可以乘以255以使这些像素达到最大值并显示为亮白色像素
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
这篇关于python:不支持图像的OpenCV深度(CV_64F)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!