How to Use gdal for re-sample images

Source: Internet
Author: User

When writing a re-sample image, you can use gdal to read and write the image, and then write the re-sample algorithm (nearest pixel method, bilinear interpolation method, cubic convolution method, etc) [I will write an article to illustrate the principle of this sampling algorithm when I have time] Write the calculation result into the image for implementation.

In the gdal algorithm, five sampling algorithms are provided, which are defined as follows (46 rows of location gdalwarper. h ):

/*! Warp Resampling Algorithm */typedef enum {  /*! Nearest neighbour (select on one input pixel) */ GRA_NearestNeighbour=0,  /*! Bilinear (2x2 kernel) */                         GRA_Bilinear=1,  /*! Cubic Convolution Approximation (4x4 kernel) */  GRA_Cubic=2,  /*! Cubic B-Spline Approximation (4x4 kernel) */     GRA_CubicSpline=3,  /*! Lanczos windowed sinc interpolation (6x6 kernel) */ GRA_Lanczos=4} GDALResampleAlg;

View the source code of gdalwarp and find that warp is very powerful and can be used for projection conversion, re-projection, projection definition, re-sampling, Mosaic, geometric correction and image registration. In a word, it is very good and powerful. Next let's take a look at a little bit of it. Use warp to write a re-sampling interface. The Code is as follows:

