我试图将类型从Float64转换为Float32的阈值数组(隔离森林的pickle文件从scikit学习)
for i in range(len(tree.tree_.threshold)):
tree.tree_.threshold[i] = tree.tree_.threshold[i].astype(np.float32)
Run Code Online (Sandbox Code Playgroud)
然后打印它
for value in tree.tree_.threshold[:5]:
print(type(value))
print(value)
Run Code Online (Sandbox Code Playgroud)
我得到的输出是:
<class 'numpy.float64'>
526226.0
<class 'numpy.float64'>
91.9514312744
<class 'numpy.float64'>
3.60330319405
<class 'numpy.float64'>
-2.0
<class 'numpy.float64'>
-2.0
Run Code Online (Sandbox Code Playgroud)
我没有得到正确的转换到Float32.我想将值及其类型转换为Float32,有没有人有解决方法?
因此,我尝试使用此代码显示仅黑白图像的二进制图片:
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()
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:
>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).
Run Code Online (Sandbox Code Playgroud)