【问题标题】:reading images with matplotlib使用 matplotlib 读取图像
【发布时间】:2016-05-17 05:48:22
【问题描述】:

所以我在玩:http://scikit-learn.org/stable/auto_examples/classification/plot_digits_classification.html#example-classification-plot-digits-classification-py

我试图从我的磁盘加载一张图片,我使用油漆在 8x8 png 图像中绘制了一个数字。

scikit-learn 图像看起来像这样:

[[  0.   0.   5.  13.   9.   1.   0.   0.]
 [  0.   0.  13.  15.  10.  15.   5.   0.]
 [  0.   3.  15.   2.   0.  11.   8.   0.]
 [  0.   4.  12.   0.   0.   8.   8.   0.]
 [  0.   5.   8.   0.   0.   9.   8.   0.]
 [  0.   4.  11.   0.   1.  12.   7.   0.]
 [  0.   2.  14.   5.  10.  12.   0.   0.]
 [  0.   0.   6.  13.  10.   0.   0.   0.]]

其中 0 为白色,值越大,像素越暗。 当我加载图像时,我得到了这个:

[[[ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 0.85882354  0.85882354  0.85882354]
  [ 0.14901961  0.14901961  0.14901961]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]]

 [[ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 0.56862748  0.56862748  0.56862748]
  [ 0.22745098  0.22745098  0.22745098]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]]

 [[ 1.          1.          1.        ]
  [ 0.9254902   0.9254902   0.9254902 ]
  [ 0.13725491  0.13725491  0.13725491]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]]

 [[ 1.          1.          1.        ]
  [ 0.53725493  0.53725493  0.53725493]
  [ 0.63137257  0.63137257  0.63137257]
  [ 0.62352943  0.62352943  0.62352943]
  [ 0.97254902  0.97254902  0.97254902]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]]

 [[ 1.          1.          1.        ]
  [ 0.36078432  0.36078432  0.36078432]
  [ 0.18431373  0.18431373  0.18431373]
  [ 0.72941178  0.72941178  0.72941178]
  [ 0.07843138  0.07843138  0.07843138]
  [ 0.86274511  0.86274511  0.86274511]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]]

 [[ 1.          1.          1.        ]
  [ 0.29411766  0.29411766  0.29411766]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 0.42352942  0.42352942  0.42352942]
  [ 0.80000001  0.80000001  0.80000001]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]]

 [[ 1.          1.          1.        ]
  [ 0.19607843  0.19607843  0.19607843]
  [ 0.96470588  0.96470588  0.96470588]
  [ 0.90980393  0.90980393  0.90980393]
  [ 0.08627451  0.08627451  0.08627451]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]]

 [[ 1.          1.          1.        ]
  [ 0.96862745  0.96862745  0.96862745]
  [ 0.28627452  0.28627452  0.28627452]
  [ 0.35686275  0.35686275  0.35686275]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]
  [ 1.          1.          1.        ]]]

是否有内置函数可以将其转换为类似于 scikit-learn 图像的格式?还是我应该编写自己的转换函数?

有人能解释一下 imread 输出是什么意思吗?我有 8 个 8x3 2D 数组,我不知道它是什么。

文档说是 MxN,但我不知道 MxN 是什么意思。 http://matplotlib.org/api/image_api.html#matplotlib.image.imread

谢谢

编辑

这是我感谢 ali_m 编写的代码

from numpy import array

def loadImageFromDisk(image_path):

    def convertImreadImage(img):
        """
        Convert imread images to scikit-learn images
            -img : a 2 dimension array (regular or numpy array)
            -return : a 2 dimension numpy array where 0 is white and
                      the higher the value the darker the pixel
        """
        res = []
        for row in img:
            newRow = []
            for value in row:
                if value == 1.:
                    newRow.append(0)
                else:
                    newRow.append(math.floor((100-value*100)/5))
            res.append(newRow)
        return array(res)

    #needs to be a absolute path
    img = mpimg.imread(image_path)[:, :, 0]
    return convertImreadImage(img)

8x8 png 文件中 6 的输出:

[[  0.   0.   0.   2.  19.   0.   0.   0.]
 [  0.   0.   2.  20.  13.   0.   0.   0.]
 [  0.   1.  20.  16.   0.   0.   0.   0.]
 [  0.  15.  20.  20.  16.   3.   0.   0.]
 [  0.  20.  20.   1.  18.  20.   0.   0.]
 [  0.  20.   9.   0.   1.  20.   0.   0.]
 [  0.  20.   9.   2.  17.  18.   0.   0.]
 [  0.   6.  20.  20.  19.   0.   0.   0.]]

【问题讨论】:

  • 我不知道这种格式,但是有可能8个数组对应图像的行。每个元素对应于每个像素 rgb(或 bgr)的颜色。此外,您的图像似乎是由灰度组成的。一行可能的 [[bgr][bgr]...[bgr]]
  • 您可以轻松地对该代码进行矢量化:img = np.abs(img - img.max())
  • 它会将 1 变为 0,但其他值与训练数据集 (0-20) 不在同一范围内

标签: python python-2.7 matplotlib scikit-learn


【解决方案1】:

看起来imread 为您提供了一个 8x8x3 数组,其中最终维度对应于红色、绿色和蓝色通道(它们都是相同的,因为您在 Paint 中制作的图像是灰度的)。顺便说一句,对于大于 2 维的数组,通过检查它们的 .shape 属性而不是将它们的内容打印到终端来更容易判断它们的形状。

要获得 8x8 数组,您只需索引第一个颜色通道,例如im[:, :, 0] 其中imimread 返回的8x8x3 数组。

【讨论】:

    猜你喜欢
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2016-01-13
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多