简体   繁体   English

如何将0-1图像浮点数组转换为0-255 int数组

[英]How to convert 0-1 image float array to 0-255 int array

import matplotlib
import numpy as np

photo=plt.imread('Feynman.png')
plt.figure
plt.subplot(121)
plt.imshow(photo)
photo*=255
plt.subplot(122)
plt.imshow(photo)
  • Plot results in: Plot 结果:
    • Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers). and only displays an image in the first subplot并且只在第一个子图中显示图像

在此处输入图像描述

  • Used image使用图像

我使用的图像

  • The issue is photo*=255 is still an array of floats.问题是photo*=255仍然是一组浮点数。
    • Look at the photo array.看看照片阵列。
    • Add photo = photo.astype(int) after photo*=255 .photo*=255之后添加photo = photo.astype(int)
    • X in .imshow should be int type when the array is 0-255: (M, N, 3): an image with RGB values (0-1 float or 0-255 int)当数组为0-255时, .imshow中的X应该是int类型: (M, N, 3): RGB值的图像(0-1 float or 0-255 int)
photo = plt.imread('Feynman.png')

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(8, 5))

print(photo[0][0])

ax1.imshow(photo)

photo*=255

print(photo[0][0])

photo = photo.astype(int)

print(photo[0][0])

ax2.imshow(photo)

[output]:
[0.16470589 0.16470589 0.16470589]
[42. 42. 42.]
[42 42 42]

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何读取整数范围为 0-255(表示图像的灰度像素)的文本文件并将其转换为二维数组? - How to read a text file that has integers ranging from 0-255 (representing grayscale pixels of images) and convert into a 2D array? 将 numpy 数组从 -0.1 - 0.2 缩放到 0-255 - Scale a numpy array with from -0.1 - 0.2 to 0-255 通过 Image.fromarray 将浮点图像数组转换为 PIL 中的 int - convert float image array to int in PIL via Image.fromarray 如何将数组值0和255转换为对应的0和1数组 - How to convert array values 0 and 255 to corresponding 0 and 1 array 将设置转换为numpy 0-1数组? - convert set to numpy 0-1 array? 如何将包含浮点数组的 pandas 列转换为 int 数组 - How to convert a pandas column containing float array to int array numpy.astype(np.uint8) 如何转换浮点数组? -1.2997805 变成 255 - how does numpy.astype(np.uint8) convert a float array? -1.2997805 became 255 如何将numpy int转换为带有单独的numpy数组的浮点数? - How to convert numpy int to float with separate numpy array? 如何将 8bit (0-255) data.table 保存为图像文件,然后从图像中检索数据? - How to save 8bit (0-255) data table as an image file and then retrieve data from the image? 使用NumPy将ubyte [0,255]数组转换为float数组[-0.5,+0.5]的最快方法 - Fastest way to convert ubyte [0, 255] array to float array [-0.5, +0.5] with NumPy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM