GDAL影像合并

本文介绍如何利用GDAL库进行多幅影像的高效合并,重点讲解自己构建掩膜的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// merge.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "../3dparty/include/gdal_priv.h"
#include "../3dparty/include/gdal_alg.h"
#include "../3dparty/include/gdalwarper.h"

#include <cassert>

#ifdef _DEBUG
#pragma comment(lib, "../3dparty/lib/gdal_i.lib")
#else
#pragma comment(lib, "../3dparty/lib/gdal_i.lib")
#endif

#define SIZEOFARRAY(array) sizeof(array)/sizeof(array[0])

static double dfMinX=0.0, dfMinY=0.0, dfMaxX=0.0, dfMaxY=0.0;
static double dfXRes=0.0, dfYRes=0.0;

static int bTargetAlignedPixels = FALSE;
static int nForcePixels=0, nForceLines=0, bQuiet = FALSE;
static int bEnableDstAlpha = FALSE, bEnableSrcAlpha = FALSE;
static int bVRT = FALSE;

static char *pszDstFileName = "../data/out.tif";
static char *apszSrcFiles[] = 
{
	/*"../data/H50G038013DOM.tif",
	"../data/H50G038014DOM.tif",
	"../data/H50G038015DOM.tif",
	"../data/H50G039012DOM.tif",
	"../data/H50G039013DOM.tif",
	"../data/H50G039014DOM.tif",*/
	"../data/H50G039015DOM.tif",
	"../data/H50G039016DOM.tif"
};

GDALDataType eWorkingType = GDT_Byte;

static GDALResampleAlg eResampleAlg = GRA_NearestNeighbour;
static GDALDatasetH GDALWarpCreateOutput( char **papszSrcFiles, 
										 int nFilesCount, 
										 const char *pszFilename, 
										 const char *pszFormat, 
										 GDALDataType eDT );

static void CreateMask(char* const* paFilePath, const int nFilesCount);

int _tmain(int argc, _TCHAR* argv[])
{
	GDALAllRegister();

	GDALDatasetH hSrcDS = NULL;
	GDALDatasetH hDstDS = NULL;

	int nFilesCount = SIZEOFARRAY(apszSrcFiles);
	hDstDS = GDALWarpCreateOutput(apszSrcFiles, nFilesCount,
		pszDstFileName, "GTiff", GDT_Byte);

	char **papszSrcFiles = apszSrcFiles;
	CreateMask(papszSrcFiles, nFilesCount);


	for( int iSrc = 0; iSrc < nFilesCount; iSrc++ )
	{
		GDALDatasetH hSrcDS;

		hSrcDS = GDALOpen( apszSrcFiles[iSrc], GA_ReadOnly );
		GDALDataset *pSrcDS = (GDALDataset *)hSrcDS;
		assert(pSrcDS->GetRasterBand(1)->GetMaskBand() != NULL);

		if( hSrcDS == NULL )
			exit( 2 );

		if ( GDALGetRasterCount(hSrcDS) == 0 )
		{
			fprintf(stderr, "Input file %s has no raster bands.\n", apszSrcFiles[iSrc] );
			exit( 1 );
		}

		if( !bQuiet )
			printf( "Processing input file %s.\n", apszSrcFiles[iSrc] );

		if ( eResampleAlg != GRA_NearestNeighbour &&
			GDALGetRasterColorTable(GDALGetRasterBand(hSrcDS, 1)) != NULL)
		{
			if( !bQuiet )
				fprintf( stderr, "Warning: Input file %s has a color table, which will likely lead to "
				"bad results when using a resampling method other than "
				"nearest neighbour. Converting the dataset prior to 24/32 bit "
				"is advised.\n", apszSrcFiles[iSrc] );
		}

		if( GDALGetRasterColorInterpretation( 
			GDALGetRasterBand(hSrcDS,GDALGetRasterCount(hSrcDS)) ) 
			== GCI_AlphaBand 
			&& !bEnableSrcAlpha )
		{
			bEnableSrcAlpha = TRUE;
			if( !bQuiet )
				printf( "Using band %d of source image as alpha.\n", 
				GDALGetRasterCount(hSrcDS) );
		}
		void *hTransformArg = NULL;

		hTransformArg = GD
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值