图像重采样

 1、tif图像重采样

from osgeo import gdal, gdalconst
import os

def savepicname(datapath, savepath):
    pathDir = os.listdir(datapath)
    #print(datapath)   #datapath为文件夹路径
    picpath = []
    savepicpath = []
    for allDir in pathDir:
        #print(allDir)   #allDir为文件名
        #print(datapath)
        #print(savepath)
        pathDir = os.path.join(datapath, allDir)   #单张图片的绝对路径
        Savepath = os.path.join(savepath, allDir)
        picpath.append(pathDir)
        print(picpath)
        savepicpath.append(Savepath)

    return picpath,savepicpath



def resampling(source_file, target_file, scale=5.):
    """
    影像重采样
    :param source_file: 源文件
    :param target_file: 输出影像
    :param scale: 像元缩放比例
    :return:
    """


    dataset = gdal.Open(source_file, gdalconst.GA_ReadOnly)
    band_count = dataset.RasterCount  # 波段数

    if band_count == 0 or not scale > 0:
        print("参数异常")
        return

    cols = dataset.RasterXSize  # 列数
    rows = dataset.RasterYSize  # 行数
    cols = int(cols * scale)  # 计算新的行列数
    rows = int(rows * scale)

    geotrans = list(dataset.GetGeoTransform())
    print(dataset.GetGeoTransform())
    print(geotrans)
    geotrans[1] = geotrans[1] / scale  # 像元宽度变为原来的scale倍
    geotrans[5] = geotrans[5] / scale  # 像元高度变为原来的scale倍
    print(geotrans)

    if os.path.exists(target_file) and os.path.isfile(target_file):  # 如果已存在同名影像
        os.remove(target_file)  # 则删除之

    band1 = dataset.GetRasterBand(1)
    data_type = band1.DataType
    target = dataset.GetDriver().Create(target_file, xsize=cols, ysize=rows, bands=band_count,
                                        eType=data_type)
    target.SetProjection(dataset.GetProjection())  # 设置投影坐标
    target.SetGeoTransform(geotrans)  # 设置地理变换参数
    total = band_count + 1
    for index in range(1, total):
        # 读取波段数据
        print("正在写入" + str(index) + "波段")
        data = dataset.GetRasterBand(index).ReadAsArray(buf_xsize=cols, buf_ysize=rows)
        out_band = target.GetRasterBand(index)
        # out_band.SetNoDataValue(dataset.GetRasterBand(index).GetNoDataValue())
        out_band.WriteArray(data)  # 写入数据到新影像中
        out_band.FlushCache()
        out_band.ComputeBandStats(False)  # 计算统计信息
    print("正在写入完成")
    del dataset


if __name__ == "__main__":
    source_file = r"E:\data\Shujuku_1500\label"
    target_file = r"E:\data\Shujuku_1500\lab_png"
    picpath, savepicpath = savepicname(source_file, target_file)
    print(picpath,savepicpath)
    for i in range(len(picpath)):    #
        iterpath = picpath[i]
        itersavepath = savepicpath[i]  #保存
        print(i)
        resampling(iterpath, itersavepath, scale=0.55)

2、opencv 图像插值 

import os
import cv2
import sys


def interpolation(filepath,destpath):
    pathDir = os.listdir(filepath)

    for allDir in pathDir:
        print(allDir)
        child = os.path.join(filepath,allDir)
        dest = os.path.join(destpath,allDir)

        if os.path.isfile(child):
            img = cv2.imread(child)
            if child is None:
                print('faile')
                sys.exit()

            w = 512
            h = 512

            img1 = cv2.resize(img,(w,h),interpolation=cv2.INTER_CUBIC)

            cv2.imwrite(dest,img1)



if __name__== "__main__":
    filepath = r'E:\2022Projects\coast\Ex3\lab40961'
    destpath = r'E:\2022Projects\coast\Ex3\lab5121'
    interpolation(filepath,destpath)

插值方法:

图像重采样_第1张图片图像重采样_第2张图片

 

 

你可能感兴趣的:(小工具,python,人工智能,html5)