使用scikit-learn歧管将RGB转换为灰色

时间:2016-04-28 11:16:33

标签: python-2.7 image-processing scikit-learn dimensionality-reduction

我正在尝试缩小尺寸,使用manifold learning methods将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

我得到了

  

ValueError:要解压的值太多。

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

你可以做到

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