Scikit图像负载二进制图像并转换为二进制矩阵
我创建了一个Numpy阵列形状(11 x 11),所有像素0不包括一个填充1的一列。
[[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]]
使用matplotlib.imsave将阵列保存为PNG图像,得出预期的图像 - 中间有白色线的黑色背景。
试图重新登录保存的PNG图像 skipy.imread and pil.image.open产生形式的数组,
[[[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[253 231 36 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]]
...
]
此文件格式是什么意思(无法在Scikit Image文档中找到说明)?
以及如何将其转换回二进制输入图像?
I have created a numpy array shape(11 x 11) with all pixels 0 excluding one column filled with 1.
[[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]
[ 0 0 0 0 0 1 0 0 0 0 0 ]]
The array was saved as a png image using matplotlib.imsave yielding the expected image - black background with a white line in the middle.
When trying to reimport the saved png image
skipy.imread and Pil.Image.Open yield an array of the form
[[[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[253 231 36 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]
[ 68 1 84 255]]
...
]
What does this file format mean (could not find an explanation in the scikit image documentation) ?
And how do I convert it back to the binary input image?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您看到的是这样解释的:
- 线看起来是黄色的,背景看起来深蓝色/紫色?
如果您想保持数据的灰度外观,您将有一些选择。
使用
plt.imshow(arr,cmap =“灰色”)
,它使用灰色映射而不是彩色映射。阅读图像并将任何颜色转换为灰度时,您可以选择Scikit-image或OpenCV。 OPENCV具有
cv.imread(fname,cv.imread_grayscale)
。 scikit-image提供skimage.io.imread(fname,as_gray = true)
。实际上,您应该首先使用Scikit-image或OpenCV来编写图片。 Matplotlib用于绘图,而不是为了真实地存储数据。 Matplotlib获取了您的数据并重新缩放了数据,因此最大值和最小值变为0和1,对于
灰色>灰色
cmap是黑色和白色。What you see is explained thusly:
-- the line looks yellow and the background looks dark blue/violet?
If you wanted to maintain the grayscale appearance of your data, you'd have some choices.
Use
plt.imshow(arr, cmap="gray")
, which uses a gray color map rather than a colorful one.When reading the image, and also converting any color to grayscale, you can choose scikit-image or OpenCV. OpenCV has
cv.imread(fname, cv.IMREAD_GRAYSCALE)
. scikit-image offersskimage.io.imread(fname, as_gray=True)
.And really you should use scikit-image or OpenCV for writing your picture in the first place. Matplotlib is for plotting, not for storing data authentically. Matplotlib took your data and rescaled it so the maximum and minimum value become 0 and 1, which is black and white for the
gray
cmap.在灰度上,一个具有值1的像素不会出现白色 - 这简而言之是因为Matplotlib在显示图像之前将图像归一化。
选择要么:
a)保持原始二进制值,然后保存的图像在中间没有白线
b)中间有一条白色线,但是然后您必须在保存和加载后修改数组。
AD B)
On grayscale, a pixel with value 1 doesn't appear white - this simply happens because matplotlib normalizes the image before displaying it.
Choose either:
a) Keep the original binary values, then the saved image won't have a white line in the middle
b) Have a white line in the middle, but then you'll have to modify the array before saving and after loading it.
Ad b)