chm_zd.py
def chm_segmentation(dsm_fn):
    """CHM ata filtering, masking and watershed segmentation.
    Idea taken from https://www.neonscience.org/resources/learning-hub/tutorials/calc-biomass-py"""

    driver = gdal.GetDriverByName("GTiff")
    dsm_ds = gdal.Open(dsm_fn)
    dsm_ds.GetRasterBand(1).SetNoDataValue(0)
    chm_array = dsm_ds.GetRasterBand(1).ReadAsArray().astype(np.float32)

    start = time.time()
    print("Watershed segmentation...")
    # APPLYING GAUSSIAN FILTER TO REMOVE WRONG POINTS
    chm_array_smooth = ndi.gaussian_filter(chm_array, 0,
                                           mode='constant',
                                           cval=0,
                                           truncate=1)
    chm_array_smooth[chm_array == 0] = 0

    # CALCULATE LOCAL MAXIMUM POINTS
    max_coords = peak_local_max(chm_array_smooth,
                                footprint=np.ones((5, 5)))

    # CREATE MASK TO MATCH INPUT ARRAY SIZE
    markers = np.zeros_like(chm_array_smooth, dtype=bool)
    markers[tuple(max_coords.T)] = True

    # IDENTIFY ALL THE MAXIMUM POINTS
    markers = ndi.label(markers)[0]

    # CREATE MASK TO REDUCE SEGMENTATION ONLY TO TREES
    chm_mask = chm_array_smooth.copy()
    chm_mask[chm_array_smooth > 0] = 1
    chm_mask[chm_array_smooth <= 0] = 0

    # WATERSHED SEGMENTATION
    labels = watershed(chm_array_smooth,
                       markers,
                       compactness=2,
                       mask=chm_mask,
                       watershed_line=False)

    # CHANGING 0 VALUES TO -9999
    segments = np.where(labels == 0, -9999, labels)

    dsm_ds = None
    end = time.time()
    print("CHM segmented in {:.4f} sec\n".format(end - start))

    return segments
CHM ata filtering, masking and watershed segmentation.
    Idea taken from https://www.neonscience.org/resources/learning-hub/tutorials/calc-biomass-py
CHM ata filtering, masking and watershed segmentation.
CHM ata filtering masking and watershed segmentation .
def chm_segmentation(dsm_fn):
    driver = gdal.GetDriverByName("GTiff")
    dsm_ds = gdal.Open(dsm_fn)
    dsm_ds.GetRasterBand(1).SetNoDataValue(0)
    chm_array = dsm_ds.GetRasterBand(1).ReadAsArray().astype(np.float32)
    start = time.time()
    print("Watershed segmentation...")
    chm_array_smooth = ndi.gaussian_filter(chm_array, 0,
                                           mode='constant',
                                           cval=0,
                                           truncate=1)
    chm_array_smooth[chm_array == 0] = 0
    max_coords = peak_local_max(chm_array_smooth,
                                footprint=np.ones((5, 5)))
    markers = np.zeros_like(chm_array_smooth, dtype=bool)
    markers[tuple(max_coords.T)] = True
    markers = ndi.label(markers)[0]
    chm_mask = chm_array_smooth.copy()
    chm_mask[chm_array_smooth > 0] = 1
    chm_mask[chm_array_smooth <= 0] = 0
    labels = watershed(chm_array_smooth,
                       markers,
                       compactness=2,
                       mask=chm_mask,
                       watershed_line=False)
    segments = np.where(labels == 0, -9999, labels)
    dsm_ds = None
    end = time.time()
    print("CHM segmented in {:.4f} sec\n".format(end - start))
    return segments
def chm_segmentation ( dsm_fn ) : driver = gdal . GetDriverByName ( "GTiff" ) dsm_ds = gdal . Open ( dsm_fn ) dsm_ds . GetRasterBand ( 1 ) . SetNoDataValue ( 0 ) chm_array = dsm_ds . GetRasterBand ( 1 ) . ReadAsArray ( ) . astype ( np . float32 ) start = time . time ( ) print ( "Watershed segmentation..." ) chm_array_smooth = ndi . gaussian_filter ( chm_array , 0 , mode = 'constant' , cval = 0 , truncate = 1 ) chm_array_smooth [ chm_array == 0 ] = 0 max_coords = peak_local_max ( chm_array_smooth , footprint = np . ones ( ( 5 , 5 ) ) ) markers = np . zeros_like ( chm_array_smooth , dtype = bool ) markers [ tuple ( max_coords . T ) ] = True markers = ndi . label ( markers ) [ 0 ] chm_mask = chm_array_smooth . copy ( ) chm_mask [ chm_array_smooth > 0 ] = 1 chm_mask [ chm_array_smooth <= 0 ] = 0 labels = watershed ( chm_array_smooth , markers , compactness = 2 , mask = chm_mask , watershed_line = False ) segments = np . where ( labels == 0 , - 9999 , labels ) dsm_ds = None end = time . time ( ) print ( "CHM segmented in {:.4f} sec\n" . format ( end - start ) ) return segments
CHM ata filtering, masking and watershed segmentation.
CHM ata filtering masking and watershed segmentation .
"""CHM ata filtering, masking and watershed segmentation.
    Idea taken from https://www.neonscience.org/resources/learning-hub/tutorials/calc-biomass-py""" # APPLYING GAUSSIAN FILTER TO REMOVE WRONG POINTS
 # CALCULATE LOCAL MAXIMUM POINTS
 # CREATE MASK TO MATCH INPUT ARRAY SIZE
 # IDENTIFY ALL THE MAXIMUM POINTS
 # CREATE MASK TO REDUCE SEGMENTATION ONLY TO TREES
 # WATERSHED SEGMENTATION
 # CHANGING 0 VALUES TO -9999

dsm_fn