我想对图像做如下卷积操作:计算窗口中每个值与中心像素值之差的绝对值之和。
我怎么能在谷歌地球引擎 Javascript API 中做到这一点?
非常感谢 !
我想对图像做如下卷积操作:计算窗口中每个值与中心像素值之差的绝对值之和。
我怎么能在谷歌地球引擎 Javascript API 中做到这一点?
非常感谢 !
我相信您可以.neighborhoodToBands()
在图像上使用该方法来获得所需的内容。我从Google Earth Engine API 教程的Texture 页面学到了这一点。
首先,加载图像并选择单个波段:
// 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);
将邻域转换为波段,然后执行计算:
// 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");
这能得到你想要的吗?