Skimage - 调整大小功能的奇怪结果
Skimage - Weird results of resize function
我正在尝试使用 skimage.transform.resize function
调整 .jpg
图片的大小。函数 returns me 奇怪的结果(见下图)。我不确定这是错误还是只是错误地使用了该功能。
import numpy as np
from skimage import io, color
from skimage.transform import resize
rgb = io.imread("../../small_dataset/" + file)
# show original image
img = Image.fromarray(rgb, 'RGB')
img.show()
rgb = resize(rgb, (256, 256))
# show resized image
img = Image.fromarray(rgb, 'RGB')
img.show()
原图:
已调整图像大小:
我已经检查过 ,但我认为我的错误具有不同的特性。
更新:rgb2lab 函数也有类似的错误。
问题是 skimage 在调整图像大小后转换数组的像素数据类型。原始图像每像素 8 位,类型为 numpy.uint8
,调整后的像素为 numpy.float64
个变量。
调整大小操作正确,但结果显示不正确。为了解决这个问题,我提出了两种不同的方法:
要更改结果图像的数据结构。在更改为 uint8 值之前,必须将像素转换为 0-255 比例,因为它们处于 0-1 标准化比例:
# ...
# Do the OP operations ...
resized_image = resize(rgb, (256, 256))
# Convert the image to a 0-255 scale.
rescaled_image = 255 * resized_image
# Convert to integer data type pixels.
final_image = rescaled_image.astype(np.uint8)
# show resized image
img = Image.fromarray(final_image, 'RGB')
img.show()
更新:根据scipy.misc.imshow
,此方法已弃用
- 使用另一个库 来显示图像。查看 Image library documentation, there isn't any mode supporting 3xfloat64 pixel images. However, the scipy.misc 库有适当的工具来转换数组格式以正确显示它:
from scipy import misc
# ...
# Do OP operations
misc.imshow(resized_image)
我正在尝试使用 skimage.transform.resize function
调整 .jpg
图片的大小。函数 returns me 奇怪的结果(见下图)。我不确定这是错误还是只是错误地使用了该功能。
import numpy as np
from skimage import io, color
from skimage.transform import resize
rgb = io.imread("../../small_dataset/" + file)
# show original image
img = Image.fromarray(rgb, 'RGB')
img.show()
rgb = resize(rgb, (256, 256))
# show resized image
img = Image.fromarray(rgb, 'RGB')
img.show()
原图:
已调整图像大小:
我已经检查过
更新:rgb2lab 函数也有类似的错误。
问题是 skimage 在调整图像大小后转换数组的像素数据类型。原始图像每像素 8 位,类型为 numpy.uint8
,调整后的像素为 numpy.float64
个变量。
调整大小操作正确,但结果显示不正确。为了解决这个问题,我提出了两种不同的方法:
要更改结果图像的数据结构。在更改为 uint8 值之前,必须将像素转换为 0-255 比例,因为它们处于 0-1 标准化比例:
# ... # Do the OP operations ... resized_image = resize(rgb, (256, 256)) # Convert the image to a 0-255 scale. rescaled_image = 255 * resized_image # Convert to integer data type pixels. final_image = rescaled_image.astype(np.uint8) # show resized image img = Image.fromarray(final_image, 'RGB') img.show()
更新:根据scipy.misc.imshow
,此方法已弃用
- 使用另一个库 来显示图像。查看 Image library documentation, there isn't any mode supporting 3xfloat64 pixel images. However, the scipy.misc 库有适当的工具来转换数组格式以正确显示它:
from scipy import misc
# ...
# Do OP operations
misc.imshow(resized_image)