【发布时间】:2016-05-17 05:48:22
【问题描述】:
我试图从我的磁盘加载一张图片,我使用油漆在 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