Calculate the percentage of cells per value category in a raster in QGIS

Kristian Evers asked on the fediverse:

I have a raster where each cell is categorized with a value of either 1, 2 or 3.

In #QGIS, how would I determine the percentage of cells in each category?

This piqued my curiosity so here you go!

The Raster layer unique values report tool provides both a table containing value & count columns and a number of outputs, among other things TOTAL_PIXEL_COUNT and NODATA_PIXEL_COUNT.

Unique values tables for a UInt16 raster

The Field calculator can be used to calculate an additional column for the table. Note that I decided for NODATA pixels to not count towards the total number of pixels here!

Combine them in a model, add appropriate inputs and you have a fancy new QGIS tool to calculate the percentages of each value in a raster image:

Here is my model file:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.