当前位置:网站首页>GDAL image resampling
GDAL image resampling
2022-07-25 03:54:00 【Meteorite cloud】
#include "gdal_priv.h"
#include "ogrsf_frmts.h"
#include "gdalwarper.h"
/** * Resampling function (GDAL) * @param pszSrcFile Enter the path to the file * @param pszOutFile The path of the written result image * @param fResX X Convert sampling ratio , The default size is 1.0, Greater than 1 The image becomes larger , Less than 1 Indicates that the image is reduced * @param fResY Y Convert sampling ratio , The default size is 1.0 * @param nResampleMode Sampling mode , There are five kinds of , Specific see GDALResampleAlg Definition , The default is bilinear interpolation GRA_NearestNeighbour=0 Nearest neighbor method , The algorithm is simple and can keep the original spectral information unchanged ; The disadvantage is poor geometric accuracy , The gray scale is discontinuous , Jagged edges appear GRA_Bilinear=1 Bilinear method , Simple calculation , The gray scale of the image is continuous and the sampling accuracy is relatively accurate ; The disadvantage is the loss of details ; GRA_Cubic=2 Cubic convolution , Large amount of computation , The gray scale of the image is continuous and the sampling accuracy is high ; GRA_CubicSpline=3 Cubic spline method , Gray continuity and sampling accuracy are the best ; GRA_Lanczos=4 Block Lanczos , By Hungarian mathematicians 、 Physicist lanzos law founded , Experiments show that the effect is close to bilinear ; * @return 0 success ;-1 Failed to open source file ;-2 Failed to create new file ;-3 Error in processing */
int ResampleGDAL(const char* pszSrcFile, const char* pszOutFile, float fResX = 1.0, float fResY = 1.0, GDALResampleAlg eResample = GRA_Bilinear)
{
GDALAllRegister();//
CPLSetConfigOption("GDAL_FILENAME_IS_UTF8", "NO");// Set the path to support Chinese
GDALDataset* pDSrc = (GDALDataset*)GDALOpen(pszSrcFile, GA_ReadOnly);
if (pDSrc == NULL)
{
return -1;
}
GDALDriver* pDriver = GetGDALDriverManager()->GetDriverByName("GTiff");// Set file type
if (pDriver == NULL)
{
GDALClose((GDALDatasetH)pDSrc);
return -2;
}
int width = pDSrc->GetRasterXSize();// Width
int height = pDSrc->GetRasterYSize();// Height
int nBandCount = pDSrc->GetRasterCount();// Band number
GDALDataType dataType = pDSrc->GetRasterBand(1)->GetRasterDataType();// Grid data type
char* pszSrcWKT = NULL;
pszSrcWKT = const_cast<char*>(pDSrc->GetProjectionRef()); // Projection information
double dGeoTrans[6] = {
0 };
int nNewWidth = width, nNewHeight = height;
pDSrc->GetGeoTransform(dGeoTrans); // Affine transformation
//adfGeoTransform[0] /* top left x */
//adfGeoTransform[1] /* w-e pixel resolution */
//adfGeoTransform[2] /* rotation, 0 if image is "north up" */
//adfGeoTransform[3] /* top left y */
//adfGeoTransform[4] /* rotation, 0 if image is "north up" */
//adfGeoTransform[5] /* n-s pixel resolution */
// Calculate the image size after sampling
dGeoTrans[1] = dGeoTrans[1] / fResX;
dGeoTrans[5] = dGeoTrans[5] / fResY;
nNewWidth = static_cast<int>(nNewWidth * fResX + 0.5);
nNewHeight = static_cast<int>(nNewHeight * fResY + 0.5);
// Create a result dataset
GDALDataset* pDDst = pDriver->Create(pszOutFile, nNewWidth, nNewHeight, nBandCount, dataType, NULL);
if (pDDst == NULL)
{
GDALClose((GDALDatasetH)pDSrc);
return -2;
}
pDDst->SetProjection(pszSrcWKT);
pDDst->SetGeoTransform(dGeoTrans);
void* hTransformArg = NULL;
hTransformArg = GDALCreateGenImgProjTransformer2((GDALDatasetH)pDSrc, (GDALDatasetH)pDDst, NULL); //GDALCreateGenImgProjTransformer((GDALDatasetH) pDSrc,pszSrcWKT,(GDALDatasetH) pDDst,pszSrcWKT,FALSE,0.0,1);
if (hTransformArg == NULL)
{
GDALClose((GDALDatasetH)pDSrc);
GDALClose((GDALDatasetH)pDDst);
return -3;
}
GDALWarpOptions* psWo = GDALCreateWarpOptions();
psWo->papszWarpOptions = CSLDuplicate(NULL);
psWo->eWorkingDataType = dataType;
psWo->eResampleAlg = eResample;
psWo->hSrcDS = (GDALDatasetH)pDSrc;
psWo->hDstDS = (GDALDatasetH)pDDst;
psWo->pfnTransformer = GDALGenImgProjTransform;
psWo->pTransformerArg = hTransformArg;
psWo->nBandCount = nBandCount;// Band number
psWo->panSrcBands = (int*)CPLMalloc(nBandCount * sizeof(int));// The memory occupied by the number of bands of the input file
psWo->panDstBands = (int*)CPLMalloc(nBandCount * sizeof(int));// Memory occupied by the number of bands of the output file
for (int i = 0; i < nBandCount; i++)
{
psWo->panSrcBands[i] = i + 1;
psWo->panDstBands[i] = i + 1;
}
GDALWarpOperation oWo;
if (oWo.Initialize(psWo) != CE_None)
{
GDALClose((GDALDatasetH)pDSrc);
GDALClose((GDALDatasetH)pDDst);
return -3;
}
oWo.ChunkAndWarpImage(0, 0, nNewWidth, nNewHeight);
GDALDestroyGenImgProjTransformer(hTransformArg);
GDALDestroyWarpOptions(psWo);
GDALFlushCache(pDDst);
GDALClose((GDALDatasetH)pDSrc);
GDALClose((GDALDatasetH)pDDst);
return 0;
}
int main()
{
ResampleGDAL(" data ( Test data )/label_RGB.tif","test.tif", 0.5, 0.5, GRA_Bilinear);
}
边栏推荐
- 使用 “display: flex;justify-content: center;align-items: center; ” 解决流式栅格布局无法居中的问题
- Solve mysql5.6 database specified key was too long; Max key length is 767 bytes
- Original | record a loophole excavation in Colleges and Universities
- Unity word document click button to download
- Task02 | EDA initial experience
- 01_ Education 4
- How to do a good job in safety development?
- C language_ Defining structures and using variables
- Secondary vocational network security skills competition P100 dcore (light CMS system) SQL injection
- Zhanrui Mobile Phone Unlocked
猜你喜欢

