2

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?

0
8

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)
1
  • Thanks, @Luke was I was struggling with numpy I got the answer but it took a lot of time!. This is simple and faster. Thank you once again. Jan 5 '16 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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