GDAL重采样实现

using OSGeo.GDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Resample1
{
    class Program
    {
        static void Main(string[] args)
        {
            string srcFileName = @"E:\GDAL\GDALPractice\GDAL_in_CSharp\chapter07\QQ20150414101043.tif";
            string dstFileName = @"E:\GDAL\GDALPractice\GDAL_in_CSharp\chapter07\result_resample1.tif";

            double xRatio = 0;
            double yRatio=0;
            bool b = false;
            while (b==false)
            {
                try
                {
                    //xRatio、yRatio大于1则图像变大,小于1则变小
                    Console.WriteLine("输入X方向的采样比例:");
                    xRatio = Convert.ToDouble(Console.ReadLine());
                    Console.WriteLine("输入Y方向的采样比例:");
                    yRatio = Convert.ToDouble(Console.ReadLine());
                    b = true;
                }
                catch
                {
                    Console.WriteLine("输入错误,请重新输入:");
                }
            }

            Gdal.AllRegister();
            Dataset srcDs = Gdal.Open(srcFileName, Access.GA_ReadOnly);
            Driver srcDrv=srcDs.GetDriver();
            Band srcBand = srcDs.GetRasterBand(1);
            DataType type = srcBand.DataType;

            int bandCount = srcDs.RasterCount;
            int iWidth = srcDs.RasterXSize;
            int iHeight = srcDs.RasterYSize;

            //根据采样比例计算重采样后的图像宽高
            int dstImgWidth = (int)(iWidth * xRatio + 0.5);
            int dstImgHeight = (int)(iHeight * yRatio + 0.5);

            double[] adfGeoTransform = new double[6];
            srcDs.GetGeoTransform(adfGeoTransform);

            //计算重采样后的图像分辨率
            adfGeoTransform[1] /= xRatio;
            adfGeoTransform[5] /= yRatio;
 
            //创建输出文件
            Driver dstDrv = Gdal.GetDriverByName("GTIFF");
            Dataset dstDs = dstDrv.Create(dstFileName, dstImgWidth, dstImgHeight, bandCount, DataType.GDT_Byte, null);
            dstDs.SetGeoTransform(adfGeoTransform);

            int[] bandNum = new int[bandCount];
            for(int i=0;i<bandCount;++i)
            {
                bandNum[i] = i + 1;
            }

            if(type==DataType.GDT_Byte)
            {
                byte[] dstArray = new byte[bandCount * dstImgHeight * dstImgWidth];
                srcDs.ReadRaster(0, 0, iWidth, iHeight, dstArray, dstImgWidth, dstImgHeight, bandCount, bandNum, 0, 0, 0);
                dstDs.WriteRaster(0, 0, dstImgWidth, dstImgHeight, dstArray, dstImgWidth, dstImgHeight, bandCount, bandNum, 0, 0, 0);
            }
            dstDs.FlushCache();
            dstDs.Dispose();
            Console.WriteLine("Success");
            Console.ReadLine();
        }
    }
}

原图:

效果:


### C++ 中使用 GDAL 进行重采样的方法 在 C++ 编程环境中,GDAL 提供了一组强大的工具来处理地理空间数据集的操作,其中包括图像的重采样功能。以下是关于如何通过 C++ 使用 GDAL 实现重采样的详细介绍。 #### 1. 配置环境并加载必要的库 为了能够成功运行基于 GDAL 的程序,首先需要确保已安装 GDAL 库及其开发头文件,并将其链接到项目中[^2]。通常情况下,在 Linux 或 macOS 上可以通过包管理器完成此操作;而在 Windows 平台上则可能需要用到预编译二进制版本或者自行构建源码。 #### 2. 初始化 GDAL 和打开目标数据集 在实际编写代码之前,先要初始化 GDAL 并注册所有的驱动程序以便支持多种格式的数据读写。接着可以利用 `GDALOpen` 函数打开输入影像作为待处理对象[^3]。 ```cpp #include "gdal_priv.h" #include "cpl_conv.h" // Initialize GDAL. GDALAllRegister(); const char* pszFilename = "/path/to/input_file.tif"; GDALDatasetH hSrcDS = GDALOpen(pszFilename, GA_ReadOnly); if (hSrcDS == nullptr) { std::cerr << "Failed to open dataset." << std::endl; } ``` #### 3. 定义输出参数以及设置重采样算法 创建一个新的虚拟内存中的临时栅格文件用于存储结果,并指定其大小、像素类型以及其他元信息等内容。同时还需要决定采用哪种具体的重采样技术(例如最近邻法 Nearest Neighbor、双线性插值 Bilinear 等),这一步骤可通过调用特定函数实现[^4]。 ```cpp int nXSizeOut = ...; // Set desired output width here. int nYSizeOut = ...; // Set desired height accordingly. GDALDriverH hMemDriver = GDALGetDriverByName("MEM"); GDALDatasetH hDstDS = GDALCreate(hMemDriver, "", nXSizeOut, nYSizeOut, GDALGetRasterCount(hSrcDS), GDT_Float32, nullptr); double adfGeoTransform[6]; GDALGetGeoTransform(hSrcDS, adfGeoTransform); adfGeoTransform[1] *= static_cast<double>(nXSizeIn)/static_cast<double>(nXSizeOut); adfGeoTransform[5] *= static_cast<double>(nYSizeIn)/static_cast<double>(nYSizeOut); GDALSetGeoTransform(hDstDS, adfGeoTransform); char **papszWarpOptions = CSLSetNameValue(nullptr,"RESAMPLE_ALG","bilinear"); GDALWarpOptions *psWOpts = GDALCreateWarpOptions(); psWOpts->hDestDS = hDstDS; psWOpts->pszDestAlphaBand = nullptr; psWOpts->pfnProgress = GDALTermProgress; psWOpts->papszDSTOption = papszWarpOptions; GDALWarpOperation oWarper; oWarper.Initialize(psWOpts); oWarper.ChunkAndWarpImage(GDALGetRasterXSize(hSrcDS), GDALGetRasterYSize(hSrcDS)); GDALDestroyGenImgProjTransformer(psWOpts->pTransformerArg, psWOpts->pfnTransformer); GDALDeinitWarpOptions(psWOpts); CSLDestroy(papszWarpOptions); ``` 以上片段展示了完整的流程框架,其中包含了几个重要部分:定义输出尺寸与坐标变换关系;选择合适的重采样策略并通过 API 调用来执行具体任务。 #### 4. 导出最终成果至磁盘或其他媒介上保存下来 当完成了上述所有步骤之后,就可以考虑将得到的新图层导出成常规格式存档起来以备后续分析之需了。这里我们再次运用到了先前提到过的某些概念和技术手段[^5]。 --- ### 总结说明 综上所述,借助于 GDAL 提供给开发者的一系列接口和服务,可以在 C++ 环境下高效便捷地达成对遥感图片或者其他形式的空间数据实施重采样的目的。不过需要注意的是,实际应用过程中可能会遇到各种复杂情况,因此建议深入学习官方文档及相关资料进一步提升技能水平。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值