Scikit图像负载二进制图像并转换为二进制矩阵

发布于 2025-02-06 09:07:43 字数 814 浏览 1 评论 0原文

我创建了一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谈下烟灰 2025-02-13 09:07:43

您看到的是这样解释的:

  • 您的数据是灰度
  • ,然后您绘制 colormap
    - 线看起来是黄色的,背景看起来深蓝色/紫色?
  • 然后,您告诉Matplotlib保存该错误的颜色图片
  • 您会读取该误报图片
  • ,然后您现在有了RGBA像素数据, 。您会看到第一个像素行,并且每个颜色像素的每个值,

如果您想保持数据的灰度外观,您将有一些选择。

使用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:

  • your data was grayscale
  • then you plotted that with a colormap
    -- the line looks yellow and the background looks dark blue/violet?
  • then you told matplotlib to save that false-color picture
  • then you read that false-color picture back
  • now you have RGBA pixel data. you see the first pixel row, and each value of each color pixel

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 offers skimage.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.

萌能量女王 2025-02-13 09:07:43

在灰度上,一个具有值1的像素不会出现白色 - 这简而言之是因为Matplotlib在显示图像之前将图像归一化。

选择要么:
a)保持原始二进制值,然后保存的图像在中间没有白线
b)中间有一条白色线,但是然后您必须在保存和加载后修改数组。

AD B)

import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np

# This is the array you have
arr = np.zeros((11, 11), dtype=np.uint8)
arr[:, 5] = 1

plt.figure()
plt.imshow(arr, cmap='gray')
plt.show()


# This will ensure that the line appears white in the .png
arr_png = arr * 255  # 2**8 - 1

# Write to disk
cv.imwrite('line.png', arr_png)

# Load from disk
arr_from_disk = np.array(cv.imread('line.png', 0), dtype=np.uint8)

# Rescale
arr_from_disk = np.divide(arr_from_disk, 255)

assert np.array_equal(arr, arr_from_disk), 'Oops'

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)

import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np

# This is the array you have
arr = np.zeros((11, 11), dtype=np.uint8)
arr[:, 5] = 1

plt.figure()
plt.imshow(arr, cmap='gray')
plt.show()


# This will ensure that the line appears white in the .png
arr_png = arr * 255  # 2**8 - 1

# Write to disk
cv.imwrite('line.png', arr_png)

# Load from disk
arr_from_disk = np.array(cv.imread('line.png', 0), dtype=np.uint8)

# Rescale
arr_from_disk = np.divide(arr_from_disk, 255)

assert np.array_equal(arr, arr_from_disk), 'Oops'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文