通过对每个 2x2 网格的像素强度求和来调整灰度图像的大小
Resize grayscale image by summing pixel intensities for every 2x2 grid
我有一个包含 36,000 张 tif 图像(灰度,16 位)的数据集,每张图像的大小为 2048 x 2048 像素。我想将它们的大小调整为 1024 x 1024 像素,方法是在每个 2x2 网格处添加强度,以在调整大小后的图像中的每个像素处生成强度。我需要在 Python 中执行此操作。我一直在使用 ImageJ 和 Image>Transform>Bin, method = Sum 来做这件事。我找不到执行此操作的 Python 库。任何帮助表示赞赏。谢谢。
来自 skimage_measure 的 block_reduce 对我有用。这是代码片段:
import numpy as np
from skimage.measure import block_reduce
import skimage.io as tiffio
#read original 2k x 2k image
original_image = tiffio.imread(read_path+"/RawImage_00000.tif", plugin = 'tifffile')
#bin image by factor of 2 along both axes, summing pixel values in 2x2 blocks
sum2bin_image = block_reduce(original_image, block_size=(2, 2), func=np.sum)
#Numpy arrays are 64-bit float variables, so the following step restores the original unsigned 16-bit format
sum2bin_image = np.round(sum2bin_image).astype(np.uint16)
#save generated image
tiffio.imsave(save_path+'/'+'sum2bin_00000.tif', sum2bin_image, plugin='tifffile')
我有一个包含 36,000 张 tif 图像(灰度,16 位)的数据集,每张图像的大小为 2048 x 2048 像素。我想将它们的大小调整为 1024 x 1024 像素,方法是在每个 2x2 网格处添加强度,以在调整大小后的图像中的每个像素处生成强度。我需要在 Python 中执行此操作。我一直在使用 ImageJ 和 Image>Transform>Bin, method = Sum 来做这件事。我找不到执行此操作的 Python 库。任何帮助表示赞赏。谢谢。
block_reduce 对我有用。这是代码片段:
import numpy as np
from skimage.measure import block_reduce
import skimage.io as tiffio
#read original 2k x 2k image
original_image = tiffio.imread(read_path+"/RawImage_00000.tif", plugin = 'tifffile')
#bin image by factor of 2 along both axes, summing pixel values in 2x2 blocks
sum2bin_image = block_reduce(original_image, block_size=(2, 2), func=np.sum)
#Numpy arrays are 64-bit float variables, so the following step restores the original unsigned 16-bit format
sum2bin_image = np.round(sum2bin_image).astype(np.uint16)
#save generated image
tiffio.imsave(save_path+'/'+'sum2bin_00000.tif', sum2bin_image, plugin='tifffile')