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库进行重采样的问题,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了GDAL库,并将其包含在您的C++项目中。您可以从GDAL官方网站(https://gdal.org/)下载并安装GDAL。 2. 在您的C++代码中,包含GDAL的头文件: ```cpp #include <gdal_priv.h> ``` 3. 创建一个GDAL数据集对象,并打开源图像文件: ```cpp GDALAllRegister(); // 注册所有的GDAL驱动 // 打开源图像文件 GDALDataset* srcDataset = (GDALDataset*)GDALOpen("path_to_source_image", GA_ReadOnly); if (srcDataset == nullptr) { // 处理打开源图像文件失败的情况 } ``` 4. 定义目标图像的宽度、高度和波段数目,并创建一个新的GDAL数据集对象: ```cpp int targetWidth = 800; // 目标图像宽度 int targetHeight = 600; // 目标图像高度 int targetBands = srcDataset->GetRasterCount(); // 目标图像波段数目与源图像相同 // 创建新的目标图像数据集 GDALDriver* memDriver = GetGDALDriverManager()->GetDriverByName("MEM"); GDALDataset* targetDataset = memDriver->Create("", targetWidth, targetHeight, targetBands, GDT_Byte, nullptr); if (targetDataset == nullptr) { // 处理创建目标图像数据集失败的情况 } ``` 5. 使用GDAL库提供的重采样方法,将源图像数据写入目标图像数据集中: ```cpp GDALResampleAlg resampleMethod = GRA_Bilinear; // 重采样方法,这里使用双线性插值 // 重采样 for (int band = 0; band < targetBands; ++band) { GDALRasterBand* srcBand = srcDataset->GetRasterBand(band + 1); // 获取源图像波段 GDALRasterBand* targetBand = targetDataset->GetRasterBand(band + 1); // 获取目标图像波段 // 执行重采样 GDALReprojectImage(srcBand, nullptr, targetBand, nullptr, resampleMethod); } ``` 6. 完成重采样后,您可以将目标图像保存到磁盘上: ```cpp GDALDriver* driver = GetGDALDriverManager()->GetDriverByName("GTiff"); // 指定保存为GeoTIFF格式 driver->CreateCopy("path_to_target_image", targetDataset, 0, nullptr, nullptr, nullptr); ``` 7. 最后,别忘记释放使用的内存和关闭数据集: ```cpp GDALClose(srcDataset); // 关闭源图像数据集 GDALClose(targetDataset); // 关闭目标图像数据集 ``` 这些步骤提供了一个基本的框架,您可以根据您的具体需求进行修改和扩展。希望能对您有所帮助!如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值