Hi All, 

I am testing the Python capabilities with the image object level in Developer 10.4. I have adapted the code presented during the 10.4 webinar for vegetation classification and noticed a ZeroDivisionError when trying to calculate the NDVI value for some objects. This is due to my test tile containing NoData values. I have a fix, and wanted to post the code here, so that anyone else seeing the error also knows the fix. Please adapt as you see fit for your projects. I can post more observations as I get more into the new capabilities. Code below and screenshots of both the error and output after the fix.

import ecognition as ecog
import numpy as np
import pandas as pd
import scipy
import shapely

# uncomment line below to enable debugging (only works on Windows OS, using external *.py file)
# you can then attach debugger in external IDE
# ecog.wait_for_debugger(port=5678)

def function(input_dict):
  """ """
  # Create image objects (an iterator)
  image_objects = ecog.ProcessContext.get_image_object_iterator()

  # Write input features as Feature Objects
  red_feature = input_dict['feature']['Mean red']
  nir_feature = input_dict['feature']['Mean nir']

  # Iterate through image objects
  for image_object in image_objects:
      # Calculate feature value for specific object
      red_value = red_feature.calculate(image_object)
      nir_value = nir_feature.calculate(image_object)
      # Calculate ndvi value - getting ZeroDivisionError in testing - for nodata values
      try:
          ndvi_value = (nir_value - red_value) / (nir_value + red_value)
      except ZeroDivisionError as error:
          error_flag = True
      else:
          error_flag = False
      # Set ndvi threshold
      ndvi_threshold = 0.2
      # Assign to class based on ndvi - first check for no data object (error flag set)
      if error_flag:
          image_object.set_classification('nodata', True)
      else:
          if ndvi_value >= ndvi_threshold:
              image_object.set_classification('vegetation', True)
          else:
              image_object.set_classification('nonvegetation', True)
Error:
 
Output after solution:

0 comments

Recent Discussions

Need more help?

Enter Knowledge Base Contact Support