[GIS] Counting number of pixels/cells with particular value in raster using ArcPy

arcpyraster

I have a raster image containing values ranging from 1 to 10 in Integer format.

I want to count the number of pixel with value = 1.

Is there any method in Arcpy which returns the pixel count if given raster and value as input?

Best Answer

Use the Build Raster Attribute Table tool, then a SearchCursor.

values = {}
arcpy.BuildRasterAttributeTable_management(your_raster, "Overwrite")
with arcpy.da.SearchCursor(your_raster, ['VALUE', 'COUNT']) as rows:
    for row in rows:
        values[row[0]] = row[1]
print values.get(12345, 0)
Related Question