# Counting percentage of raster cell inside polygon using QGIS

I have a raster image that contains a different value and the polygons over the raster's (attached below). Is it possible to calculate the percentage of different cells inside the polygon i.e., 20% cells have x value, 80% have y values. If I use zonal histograms they give me no of cells of the same value i.e., 200 cells of histogram 1. 800 cells of histogram 2 and blah blah .....etc but I want in percentage, not in numbers.

Is it possible to get this percentage in QGIS?

• Did you try the zonal statistics tool? Commented Jul 9, 2021 at 13:47
• Yes I tried zonal statistics tool but there in no percentage option in it -Ben W . Commented Jul 9, 2021 at 13:51
• It's late evening here & I'm just going to bed, but if noone else answers in the meantime or you don't find a solution, I will try to help more tomorrow. Commented Jul 9, 2021 at 14:13

To tool to use for this is `Zonal histogram` (see documentation). Set the raster layer and the polygon (red hached in the screenshot) as input and it calculates the no. of pixels inside the polygon for each pixel value.

From this, it's easy to count the percentage (e.g. with field calculator) - in the example here:

• all pixels: 245150 + 427556 = 672706 = 100%
• green pixels (value 5): 245150/672706*100= 36.4%
• yellow pixels (value 6): 427556/672706*100= 63.6%

If you have a lot of layers, you can use `Zonal histogram` in batch mode (click in the lower left of the dialog window). There, click `autofill...` to get several options like `Add Values by Expression…` and `Add Files by Pattern…`.

Screenshot: see the attribute table of the output layer: it shows that inside the red polygon, there are `245150` pixels with value=5 (green) and `427556` pixels with value=6 (yellow):

• -Babel thank you so much for great explanation. I have 200 polygons and 30 raster image so it is possible to do direct calculation? Because by doing this manually it will take so much time. Any shortcut? Commented Jul 9, 2021 at 18:28
• Use batch mode - see updated answer Commented Jul 9, 2021 at 19:31
• -Babel which updated answer? Commented Jul 10, 2021 at 14:25
• My answer above was updated - read this part: " If you have a lot of layers, you can..." Commented Jul 10, 2021 at 15:01