Select to view content in your preferred language

Raster tif count pixels of certain values

6715
4
Jump to solution
06-05-2017 04:33 AM
AnneTrampe
Deactivated User

Hi,

I have a raster tif with different values and want to count the pixels in the images, but for certain value classes

for example count the pixels of value: 1-3, 3-15, 15-50, 50-200, 200-400, 400-1000

Any idea, how this could been done with arcgis tools. I am not really good in scripting anything, so would prefer to us only a few tools. Also I have to proceed for 25 images, so I am looking for a simple and fast solution

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You will probably have to reclassifiy the rasters  See Reclassify—Help | ArcGIS Desktop 

This will create a new raster of type integer that will have an attribute table which will provide the number of pixels for each class.

To do this on 25 rasters you can use Model Builder see What is ModelBuilder?—Help | ArcGIS Desktop  or a few lines of Python code; see What is Python?—Help | ArcGIS Desktop 

View solution in original post

4 Replies
XanderBakker
Esri Esteemed Contributor

You will probably have to reclassifiy the rasters  See Reclassify—Help | ArcGIS Desktop 

This will create a new raster of type integer that will have an attribute table which will provide the number of pixels for each class.

To do this on 25 rasters you can use Model Builder see What is ModelBuilder?—Help | ArcGIS Desktop  or a few lines of Python code; see What is Python?—Help | ArcGIS Desktop 

TimothyHales
Esri Notable Contributor

Check out the Lookup tool - Lookup—Help | ArcGIS Desktop Keep in mind that it requires an integer raster.

-Credit to Raster Master Jeffrey Swain‌ 

Being more of a feature guy myself, I would have said to convert the raster to points and then pull the values from the output feature class.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Actually, the LookUp tool will not give you the result you are looking for. It will create a raster based on an attribute value of the integer raster. This will not provide you the pixel count fro each class that you specified. Converting the raster to point, could potentially a huge set of points and will still require you to classify the values or define where clauses and do counts. To do this on 25 point featureclasses (derived from the rasters) would still require some programming or the use of Model Builder.  

Although you said that you are not very experienced in programming, below an example Python script that could do the job. Just adjust the folder on line 6 where you have stored you TIFFs:

def main():
    import arcpy
    import os

    # folder with input rasters (TIF)
    ws = r'C:\GeoNet\CompareDEMs\DEM'  ### change this folder

    # definition of the classes
    classes = "1 3 1;3 15 2;15 50 3;50 200 4;200 400 5;400 1000 6"

    arcpy.env.workspace = ws
    arcpy.env.overwriteOutput = True
    ws_tmp = 'IN_MEMORY'
    arcpy.CheckOutExtension("Spatial")

    # create a list of rasters
    lst_rasters = arcpy.ListRasters()

    # loop through the list of rasters
    cnt = 0
    for raster_name in lst_rasters:
        cnt += 1
        # classify the raster
        raster = os.path.join(ws, raster_name)
        out_ras = os.path.join(ws_tmp, "reclass{0}".format(cnt))
        arcpy.gp.Reclassify_sa(raster, "VALUE", classes, out_ras, "NODATA")

        # print the resulting statistics
        print "\n", raster_name
        PrintStats(out_ras)


def PrintStats(ras):
    # get the statistics and print them
    fld_val = 'Value'
    fld_cnt = 'Count'
    flds = (fld_val, fld_cnt)
    curs = arcpy.SearchCursor(ras, flds)
    for row in curs:
        value = row.getValue(fld_val)
        count = row.getValue(fld_cnt)
        print value, count


if __name__ == '__main__':
    main()
GünterDörffel
Frequent Contributor

Hi,

making use of the Raster Attribute Table ( a rather old concept) would be nice here ... but fails when trying to export it. Based on Xanders code here is a modified version that exports the actual values to an excel file (after writing them to an in_memory table). In addition, the ListRasters has been modified to only read tifs - as in the original question ... AND the resulting table in excel will have two additional fields that reflect the re-classification done in the "classes" variable by Xander.

Hope this helps. I added it as a python file ...

0 Kudos