I have a raster with land use classes (see image). I want to count the pixel values (i.e. the classes) within the blue perimeter. In QGIS, there is a tool called 'Raster layer unique values report' which can be used to count pixel values. However, it is not possible to select a vector layer to only count the unique values within a specific polygon. As such, I get the pixel counts for the entire raster.
Is it possible to only count the pixel values from a raster within a polygon in QGIS?
Best Answer
The
Zonal Histogram tool
using a polygon layer as input will give a new version of the polygon layer with a column for each raster value containing its countAlternatively, you can use PyQGIS to clip the raster by the mask polygon, and then access the pixel counts through its data provider: