问题描述
我正在尝试使用skimage.transform.resize function
调整.jpg
图像的大小.函数返回奇怪的结果(请参见下图).我不确定这是一个错误还是对该功能的错误使用.
I am trying to resize a .jpg
image with skimage.transform.resize function
. Function returns me weird result (see image below). I am not sure if it is a bug or just wrong use of the function.
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()
原始图片:
调整大小的图像:
我已经检查过 skimage调整大小以提供奇怪的输出,但是我认为我的错误有不同的属性.
I allready checked skimage resize giving weird output, but I think that my bug has different propeties.
更新:另外rgb2lab函数也有类似的错误.
Update: Also rgb2lab function has similar bug.
推荐答案
问题是 skimage 在调整图像大小后正在转换数组的像素数据类型.原始图像的每个像素为8位,类型为numpy.uint8
,调整大小后的像素为numpy.float64
变量.
The problem is that skimage is converting the pixel data type of your array after resizing the image. The original image has a 8 bits per pixel, of type numpy.uint8
, and the resized pixels are numpy.float64
variables.
调整大小操作正确,但是结果未正确显示.为了解决这个问题,我提出了两种不同的方法:
The resize operation is correct, but the result is not being correctly displayed. For solving this issue, I propose 2 different approaches:
-
要更改结果图像的数据结构.在更改为uint8值之前,必须将像素转换为0-255的比例,因为它们是0-1标准化的比例:
To change the data structure of the resulting image. Prior to changing to uint8 values, the pixels have to be converted to a 0-255 scale, as they are on a 0-1 normalized scale:
# ...
# 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()
要使用另一个库来显示图像.看一看图片库文档,没有任何模式支持3xfloat64像素图像.但是, scipy.misc 库具有用于转换数组的适当工具格式以便正确显示:
To use another library for displaying the image. Taking a look at the Image library documentation, there isn't any mode supporting 3xfloat64 pixel images. However, the scipy.misc library has the appropriate tools for converting the array format in order to display it correctly:
from scipy import misc
# ...
# Do OP operations
misc.imshow(resized_image)
这篇关于Skimage-调整大小功能的结果很奇怪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!