我正在尝试使用多种学习方法减小尺寸以将RGB图像转换为灰色。
我已将图像转换为 numpy 数组 (image_array)
import numpy as np
from sklearn.datasets import load_sample_image
china = load_sample_image("china.jpg")
# Convert to floats instead of the default 8 bits integer coding. Dividing by
# 255 is important so that plt.imshow behaves works well on float data (need to
# be in the range [0-1]
china = np.arraychina, dtype=np.float64) / 255
# Load Image and transform to a 2D numpy array.
w, h, d = original_shape = tuple(china.shape)
assert d == 3
image_array = np.reshape(china, (w * h, d))
检查image_array
image_array.shape
(273280, 3)
尝试时,
X, color = image_array
我得到
值错误:要解压缩的值太多。
有没有办法解决这个问题?
你可以做
china = (china[:,:,:3] * [0.2989, 0.5870, 0.1140]).sum(axis=2)
这就是我在 diy 机器学习中所做的(我发现纯 numpy 比使用 scikit 快得多,包括梯度定向直方图的版本,请参见 https://github.com/paddywwoof/Machine-Learning/blob/master/image_processor.py)