<jupyter_start><jupyter_text>Load packages and initiate earth engine<jupyter_code>import ee
ee.Initialize()

import folium
import palette
import geemap.eefolium as emap
import subprocess
#import geemap as emap
from IPython.display import Image<jupyter_output><empty_output><jupyter_text>Define aoi of NAIP images<jupyter_code>collection = ee.ImageCollection('USDA/NAIP/DOQQ')
aoi = ee.Geometry.Polygon([
    [-74.02,40.90],
          [-74.02,40.85],
          [-73.90,40.85],
          [-73.90,40.90]
])
centroid = aoi.centroid()
long, lat = centroid.getInfo()['coordinates']
print("long = {}, lat = {}".format(long,lat))<jupyter_output>long = -73.95999999999945, lat = 40.87501239848901<jupyter_text>Filter NAIP image collection by time and aoi<jupyter_code>long_lat = ee.Geometry.Point(long, lat)
naip = collection.filterBounds(aoi)
naip17 = collection.filterDate('2017-05-01','2017-8-05')
count = naip17.size().getInfo()
print('Count:', count)<jupyter_output>Count: 53586<jupyter_text>Display NAIP images for aoi<jupyter_code>Map = emap.Map(center=[lat,long], zoom=15)

Map.add_basemap('TERRAIN') 
#vis = {'bands': ['N', 'R', 'G']}

vis = {'bands': ['R', 'G', 'B']}
imgs = naip17.mosaic().clip(aoi)
#Map.addLayer(aoi)
Map.addLayer(imgs,vis)
Map<jupyter_output><empty_output><jupyter_text>Calculate NDVI<jupyter_code>#nir, r = imgs.select('N'), imgs.select('R')
ndvi = imgs.normalizedDifference(["N", "R"])
ndvi_vis = {'min': -1, 'max': 1, 'palette':['red',  'yellow', 'green']}

#Map.add_basemap('TERRAIN') 
Map.addLayer(ndvi,ndvi_vis)
Map<jupyter_output><empty_output><jupyter_text>Show region with NDVI higher than 0.1<jupyter_code>veg_mask = ndvi.updateMask(ndvi.gte(0.1))
veg_vis = {'min': 0, 'max': 1, 'palette': ['blue']}
Map.addLayer(veg_mask,veg_vis)
Map<jupyter_output><empty_output><jupyter_text>Images segmentation<jupyter_code>seed = ee.Algorithms.Image.Segmentation.seedGrid(6)
#seg = ee.Algorithms.Image.Segmentation.GMeans(image=imgs,numIterations=100,pValue=50,neighborhoodSize=500)
seg = ee.Algorithms.Image.Segmentation.SNIC(image=imgs, size=10,compactness= 0, neighborhoodSize=500,connectivity= 8, seeds=seed).select(['R_mean', 'G_mean', 'B_mean', 'N_mean', 'clusters'], ['R', 'G', 'B', 'N', 'clusters'])
clusters = seg.select('clusters')
seg_vis = {'bands': ['R', 'G', 'B'], 'min':0, 'max':1, 'gamma':0.8}
Map.addLayer(clusters.randomVisualizer(), {}, 'clusters',opacity=1)
Map<jupyter_output><empty_output><jupyter_text>Calculate per-cluster features (for future use)<jupyter_code>## ndvi
seg_ndvi = ndvi.addBands(clusters).reduceConnectedComponents(ee.Reducer.mean(),'clusters').rename('seg_ndvi')
Map.addLayer(seg_ndvi,{},'seg_ndvi')

## standard-deviation
std = ndvi.addBands(clusters).reduceConnectedComponents(ee.Reducer.stdDev(),'clusters').rename('std')
Map.addLayer(std,{},'StdDev')

## area
area = ee.Image.pixelArea().addBands(clusters).reduceConnectedComponents(ee.Reducer.sum(), 'clusters')
Map.addLayer(area,{}, 'Area')

## perimeter
minMax = clusters.reduceNeighborhood(ee.Reducer.minMax(), ee.Kernel.square(1))
perimeterPixels = minMax.select(0).neq(minMax.select(1)).rename('perimeter')
Map.addLayer(perimeterPixels,{},'perimeterPixels')
perimeter = perimeterPixels.addBands(clusters).reduceConnectedComponents(ee.Reducer.sum(), 'clusters')
Map.addLayer(perimeter, {}, 'Perimeter')

## width and height
sizes = ee.Image.pixelLonLat().addBands(clusters).reduceConnectedComponents(ee.Reducer.minMax(), 'clusters')
width = sizes.select('longitude_max').subtract(sizes.select('longitude_min')).rename('width')
height = sizes.select('latitude_max').subtract(sizes.select('latitude_min')).rename('height')
Map.addLayer(width, {},'Width')
Map.addLayer(height, {}, 'Height')<jupyter_output><empty_output><jupyter_text>Filter out objects with NDVI < 0.1<jupyter_code>seg_veg = seg_ndvi.updateMask(seg_ndvi.gte(0.1))

Map2 = emap.Map(center=[lat,long], zoom=15)
Map2.add_basemap('SATELLITE') 
Map2.addLayer(seg_veg.randomVisualizer(), {})
Map2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18311266550087435, 'fraction_numerical': 0.03347489382962778, 'mean_word_length': 8.162471395881006, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '204425', 'license': 'MIT', 'n_tokens_mistral': 1640, 'n_tokens_neox': 1552, 'n_words': 212, 'path': 'code/GEE -- NAIP Segmentation.ipynb', 'repo_name': 'wanwanliang/Image-Processing', 'size': '4003'}