GEE问题:image集合中median和first的区别

2024-08-14 14:24:28 浏览数 (1)

问题

我是GEE的新手。我正在试图理解两个图像之间的位移。 我正在尝试以下例子: - 加载图像 - 手动替换(将图像移动40米) - 使用位移函数计算图像移动了多少。 - 如果一切顺利,我应该后退40米 如果我将代码应用于“原始”图像 image 1 = collection.First()一切正常 如果我将代码应用于“中位数”图像 image 1 = collection.median()它不是!

函数

median()

Reduces an image collection by calculating the median of all values at each pixel across the stack of all matching bands. Bands are matched by name.

通过计算所有匹配波段堆栈中每个像素处所有值的中位数来聚合图像集合。乐队按名称匹配。

Arguments:

this:collection (ImageCollection):

The image collection to reduce.

Returns: Image

first()

Returns the first entry from a given collection.

Arguments:

this:imagecollection (ImageCollection):

The ImageCollection instance.

Returns: Image

代码

代码语言:javascript复制
// area of interest

var geoJSON = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "coordinates": [
          [
            [
              12.411401665803425,
              41.68238436943494
            ],
            [
              12.411401665803425,
              41.64142345909477
            ],
            [
              12.463637299444343,
              41.64142345909477
            ],
            [
              12.463637299444343,
              41.68238436943494
            ],
            [
              12.411401665803425,
              41.68238436943494
            ]
          ]
        ],
        "type": "Polygon"
      }
    }
  ]
}
var coords = geoJSON['features'][0]['geometry']['coordinates']
var aoi = ee.Geometry.Polygon(coords)

// load original image

var start_date = ee.Date('2022-01-01')
var end_date = ee.Date('2024-07-31')
var image1 = ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
var image = image1.filter(ee.Filter.bounds(aoi)).select(['B4', 'B3','B2'])

image1 = image1.filter(ee.Filter.date(start_date,end_date)).median() 
print(image1)

image1 = image.filter(ee.Filter.date(start_date,end_date)).first()
print(image1)
// <== if I use first(), then it works!!?
image1 = image1.rename('R', 'G', 'B').clip(aoi)

// create a displacement image, to shift image of 40 m
// this is probably a stupid way of creating such a displacement...

var x = image1.gt(0)
x = x.addBands({srcImg:x.select('R').multiply(40), overwrite:true})
x = x.addBands({srcImg:x.select('G').multiply(0), overwrite:true})

// create a displaced (shifted) image

var image2 = image1.displace(x)
var center = aoi.centroid()
print(center)

//var s2Vis = {
//  min: 0.0,
//  max: 3000,
//  bands: ['R', 'G', 'B'],
//}
//Map.setCenter(12.44,41.66, 9)
//Map.addLayer(image1,s2Vis);
//Map.addLayer(image2,s2Vis);


// compute displacement

var image1RedBand = image1.select('R')
var image2RedBand = image2.select('R')
var displacement = image2RedBand.displacement({
  referenceImage: image1RedBand,
  maxOffset: 50.0,
  patchWidth: 100.0
});

var offset = displacement.select('dx').hypot(displacement.select('dy'))
var theMax = offset.reduceRegion({reducer:ee.Reducer.max(), geometry:aoi, scale:10, bestEffort:true})
var theMin = offset.reduceRegion({reducer:ee.Reducer.min(), geometry:aoi, scale:10, bestEffort:true})
print('max displacement =',theMax.values())
print('min displacement =',theMin.values())

结果

解答

这里我们需要进行明白的就是,first函数在默认状态下进行了影像属性的copy但是我们这里如果用median的话就不没有办法自动copy,如果我们想要实现上面的功能,就需要用下面的函数:

copyProperties(source, properties, exclude)

Copies metadata properties from one element to another.

Arguments:

this:destination (Element, default: null):

The object whose properties to override.

source (Element, default: null):

The object from which to copy the properties.

properties (List, default: null):

The properties to copy. If omitted, all ordinary (i.e. non-system) properties are copied.

exclude (List, default: null):

The list of properties to exclude when copying all properties. Must not be specified if properties is.

Returns: Element

至于需要什么属性,我们要根据自己的情况去分析,利用这个函数来实现属性的分析。这样后续就可以进行相关的操作。

0 人点赞