GEE面向对象分类(先分割影像 、再计算特征、最后分类)
生活随笔
收集整理的這篇文章主要介紹了
GEE面向对象分类(先分割影像 、再计算特征、最后分类)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
GEE有內存限制,我這面向對象提取不太好用,如有大神有更好的方法,請多指導。
//-------------------------------去云 ----------------------------------------------// function sentinel2toa(img) {var toa = img.select(['B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9','B10', 'B11','B12'], ['aerosol', 'blue', 'green', 'red', 're1','re2','re3', 'nir','nir2', 'h2o', 'cirrus','swir1', 'swir2']).divide(10000).addBands(img.select(['QA60'])).set('solar_azimuth',img.get('MEAN_SOLAR_AZIMUTH_ANGLE')).set('solar_zenith',img.get('MEAN_SOLAR_ZENITH_ANGLE'))return toa; } function ESAcloud(toa) {var qa = toa.select('QA60');var cloudBitMask = Math.pow(2, 10);var cirrusBitMask = Math.pow(2, 11);var clear = qa.bitwiseAnd(cloudBitMask).eq(0).and(qa.bitwiseAnd(cirrusBitMask).eq(0));var cloud = clear.eq(0);return cloud; } function shadowMask(toa,cloud){var azimuth =ee.Number(toa.get('solar_azimuth')).multiply(Math.PI).divide(180.0).add(ee.Number(0.5).multiply(Math.PI));var zenith =ee.Number(0.5).multiply(Math.PI ).subtract(ee.Number(toa.get('solar_zenith')).multiply(Math.PI).divide(180.0));var nominalScale = cloud.projection().nominalScale();var cloudHeights = ee.List.sequence(200,10000,500);var shadows = cloudHeights.map(function(cloudHeight){cloudHeight = ee.Number(cloudHeight);var shadowVector = zenith.tan().multiply(cloudHeight);var x = azimuth.cos().multiply(shadowVector).divide(nominalScale).round();var y = azimuth.sin().multiply(shadowVector).divide(nominalScale).round();return cloud.changeProj(cloud.projection(), cloud.projection().translate(x, y));});var potentialShadow = ee.ImageCollection.fromImages(shadows).max();potentialShadow = potentialShadow.and(cloud.not());var darkPixels = toa.normalizedDifference(['green', 'swir2']).gt(0.25).rename(['dark_pixels']);var shadow = potentialShadow.and(darkPixels).rename('shadows');return shadow; }function cloud_and_shadow_mask(img) {var toa = sentinel2toa(img);var cloud = ESAcloud(toa);var shadow = shadowMask(toa,cloud);var mask = cloud.or(shadow).eq(0);return toa.updateMask(mask); } //-------------------------------Main----------------------------------------------------------------------------------------// var table = ee.FeatureCollection("users/xzhangsx/sample200"); var bands =['B1','B2', 'B3', 'B4', 'B5', 'B6','B7','B8','B8A','B9']; var composite = ee.ImageCollection('COPERNICUS/S2').filterDate('2020-06-01', '2020-06-15').filterBounds(table).mean(); var image = ee.Image(composite).divide(255).select(bands); print(image); //設置種子 var seeds = ee.Algorithms.Image.Segmentation.seedGrid(36); //利用 SNIC 進行分割,得到對象。參數的設置根據自己的需求來 var snic = ee.Algorithms.Image.Segmentation.SNIC({image: image, size: 100,compactness: 5,connectivity: 8,neighborhoodSize:256,seeds: seeds }).select(['B1_mean','B2_mean','B3_mean','B4_mean','B5_mean','B6_mean','B7_mean','B8_mean','B8A_mean','B9_mean', 'clusters'], ['B1','B2','B3','B4','B5','B6','B7','B8','B8A','B9', 'clusters']); var clusters = snic.select('clusters');// print("snic",snic); //計算每個對象的特征 var stdDev = image.addBands(clusters).reduceConnectedComponents(ee.Reducer.stdDev(), 'clusters', 256);//為啥和上面的均值不一樣? var area = ee.Image.pixelArea().addBands(clusters).reduceConnectedComponents(ee.Reducer.sum(), 'clusters', 256); var minMax = clusters.reduceNeighborhood(ee.Reducer.minMax(), ee.Kernel.square(1)); var perimeterPixels = minMax.select(0).neq(minMax.select(1)).rename('perimeter'); var perimeter = perimeterPixels.addBands(clusters).reduceConnectedComponents(ee.Reducer.sum(), 'clusters', 256); var sizes = ee.Image.pixelLonLat().addBands(clusters).reduceConnectedComponents(ee.Reducer.minMax(), 'clusters', 256); var width = sizes.select('longitude_max').subtract(sizes.select('longitude_min')).rename('width'); var height = sizes.select('latitude_max').subtract(sizes.select('latitude_min')).rename('height'); //分類器參數設置,選擇分類依據:包括了area,width等 var objectPropertiesImage = ee.Image.cat([snic.select(bands),//stdDev,// area,//perimeter,//width,//height ]).float(); print(objectPropertiesImage); //var img2 = ee.Image(objectPropertiesImage).divide(255); //選擇訓練樣本 var training = objectPropertiesImage.sampleRegions({collection: table,properties: ['value'],scale: 30,tileScale:8 }); print("training",training);/*var training = objectPropertiesImage.addBands(table.select('value')).updateMask(seeds).sample(table, 5); print("training2",training2);*/ //分類 var classifier = ee.Classifier.smileCart().train(training, 'value'); print(classifier); var result=objectPropertiesImage.classify(classifier,'Classified objects'); //Export.image.toCloudStorage(result,"202012172236"); //var resultshp = result.updateMask(seeds).sample(table, 5); //Export.table.toCloudStorage(resultshp,"202012172120"); print(result); Map.centerObject(table); Map.addLayer(result.randomVisualizer(), {}, 'Classified objects2'); // 計算混淆矩陣 var test=training.classify(classifier,'classification'); var confusionMatrix = test.errorMatrix('value', 'classification'); print('Confusion Matrix', confusionMatrix);?
總結
以上是生活随笔為你收集整理的GEE面向对象分类(先分割影像 、再计算特征、最后分类)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java开源生鲜电商平台-深刻理解电商的
- 下一篇: python音标1003python音标