谢谢@abarnert!这就是我要从 sklearn.datasets.fetch_olivetti_faces() 之类的数据集中加载图像的内容,该数据集具有 dtype 的 float32 和值范围从 0-1 以与使用 dtype 的 OpenCV 一起使用987654324@,取值范围为 0-255。
import numpy as np
import cv2 as cv
from sklearn import datasets
data = datasets.fetch_olivetti_faces()
image = data.images[15]
image
array([[0.6694215 , 0.6818182 , 0.7066116 , ..., 0.5082645 , 0.55785125,
0.58677685],
[0.677686, 0.70247936, 0.71487606, ..., 0.5289256, 0.5495868,
0.58264464],
[0.6983471, 0.7107438, 0.70247936, ..., 0.5495868, 0.55785125,
0.5785124],
...,
[0.59917355, 0.59917355, 0.54545456, ..., 0.10743801, 0.11157025,
0.10330579],
[0.59090906, 0.6198347, 0.5785124, ..., 0.11157025, 0.10743801,
0.10743801],
[0.5661157, 0.6280992, 0.59917355, ..., 0.11157025, 0.11157025,
0.10743801]], dtype=float32)
img = (image * 255).round().astype(np.uint8)
img
array([[171, 174, 180, ..., 130, 142, 150],
[173, 179, 182, ..., 135, 140, 149],
[178, 181, 179, ..., 140, 142, 148],
...,
[153, 153, 139, ..., 27, 28, 26],
[151, 158, 148, ..., 28, 27, 27],
[144, 160, 153, ..., 28, 28, 27]], dtype=uint8)
img 现在已准备好在 cv 库中进行进一步处理。