前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GEE问题:image集合中median和first的区别

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

作者头像
此星光明
发布2024-08-14 14:24:28
870
发布2024-08-14 14:24:28
举报

问题

我是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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-08-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题
  • 函数
    • median()
      • Arguments:
      • Returns: Image
    • first()
      • Arguments:
      • Returns: Image
  • 代码
  • 结果
  • 解答
    • copyProperties(source, properties, exclude)
      • Arguments:
      • Returns: Element
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档