如何调整多个通道的tiff图像的尺寸?

时间:2019-05-03 17:36:51

标签: python deep-learning computer-vision image-resizing tiff

我有一个尺寸为21 X 513 X 513的tiff图像,其中(513,513)是包含21个通道的图像的高度和宽度。如何将该图像调整为21 X 500 X 375?

我正在尝试使用PILLOW来这样做。但是无法弄清楚我做错了什么。

>>> from PIL import Image
>>> from tifffile import imread
>>> img = Image.open('new.tif')
>>> img

    <PIL.TiffImagePlugin.TiffImageFile image mode=F size=513x513 at 0x7FB0C8E5B940>

>>> resized_img = img.resize((500, 375), Image.ANTIALIAS)
>>> resized_img

    <PIL.Image.Image image mode=F size=500x375 at 0x7FB0C8E5B908>

>>> resized_img.save('temp.tif')

>>> img = imread('temp.tif')
>>> img.shape
  (500, 375)

此处的频道信息丢失。

2 个答案:

答案 0 :(得分:1)

尝试使用tifffilescikit-image

from tifffile import imread, imwrite
from skimage.transform import resize

data = imread('2009_003961_SEG.tif')
resized_data = resize(data, (375, 500, 21))
imwrite('multi-channel_resized.tif', resized_data, planarconfig='CONTIG')

comment98601187_55975161中链接的文件2009_003961_SEG.tif不是多通道513x513x21图像。而是文件包含513x21大小的513张图像。 tifffile库将读取文件中的一系列图像,并将其作为形状为513x513x21的numpy数组返回。

要将numpy数组的大小调整为375x500x21,请使用skimage.transform.resize(或scipy.ndimage.zoom)。分别调整21个通道的大小可能会更快。

要使用tifffile编写包含375x500x21大小的单个多通道图像的TIFF文件,请指定planarconfig参数。没有很多库或应用程序可以处理此类文件。

答案 1 :(得分:0)

您可以使用 OpenCV 调整图像大小。我可以使用以下代码调整TIFF格式图像的大小:

import cv2

file = "image.tiff"
img = cv2.imread(file)
print("original image size: ", img.shape)

new_img = cv2.resize(img,(img.shape[1]-100,img.shape[0]-100))  # cv2.resize(image,(width,height))
print("resized image size: ", new_img.shape)

输出:
原始图像尺寸:(512、768、3)
调整尺寸后的图片大小:(412、668、3)

Opencv采用 INTER_LINEAR 作为默认插值方法。

您可以通过提供其他参数来更改插值

new_img = cv2.resize(img,(img.shape[1]-100,img.shape[0]-100),interpolation=cv2.INTER_AREA)

在此处详细了解可用的插值方法:https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html?highlight=resize#resize