3

I am trying to use QGIS to analyse the colour component of a photo. I have imported the photo as a raster layer, created a polygon of the area in question and have tried to use zonal statistics but this doesn't appear to work. When I use the zonal statistics function, I fill in all the relevant boxes and click ok. Then the message window closes. That's it. When I have tried using the function on maps I already have, after clicking ok the progress bar came up and after it finished I could find the resulting stats. When I tried it on my photo, absolutely nothing happens. The window just closes, there are no stats anywhere.

Ideally, the stats I would like to produce are the number of pixels in the polygon, and the number of pixels of each colour. Or, to assign the colours into similar groups say 0-25, 25-50, 50-75 etc up to 255. Of course, this is made more challenging by the fact that a photo used as a raster layer is made up of 3 colour bands and, as yet, I haven't managed to separate the bands.

1
  • It still isn't clear what happens. "it doesn't appear to work" isn't very helpful - we can't see the screen, and you haven't described what happens instead. Please provide some detail, by editing the question.
    – BradHards
    Aug 17, 2016 at 7:35

2 Answers 2

1

You want to count the number of discrete pixel colours inside an area? As far as I know, that can't be done with zonal statistics.

e.g.

Polygon 1 - area is 1000 pixels
500 pixels are (122,135,21)
400 pixels are (22,132,178)
100 pixels are (2,156,99)

Doing that on an raw (unclassified) rgb image will give a HUGE number of possible colours. A polygon with an area of 1000 pixels could easily have 900 unique colours, for example. Most pixel colours will appear only once, and a few will appear twice.

A python/gdal script could be written to count unique colours, but it would be slow and memory-intensive for that very reason.

You can get around this by using a paletted image to "merge" similar colours into a single value. This means that greens would all appear as one value, greys another value, and so on.

(If you're familiar with Photoshop/GIMP, this is the same as reducing an image to a fixed palette size e.g. 16 colours, where each pixel is 'rounded' to the nearest colour from a representative palette of 16 colours)

Try "convert rgb to paletted" (raster > conversion > rgb to pct). More info here

This will classify pixels into groups of similar colour.

Now the problem is easier to address.

I took an aerial image and reduced it to 4 colours.

Then I ran gdalinfo to get a histogram of the values. In Linux, use the terminal, in Windows, use the OSGeo4W shell.

gdalinfo -hist /path/to/my.tiff

This gave me

313730340 154657 11795972 160083079 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

So there are 313,730,340 pixels with value 0. And only 154,657 with value 1.

You'll need to work out which value equates to which colour, though.

There's also a K-Means raster classifier in Orfeo Toolbox, this does a simplification of colours but uses a different algorithm.

1
  • In theory, this sounds spot on. I did the conversion on the whole picture and it worked perfectly. Then I tried to do the conversion on the little bit of the picture I am working on. And got stuck. I tried to clip out the relevant section of the raster using and got an error message: "ERROR 1: Unable to compute a transformation between pixel/line and georeferenced coordinates"
    – Monica
    Aug 20, 2016 at 8:58
1

You are searching at the wrong place. Zonal statistics are in the attributes table of polygon vector layer. Check it out at the next image:

enter image description here

where attributes table has all possible relevant stats for zonal statistics into ocher polygon. However, it's only for first band (red). For all bands, you should select them separately in band selector of 'Zonal Statistics' window.

enter image description here

Editing Note:

Based in your comment, you can be interested in r.report (GRASS geoalgorithm) of Processing Tool Box. However, I think that you need to use each band separately (one GRASS limitation). In my next example,

enter image description here

I selected:

  1. Raster layer to report
  2. 'c' option for count cells
  3. 'Select extent on Canvas', by dragging with the mouse, at the polygon area

After click in 'Run', I got:

enter image description here

the number of pixels (band 1) for each value. However, you can change the number of fp subranges to collect stats for grouping results. I hope that it helps.

3
  • I have tried to use zonal statistics but to no avail. After I click ok, the window closes and the attribute table remains empty. I have used this successfully on another map but it is not working for me now. However, even if zonal stats did work, it still wouldn't solve my problem - I am trying to calculate the number of pixels of each colour, zonal stats would simply tell me the basics.
    – Monica
    Aug 17, 2016 at 18:15
  • Please, check it out my 'Editing Note'.
    – xunilk
    Aug 18, 2016 at 8:17
  • @xunik This method seemed really promising but when I ran it I got a results table with no results on it. (I would put up a picture of the results table but I can't work out how to.)
    – Monica
    Aug 20, 2016 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.