如何处理谷歌地球引擎中图像绝对值之和的卷积

时间:2018-01-07 14:13:11

标签: javascript google-earth-engine

我想对图像进行以下卷积运算:计算窗口中每个值与中心像素值之差的绝对值之和。

我怎么能在Google地球引擎Javascript API中做到这一点?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我相信你可以在图片上使用.neighborhoodToBands()方法来获得你想要的东西。我是通过Google地球引擎API教程中的Texture page了解到的。

首先,加载图像并选择一个波段:

// Load a high-resolution NAIP image.
var image = ee.Image('USDA/NAIP/DOQQ/m_3712213_sw_10_1_20140613');

// Zoom to San Francisco, display.
Map.setCenter(-122.466123, 37.769833, 17);
Map.addLayer(image, {max: 255}, 'image');

// Get the NIR band.
var nir = image.select('N');

然后创建一个内核:

// Create a list of weights for a 9x9 kernel.
var list = [1, 1, 1, 1, 1, 1, 1, 1, 1];
// The center of the kernel is zero.
var centerList = [1, 1, 1, 1, 0, 1, 1, 1, 1];
// Assemble a list of lists: the 9x9 kernel weights as a 2-D matrix.
var lists = [list, list, list, list, centerList, list, list, list, list];
// Create the kernel from the weights.
// Non-zero weights represent the spatial neighborhood.
var kernel = ee.Kernel.fixed(9, 9, lists, -4, -4, false);

创建内核的替代方法...这是我用来创建一个在中心具有0权重并具有用户定义半径的内核的函数:

var create_kernel = function(pixel_radius) {
  var pixel_diameter = 2 * pixel_radius + 1;
  var weight_val = 1;
  var weights = ee.List.repeat(ee.List.repeat(weight_val, pixel_diameter), pixel_diameter);

  var mid_row = ee.List.repeat(weight_val, pixel_radius)
    .cat([0])
    .cat(ee.List.repeat(weight_val, pixel_radius));

  weights = weights.set(pixel_radius, mid_row);

  var kernel = ee.Kernel.fixed({
    height: pixel_diameter,
    width: pixel_diameter,
    weights: weights
  });

  return kernel;
};

var kernel = create_kernel(4);

将邻域转换为band,然后执行计算:

// Convert the neighborhood into multiple bands.
var neighs = nir.neighborhoodToBands(kernel);
print(neighs);

// Compute convolution; Focal pixel (represented by original nir image)
// Subtract away the 80 bands representing the 80 neighbors
// (9x9 neighborhood = 81 pixels - 1 focal pixel = 80 neighbors)
var convolution = nir.subtract(neighs).abs().reduce(ee.Reducer.sum());
print(convolution);
Map.addLayer(convolution,
             {min: 20, max: 2500, palette: ['0000CC', 'CC0000']},
             "Convolution");

这能满足您的需求吗?

Here's the link to the GEE code: