方法一:
var smooth_map = map_classification
.focal_mode({
radius: 10, kernelType: 'octagon', units: 'pixels', iterations: 1
})
.mask(map_classification.gte(1))
var crude_object_removal = map_classification
.updateMask(map_classification.connectedPixelCount(50, false).gte(50))
.unmask(smooth_map)
方法二(消除某一特定类的小斑块):
var smooth_map_15 = map_classification.mask(map_classification.neq(15))
.focal_mode({
radius: 20, kernelType: 'circle', units: 'pixels', iterations: 1
})
.mask(map_classification.gte(1))
var limit_size_15 = map_classification
.updateMask(map_classification.gt(0)
.subtract(map_classification.eq(15).selfMask()
.connectedPixelCount(300, false).lt(300).unmask(0)
) // clunky, sure there's a simpler way...
)
.unmask(smooth_map_15)
var smooth_map_14 = map_classification.mask(map_classification.neq(14))
.focal_mode({
radius: 20, kernelType: 'circle', units: 'pixels', iterations: 1
})
.mask(map_classification.gte(1))
var limit_size_14 = limit_size_15
.updateMask(map_classification.gt(0)
.subtract(map_classification.eq(14).selfMask()
.connectedPixelCount(300, false).lt(300).unmask(0)
) // clunky, sure there's a simpler way...
)
.unmask(smooth_map_14)
方法三(基于斑块边界相邻关系消除斑块):
var border_pixels = map_classification.reduceNeighborhood({
reducer: ee.Reducer.countDistinct(),
kernel: ee.Kernel.square(1)
})
var neighbour_pixels = map_classification.rename('classes').addBands(border_pixels)
.reduceConnectedComponents(ee.Reducer.max(), 'classes', 2000)
var border_compl_15 = map_classification
.updateMask(map_classification.gt(0)
.subtract(
map_classification.eq(15).and(neighbour_pixels.lte(3))
.unmask(0)
)
)
.unmask(smooth_map_15)
方法四(通过矢量化来判断某斑块百分之多少被某某斑块包围):
var map_fc = map_classification.reduceToVectors({
scale: 10,
eightConnected: false,
bestEffort: true,
maxPixels: 1e13,
tileScale: 1
})
print(map_fc.limit(50)) //只显示前50个元素信息
print(map_fc.size()) //返回元素数量
var set_neighbour_properties = function(f) {
var diff = f.buffer(10).difference(f, ee.ErrorMargin(0.5)) //提取缓冲区
var diff_classes = ee.Dictionary(
map_classification.unmask(ee.Image(0)).reduceRegion({
reducer: ee.Reducer.frequencyHistogram(),
geometry: diff.geometry(),
scale: 10
}).get('classification')
)
var diff_sum = diff_classes.toArray().reduce(ee.Reducer.sum(), [0]).get([0])
var diff_percs = diff_classes.map(function(k,v){return(ee.Number(v).divide(diff_sum).multiply(100))})
return(f.set(diff_percs))
}