/*** Re-sampling function (gdal) * @ Param pszsrcfile input file path * @ Param pszoutfile the path to the result image written * @ Param fresx x conversion sampling ratio, the default size is 1.0. If the image is larger than 1 and smaller than 1, the image is reduced * @ Param fresy y conversion sampling ratio. The default size is 1.0 * @ Param nresamplemode. There are five sampling modes, for details, see gdalresamplealg definition. The default value is bilinear interpolation * @ Param pextent sampling range. If it is null, it indicates calculating the sample band number specified by the full graph * @ Param pbandindex, null indicates the number of bands sampled in all bands * @ Param pbandcount, Which is used together with pbandindex, number of sample bands * @ Param pszformat format of the result image written * @ Param pProgress progress bar pointer * @ RET 0 is returned if urn is successful. Otherwise, it is another value */INT resamplegdal, int * pbandindex, int * pbandcount, const char * pszformat, lt_progress * pProgress) {If (pProgress! = NULL) {pProgress-> setprogresscaption ("weight??? "); PProgress-> setprogresstip (" positive? In? Hold? OK??? Like ?... ");} Gdalallregister (); gdaldataset * pdsrc = (gdaldataset *) gdalopen (pszsrcfile, ga_readonly); If (pdsrc = NULL) {If (pProgress! = NULL) pProgress-> setprogresstip ("? OK?? Text?? No? Save? At ?,? Or? By?? Ge?? No? By? Support? Hold ?!? "); Return re_nofile;} gdaldriver * pdriver = getgdaldrivermanager ()-> getdriverbyname (pszformat); If (pdriver = NULL) {If (pProgress! = NULL) pProgress-> setprogresstip ("no? Yes? Create??? Ge??? Text? Pieces ?!? "); Gdalclose (gdaldataseth) pdsrc); Return re_createfile;} int ibandcount = pdsrc-> getrastercount (); string strwkt = pdsrc-> getprojectionref (); gdaldatatype datatype = pdsrc-> getrasterband (1)-> getrasterdatatype (); double dgeotrans [6] = {0}; pdsrc-> getgeotransform (dgeotrans); int inewbandcount = ibandcount; if (pbandindex! = NULL & pbandcount! = NULL) {int imaxbandindex = pbandindex [0]; // query? Outbound? Most? Big?? Poo? Segment? Suo? Introduction? Order?? For (INT I = 1; I <* pbandcount; I ++) {If (imaxbandindex <pbandindex [I]) imaxbandindex = pbandindex [I];} If (imaxbandindex> ibandcount) {If (pProgress! = NULL) pProgress-> setprogresstip ("? OK?? Poo? Segment? Order?? Super?? Figure? Like?? Poo? Segment? Number ?,? Please? Check? Check? Lost? Incoming? Parameter? Number ?!? "); Gdalclose (gdaldataseth) pdsrc); Return re_paramerror;} inewbandcount = * pbandcount;} lt_envelope enextent; enextent. settonull (); If (pextent = NULL) // All? Figure??? {Double dprj [4] = {0}; // x1, x2, Y1, Y2 imagerowcol2projection (dgeotrans, 0, 0, dprj [0], dprj [2]); imagerowcol2projection (dgeotrans, pdsrc-> getrasterxsize (), pdsrc-> getrasterysize (), dprj [1], dprj [3]); enextent. init (dprj [0], dprj [1], dprj [2], dprj [3]); pextent = & enextent ;} dgeotrans [0] = pextent-> getminx (); dgeotrans [3] = pextent-> getmaxy (); dgeotrans [1] = dgeotrans [1]/fresx; dgeotrans [5] = dgeo Trans [5]/fresy; int inewwidth = static_cast <int> (pextent-> getmaxx ()-pextent-> getminx ()/ABS (dgeotrans [1]) + 0.5); int inewheight = static_cast <int> (pextent-> getmaxx ()-pextent-> getminx ()/ABS (dgeotrans [5]) + 0.5 )); gdaldataset * pddst = pdriver-> Create (pszoutfile, inewwidth, inewheight, inewbandcount, ype, null); If (pddst = NULL) {If (pProgress! = NULL) pProgress-> setprogresstip ("?? Lost? Outbound? Text?? Lost? Failed ?!? "); Gdalclose (gdaldataseth) pdsrc); Return re_createfile;} pddst-> setprojection (strwkt. c_str (); pddst-> setgeotransform (dgeotrans); gdalresamplealg eresample = (gdalresamplealg) nresamplemode; If (pProgress! = NULL) {pProgress-> setprogresstip ("positive? In? Hold? OK??? Like ?... "); PProgress-> setprogresstotalstep (inewbandcount * inewheight);} int * psrcband = NULL; int * pdstband = NULL; int ibandsize = 0; If (pbandindex! = NULL & pbandcount! = NULL) {ibandsize = * pbandcount; psrcband = new int [ibandsize]; pdstband = new int [ibandsize]; for (INT I = 0; I <ibandsize; I ++) {psrcband [I] = pbandindex [I]; pdstband [I] = I + 1 ;}} else {ibandsize = ibandcount; psrcband = new int [ibandsize]; pdstband = new int [ibandsize]; for (INT I = 0; I <ibandsize; I ++) {psrcband [I] = I + 1; pdstband [I] = I + 1 ;}} void * htransformarg = NULL, * hgenimupljarg = NULL; Htransformarg = hgenimupljarg = gdalcreategenimgprojtransformer2 (gdaldataseth) pdsrc, (gdaldataseth) pddst, null); If (htransformarg = NULL) {If (pProgress! = NULL) pProgress-> setprogresstip ("? Change? Parameter? Number? Error? Error ?!? "); Gdalclose (gdaldataseth) pdsrc); gdalclose (gdaldataseth) pddst); Return re_paramerror;} specify pfntransformer = conditions; gdalwarpoptions * PSWO = gdalcreatewarpoptions (); PSWO-> papszwarpoptions = cslduplicate (null); PSWO-> eworkingdatatype = datatype; PSWO-> eresamplealg = eresample; PSWO-> hsrcds = (gdaldataseth) pdsrc; PSWO-> hdstds = (gdaldataseth) pddst; PSWO-> pfnt Ransformer = pfntransformer; PSWO-> ptransformerarg = htransformarg; PSWO-> pfnprogress = gdalprogress; PSWO-> pprogressarg = pProgress; PSWO-> nbandcount = inewbandcount; PSWO-> pansrcbands = (int *) cplmalloc (inewbandcount * sizeof (INT); PSWO-> pandstbands = (int *) cplmalloc (inewbandcount * sizeof (INT )); for (INT I = 0; I <inewbandcount; I ++) {PSWO-> pansrcbands [I] = psrcband [I]; PSWO-> pandstbands [I] = PDS Tband [I];} release (psrcband); release (pdstband); gdalwarpoperation owo; If (owo. initialize (PSWO )! = Ce_none) {If (pProgress! = NULL) pProgress-> setprogresstip ("? Change? Parameter? Number? Error? Error ?!? "); Gdalclose (gdaldataseth) pdsrc); gdalclose (gdaldataseth) pddst); Return re_paramerror;} owo. chunkandwarpimage (0, 0, inewwidth, inewheight); values (PSWO-> ptransformerarg); values (PSWO); gdalclose (gdaldataseth) pdsrc); gdalclose (gdaldataseth) pddst ); if (pProgress! = NULL) pProgress-> setprogresstip ("weight???? ?!? "); Return re_success ;}

PS: When I use Windows Live writer to write a blog and use the vspaste plug-in to paste the code, I find that I will add "?" after the Chinese characters. No. I'm too lazy to modify it. Let's take a look!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.