当前位置:网站首页>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);
}
边栏推荐
- Chapter 18 request matcher serverwebexchangematcher of oauth2loginauthenticationwebfilter
- [golang] golang realizes sending wechat service number template messages
- Deep learning Titanic (beginner) kaggle Liu er's homework Lesson 8
- A 20 yuan facial cleanser sold tens of thousands in seven days. How did they do it?
- Display: the function and effect of align content, justify content and align items under flex
- 2022-07-19 study notes of group 5 self-cultivation class (every day)
- Take a note: Oracle conditional statement
- Fifth day of force deduction
- Many local and municipal supervision departments carried out cold drink sampling inspection, and Zhong Xue's high-quality products were all qualified
- Implementing DDD based on ABP -- domain logic and application logic
猜你喜欢

.net6 miniapi (V): Options

144. Preorder traversal of binary tree

Memory leak due to improper handling of custom view
![[file upload] parse text files and store them in batches through JDBC connection (dynamic table creation and dynamic storage)](/img/9c/0305f7256ab6037d586c8940b9dc76.png)
[file upload] parse text files and store them in batches through JDBC connection (dynamic table creation and dynamic storage)
![Top101 [linked list] must be applied for interview](/img/f5/2b27d0e3fbd52dd04ffcd660c01a78.png)
Top101 [linked list] must be applied for interview

Servlet personal practice notes (I)

Record once C # extract audio files with ffmempeg

Why does the legend of superstar (Jay Chou) not constitute pyramid selling? What is the difference between distribution and pyramid selling?

Visio use
![[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
随机推荐
Customized view considerations
Li Kou 279 complete square - dynamic programming
Skywalking distributed link tracking, related graphics, DLJD, cat
Performance test indicators using JMeter
Emergency response stack
How to do a good job in safety development?
[understanding of opportunity-47]: Guiguzi - Chapter 11 - decision makers, moderation, and rational distribution of interests
Use go language to import Doris data through stream load
Bond network card mode setting
Array the same value of key and merge the value value (collation)
Implementing DDD based on ABP -- domain logic and application logic
Advantages and disadvantages of zero trust security
C language introduction practice (9): completion judgment
Flink1.15 source code reading - Flink annotations
VMware installation
SQL comment
Use "display: flex; justify content: Center; align items: Center;" Solve the problem that the streaming grid layout cannot be centered
2022-07-19 study notes of group 5 self-cultivation class (every day)
Task02 | EDA initial experience
Machine learning notes - building a recommendation system (4) matrix decomposition for collaborative filtering