6

So I have this .gif file that has values from 0-255 but not entirely grayscale. I tried the r.report and it does not work. I also tried the raster calculator like: "myraster@1"= 1 (because I want to count how many pixels have the value 1) then I get a completely wrong output with nonsense values. Is it that the r.report and the raster calculator can't handle .gif files or am I doing something wrong? The white pixels have a value of 1 and I want to count these pixels. Here is the .gif file. enter image description here

If it helps I am adding the colortable of the .gif: enter image description here

Like I said, I need the number of white pixels that have the value 1.

2
  • is there a color map or is ita muliband raster ?
    – radouxju
    Sep 17, 2015 at 19:40
  • @radouxju no it only has 1 band.
    – Stelios M
    Sep 17, 2015 at 19:46

2 Answers 2

6

Try out my code (as script) at the Python Console of QGIS:

from os import path
import struct
from osgeo import gdal

def countRasterValue(val):

    layer = iface.activeLayer()
    provider = layer.dataProvider()

    fmttypes = {'Byte':'B', 'UInt16':'H', 'Int16':'h', 'UInt32':'I', 'Int32':'i', 'Float32':'f', 'Float64':'d'}

    my_path = provider.dataSourceUri()

    (root, filename) = path.split(my_path)

    dataset = gdal.Open(my_path)

    band = dataset.GetRasterBand(1)

    print "rows = %d columns = %d" % (band.YSize, band.XSize)

    BandType = gdal.GetDataTypeName(band.DataType)

    print "Data type = ", BandType

    print "Executing for %s" % filename
    print "in %s" % root

    count_value = 0

    for y in range(band.YSize):

        scanline = band.ReadRaster(0, y, band.XSize, 1, band.XSize, 1, band.DataType)
        values = struct.unpack(fmttypes[BandType] * band.XSize, scanline)

        for value in values:
            if value == val:
                count_value += 1

    print "Raster count = %d of %d" % (count_value, val)

    dataset = None

countRasterValue(1)

I tested my code with a gif (Graphics Interchange Format) raster whose values were between 1 and 3 (see next image).

enter image description here

It worked with a count of 205 for 1 values (red color).

enter image description here

2
  • You are a wizard. It worked like a charm. So I guess the lesson here is that the processing tools of QGIS can't handle .gif files properly?
    – Stelios M
    Sep 18, 2015 at 7:18
  • 1
    You're welcome. As answer to your question, I don't know. By the way, you have a recipe that it could be adapted for many purposes (stats determination, raster characterization, raster classification, etc). I used it for creating some useful plugins.
    – xunilk
    Sep 18, 2015 at 9:45
4

Simpler and faster with gdalnumeric (gdal+numpy):

import gdalnumeric

raster_file = gdalnumeric.LoadFile("/path/to/file.tif")
pixel_count = (raster_file == 1).sum()  # for pixel value = 1
print(pixel_count)

Inside Qgis (for the active layer selected):

import gdalnumeric
layer = iface.activeLayer()
provider = layer.dataProvider()

raster_file = gdalnumeric.LoadFile(provider.dataSourceUri())
pixel_count = (raster_file == 1).sum()  # for pixel value = 1
print(pixel_count)

Your Answer

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

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