原创|记一次高校漏洞挖掘

301. Delete invalid brackets

Moveit2 - 8. Motion planning API

Secondary vocational network security skills competition P100 dcore (light CMS system) SQL injection

Unity: test rotation function
![[Flink] submit the jar package to the Flink cluster and run it](/img/9b/076949a4b9fe75ee248c400ee21474.png)
[Flink] submit the jar package to the Flink cluster and run it

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

Force button brushing question 61. rotating linked list

应急响应全栈
![Top101 [linked list] must be applied for interview](/img/f5/2b27d0e3fbd52dd04ffcd660c01a78.png)
Top101 [linked list] must be applied for interview
随机推荐
Implementing DDD based on ABP -- domain logic and application logic
Customized view considerations
[Flink] transform operator flatmap
Top101 [linked list] must be applied for interview
Use "display: flex; justify content: Center; align items: Center;" Solve the problem that the streaming grid layout cannot be centered
P100 MSSQL database penetration test of secondary vocational network security skills competition
Detailed explanation of three factory modes
SQL comment
Emergency response stack
word怎么取消退出修订模式
Algorithmic interview questions
2022-07-19 study notes of group 5 self-cultivation class (every day)
Chapter 3 business function development (modify the remarks of market activities)
Machine learning notes - building a recommendation system (4) matrix decomposition for collaborative filtering
Leetcode.745. prefix and suffix search____ Double dictionary tree + double pointer
DNS domain name resolution service
[Flink] rich function
Secondary vocational network security skills competition P100 web penetration test
LeetCode. 302 weekly games___ 03_ 6121. Query the number smaller than k after cutting the number____ sort
Use go language to import Doris data through stream load