如何缩小10m分辨率的tiff图像,并创建一个50m的新图像,其中每个像素都是从第一个图像中统计出来的?
初始的tiff图像是一个二进制分类图--意味着每个像素(10m)要么属于“水”类(值=0),要么属于“冰”类(value=1)。我想要创建一个新的图像,其中每个像素是初始地图的5x5块中的水百分比,这意味着新图像的每个像素将具有50米的分辨率,并表示前一张地图的5x5像素上的“水”像素的比率或百分比。您可以在这里看到示例:示例
下面是一个图像示例(可以从google驱动器下载):https://drive.google.com/uc?export=download&id=19hWQODERRsvoESiUZuL0GQHg4Mz4RbXj
发布于 2022-06-21 11:35:13
您的图像以一种非常奇怪的格式保存,使用32位浮点数来表示两类数据,这些数据可以用一位来表示,所以我使用ImageMagick将其转换为PNG:
magick YOURIMAGE.TIF -alpha off image.png许多Python库会在实际的TIFF上结结巴巴,所以也许可以考虑使用一种不同的方式编写它。
一旦完成,代码可能如下所示:
#!/usr/bin/env python3
from PIL import Image
import numpy as np
# Set size of tiles for subsampling
tileX = tileY = 5
# Open image and convert to greyscale and thence to Numpy array
im = Image.open('image.png').convert('L')
na = np.array(im)
# Round height and width down to next lower multiple of tile sizes
h = (na.shape[0] // tileY) * tileY
w = (na.shape[1] // tileX) * tileX
# Create empty output array to fill
res = np.empty((h//tileY,w//tileX), np.uint8)
pxPerTile = tileX * tileY
for yoffset in range(0,h,tileY):
for xoffset in range(0,w,tileX):
# Count ice pixels in this 5x5 tile
nonZero = np.count_nonzero(na[yoffset:yoffset+tileY, xoffset:xoffset+tileX])
percent = int((100.0 * (pxPerTile - nonZero))/pxPerTile)
res[yoffset//tileY, xoffset//tileX] = percent
# Make Numpy array back into PIL Image and save
Image.fromarray(res.astype(np.uint8)).save('result.png')

经过反思,您可能可以更快、更简单地使用cv2.resize()和对轴和插值cv2.INTER_AREA进行0.2的抽取。
发布于 2022-07-04 10:29:28
我用比维普斯做了一个版本
#!/usr/bin/python3
import sys
import pyvips
image = pyvips.Image.new_from_file(sys.argv[1])
# label (0 == water, 1 == ice) is in the first band
label = image[0]
# average 5x5 areas
label = label.shrink(5, 5)
# turn into a percentage of water
water_percent = 100 * (1 - label)
# ... and save
water_percent.write_to_file(sys.argv[2])我可以像这样在你的测试映像上运行它:
$ ./average.py ~/pics/meltPondClassifiedAndS12.tif x.png要使这个输出(相当暗):

https://stackoverflow.com/questions/72678982
复制相似问题