[Qgis-user] Qgis : Calculat the median of multiple bands of raster (90 bands)
celati Laurent
laurent.celati at gmail.com
Tue Dec 23 15:23:24 PST 2025
Good afternoon,
I work with Qgis 3.40.
I have a question almost similar to this post
<https://gis.stackexchange.com/questions/338888/calculating-the-average-of-multiple-bands-of-raster-with-high-number-of-bands-in>.
But i would like to caculate the median instead of the average/median. From
one single raster with 90 bands, i would like generate a new raster file
with the median value for each pixel. The median has to be calculated from
all the 90 bands for each pixel.
Following still this post
<https://gis.stackexchange.com/questions/338888/calculating-the-average-of-multiple-bands-of-raster-with-high-number-of-bands-in>succeed
in calculating the average from the 90 bands thanks to this following
python qgis script :
rasterlayer = QgsProject.instance().mapLayersByName('infra-red')[0]
> #Change infra-red to the name of your layer
> bandcount = rasterlayer.bandCount() #Count the bands
>
> tempexp = ['"{0}@{1}"'.format(rasterlayer.name(), bandnum) for
> bandnum in range(1,bandcount+1)] #['"infra-red at 1"', '"infra-red at 2"',
> '"infra-red at 3"', '"infra-red at 4"']
> tempexp = '+'.join(tempexp) #'"infra-red at 1"+"infra-red at 2"+"infra-red at 3
> "+"infra-red at 4"'
> exp = '('+tempexp+')/{0}'.format(bandcount) #'("infra-red at 1
> "+"infra-red at 2"+"infra-red at 3"+"infra-red at 4")/4'
>
> processing.runAndLoadResults("qgis:rastercalculator",
>
> {'EXPRESSION':exp,'LAYERS':[rasterlayer.source()],'CELLSIZE':None,'EXTENT':None,'CRS':None,'OUTPUT':'TEMPORARY_OUTPUT'})
Is there a way to do the same thing but this time to calculate the median
value ?
Thanks so much.
Regards.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20251224/9f288a9d/attachment-0001.htm>
More information about the QGIS-User
mailing list