分割栅格缺失数据

问题描述

我有一个代码可以读取 .tif 并将图像拆分为特定数量的行和列(代码 1)。我对其进行了修改,以便能够轻松设置所需的图像大小(按像素)(代码 2)。想要的大小不会很完美,因为我使用 celi 函数四舍五入到最近的 int。那不是问题。基本上我正在修改 QGIS 创建网格并裁剪到每个单元格的方式。

通过分割图像,我运行各种分类方法来检测特定事物。不幸的是,我无法与您分享我的数据。我面临的问题是我的图像的左侧没有被分割,因此没有图像能够与分类一起运行。您可以看到在图像 1 中我有我的分类区域,而在图像 2 中分类区域没有完全覆盖。有一个特定的部分,他们会停下来。是的,这里有一些可以确定的区域。

我现在已经在一些图像上尝试了这种方法,并且发生了同样的事情。它总是在左侧。我已将行/列设置为 3 和 3,并且丢失的部分不再丢失。它确实分裂了。所以我不明白为什么当我将列和行增加到 600x600(像素)的图像大小时,我最终会丢失数据。

我的代码中是否有遗漏的地方或者有人有任何建议?

# Setting the directory
os.chdir(r"D:\ortho")

# Loading in the image
rimg = gdal.Open("image.tif")

# Upper left corner of the minX and maxY
gt = rimg.GetGeoTransform()

xmin = gt[0]
ymax = gt[3]

res = gt[1]
xlen = res * rimg.RasterXSize # units is important UTM or WGS
ylen = res * rimg.RasterYSize

# how many tiles you want to have in each row and column
xdiv = 70
ydiv = 105

# Determining the size of each new image 
xsize = xlen/xdiv 
ysize = ylen/ydiv

print(xsize)
print(ysize)

xsteps = [xmin + xsize * i for i in range(xdiv+1)] # plut because we start in the left top corner where X is at its lowest
ysteps = [ymax - ysize * i for i in range(ydiv+1)] # minus because we start in the left top corner where Y is at its highest

for i in range(xdiv):
    for j in range(ydiv):
        xmin = xsteps[i]
        xmax = xsteps[i+1]
        ymax = ysteps[j]
        ymin = ysteps[j+1]

        # Splices the image up into the set divs and saves them.
        gdal.Warp("D:/data/image" + str(i)+str(j) + ".tif",rimg,outputBounds = (xmin,ymin,xmax,ymax),dstNodata = -9999)
wanted_x = 2000
wanted_y = 2000
y_pixel_len = wanted_y * res
x_pixel_len = wanted_x * res


# how many tiles you want to have in each row and column
xdiv = math.ceil(xlen/x_pixel_len)
ydiv = math.ceil(ylen/y_pixel_len)


# Determining the size of each new image
xsize = math.ceil(xlen/xdiv)
ysize = math.ceil(ylen/ydiv)

图片 1

enter image description here

图片 2

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)