[gdal-dev] moving averaging filter in GDAL.
Belaid MOA
belaid_moa at hotmail.com
Thu Jul 2 03:43:00 EDT 2009
Hi,
As suggested by Frank and Even, I implemented a simple C++ version of gdalfilter.py (based on VRT driver) using the function below. Unfortunately, I keep getting the same data set as the source; no filtering is done at all! The destination file produced have the same content as the source file. Am I doing something wrong? Any help on this is very appreciated. I tried both gdal-1.4.1 and gdal-1.6.1 but no success.
================================================================
/**
* Apply an averaging filter to a GDALDataset
* sFileName: source file name
* dstFileName: destination file name
* size: size of the filter (kernal)
* coefficients: the coefficients of the filter.
**/
int average(char* sFileName, char* dstFileName, int size, const char* coefficients) {
GDALDriver *poDriver = (GDALDriver *) GDALGetDriverByName( "VRT" );
if (poDriver == NULL) {
cout <<"VRT Driver not found!"<<endl;
return -1;
}
GDALDataset *poVRTDS, *poSrcDS;
poSrcDS = (GDALDataset *) GDALOpen( sFileName, GA_ReadOnly);
poVRTDS = poDriver->CreateCopy( "", poSrcDS, FALSE, NULL, NULL, NULL );
int nBand;
for( nBand = 1; nBand <= poVRTDS->GetRasterCount(); nBand++ ) {
GDALRasterBand *poBand = poVRTDS->GetRasterBand( nBand );
char szFilterSourceXML[10000];
sprintf( szFilterSourceXML,
"<KernelFilteredSource>"
" <SourceFilename>%s</SourceFilename><SourceBand>%d</SourceBand>"
" <Kernel>"
" <Size>%d</Size>"
" <Coefs>%s</Coefs>"
" </Kernel>"
"</KernelFilteredSource>",
sFileName,nBand,size,coefficients);
cout << szFilterSourceXML <<endl;
poBand->SetMetadataItem( "source_0", szFilterSourceXML, "vrt_sources" );
//poBand->FlushCache();
}
GDALDriver *poTIFFDriver = (GDALDriver *) GDALGetDriverByName( "GTiff" );
if (poTIFFDriver == NULL) {
cout <<"GTIFF Driver not found!"<<endl;
return -1;
}
GDALDataset *poTiffDS;
poTiffDS = poTIFFDriver->CreateCopy( dstFileName, poVRTDS, FALSE, NULL, NULL, NULL );
poVRTDS->GetRasterBand( 1 )->FlushCache();
GDALClose(poSrcDS);
GDALClose(poTiffDS);
GDALClose(poVRTDS);
return 1;
}
===========================================================================
With best regards.
Belaid Moa.
From: belaid_moa at hotmail.com
To: gdal-dev at lists.osgeo.org
Subject: RE: [gdal-dev] moving filter averaging in GDAL.
Date: Fri, 19 Jun 2009 17:50:02 +0000
Thank you very much Even. I was just making sure I am not reinventing the wheel :).
I will then write the C++ version.
With best regards.
~Belaid...
> From: even.rouault at mines-paris.org
> To: gdal-dev at lists.osgeo.org
> Subject: Re: [gdal-dev] moving filter averaging in GDAL.
> Date: Fri, 19 Jun 2009 19:44:10 +0200
> CC: belaid_moa at hotmail.com
>
> It should be relatively straighforward to transpose from Python to C++, as
> Python API is mostly deduced from the original C++ one. Look at the
> documentation of the C++ API on gdal.org
>
> Le Friday 19 June 2009 19:27:40 Belaid MOA, vous avez écrit :
> > Thanks a lot Frank. Is there any C/C++ version for that? I am doing the
> > averaging filter from C++ code.
> >
> > With best regards.
> > Belaid Moa.
> >
> > > Date: Fri, 19 Jun 2009 13:23:33 -0400
> > > From: warmerdam at pobox.com
> > > To: belaid_moa at hotmail.com
> > > CC: gdal-dev at lists.osgeo.org
> > > Subject: Re: [gdal-dev] moving filter averaging in GDAL.
> > >
> > > Belaid MOA wrote:
> > > > Hi,
> > > > Does GDAL have functions for applying an averaging filter of window,
> > > > say 11x11, to an image? This is something that comes up all the time in
> > > > image analysis. So, I suppose GDAL has built-in functions for that.
> > > >
> > > > Any help on this is very appreciated.
> > >
> > > Belaid,
> > >
> > > There is support for filtering, including average filtering, with the
> > > gdalfilter.py command in gdal/swig/python/samples. It accomplished
> > > by creating a virtual file using the <KernelFilteredSource> directive -
> > > so ultimately the work is done by the VRT driver.
> > >
> > > Best regards,
> > > --
> > > ---------------------------------------+---------------------------------
> > >----- I set the clouds in motion - turn up | Frank Warmerdam,
> > > warmerdam at pobox.com light and sound - activate the windows |
> > > http://pobox.com/~warmerdam and watch the world go round - Rush |
> > > Geospatial Programmer for Rent
> >
> > _________________________________________________________________
> > We are your photos. Share us now with Windows Live Photos.
> > http://go.microsoft.com/?linkid=9666047
>
>
Attention all humans. We are your photos. Free us.
_________________________________________________________________
We are your photos. Share us now with Windows Live Photos.
http://go.microsoft.com/?linkid=9666047
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20090702/d1736f16/attachment-0001.html
More information about the gdal-dev
mailing list