Select to view content in your preferred language

Arcpy Condition on Green Band of Raster Dataset - Incorrect Results

193
1
05-02-2025 01:22 AM
NickCollis1
New Contributor

There is a multi-band raster dataset, IN_RASTER, split into four bands of RGB and an alpha band. A script tool is written to pull out the green band of IN_RASTER, and run Conditional on it to return a raster of 0 for all cells where the input is outside of a given range. A range of values for the green band is given, with Con returning 0 for any values outside of this range:

    from arcpy.ia import ExtractBand
    from arcpy.sa import Con

    IN_RASTER = "C:/Users/mallard/mallards_input_colour_raster.tif"
    GREEN_RANGE = [100, 200]

    green_band = ExtractBand(raster=IN_RASTER, band_names=["green"])

    s = GREEN_RANGE[0]
    e = GREEN_RANGE[1]
    green_filtered = Con((green_band >= s) & (green_band <= e), green_band, 0)
    green_filtered.save()

The output, green_filtered, is compared to the input raster using ArcGIS Pro. See screenshot, showing the input raster, zoomed in to show four whole cells:

input_four_cells.png

Identify is run on the top-right cell:

input_tr_cell_indentify.png

This shows a value of 125 for the green band. This is within the range GREEN_RANGE used in the Con tool. As such, this cell's value of 125 should be returned in the green_filtered output raster. However, inspecting this same cell in the output raster, its value is zero:

output-raster-tr-cell-identify.png

What is the error in the code or the approach?

Tags (3)
0 Kudos
1 Reply
JohannesBierer
Frequent Contributor

Are there brackets missing in the con statemant?

Con(((inRaster1 == 1) & (inRaster2 == 5)), inRaster1 + inRaster2, 99)

https://desktop.arcgis.com/de/arcmap/latest/tools/spatial-analyst-toolbox/con-.htm 

0 Kudos