Pri*_*moz 5 python image image-resizing scikit-image
我正在尝试用.jpg图像调整图像大小skimage.transform.resize 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()
Run Code Online (Sandbox Code Playgroud)
原始图片:

调整后的图片:

我已经检查过skimage resize给出了奇怪的输出,但我认为我的bug有不同的特性.
更新:rgb2lab函数也有类似的bug.
Jal*_*alo 11
问题是,在调整图像大小后,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()
Run Code Online (Sandbox Code Playgroud)要使用另一个库用于显示图像.看一下Image库文档,没有任何模式支持3xfloat64像素图像.但是,scipy.misc库具有适当的工具,用于转换数组格式以便正确显示它:
from scipy import misc
# ...
# Do OP operations
misc.imshow(resized_image)
Run Code Online (Sandbox Code Playgroud)