canny边缘检测
主要功能
使用landsat5数据计算归一化植被指数,之后对植被指数计算结果执行canny边缘检测,输出检测出的边缘
代码
// Canny Edge Detector example.
// Load an image and compute NDVI from it.
var image = ee.Image('LANDSAT/LT05/C01/T1_TOA/LT05_031034_20110619');
var ndvi = image.normalizedDifference(['B4','B3']);
// Detect edges in the composite.
var canny = ee.Algorithms.CannyEdgeDetector(ndvi, 0.7);
// Mask the image with itself to get rid of areas with no edges.
canny = canny.updateMask(canny);
Map.setCenter(-101.05259, 37.93418, 13);
Map.addLayer(ndvi, {min: 0, max: 1}, 'Landsat NDVI');
Map.addLayer(canny, {min: 0, max: 1, palette: 'FF0000'}, 'Canny Edges');
步骤分析
- 创建ee对象,获取landsat05数据
- 指定特定波段,计算归一化植被指数(NDVI)
- canny边缘检测
- 剔除检测出的非边缘部分
- 设置地图显示中心,缩放等级
- 添加计算出的NDVI图层
- 添加检测出的边缘图层
主要方法
- ee.Image.normalizeDifference()
Computes the normalized difference between two bands. If the bands to use are not specified, uses the first two bands. The normalized difference is computed as (first − second) / (first + second).
Arguments:
this:input (Image):
The input image.
bandNames (List, default: null):
A list of names specifying the bands to use. If not specified, the first and second bands are used.
Returns: Image
计算归一化差值指数,若没有指定波段,则计算前两个波段。
- ee.Algorithms.CannyEdgeDetector()
Applies the Canny edge detection algorithm to an image. The output is an image whose bands have the same names as the input bands, and in which non-zero values indicate edges, and the magnitude of the value is the gradient magnitude.
Arguments:
image (Image):
The image on which to apply edge detection.
threshold (Float):
Threshold value. The pixel is only considered for edge detection if the gradient magnitude is higher than this threshold.
sigma (Float, default: 1):
Sigma value for a gaussian filter applied before edge detection. 0 means apply no filtering.
Returns: Image
对影像对象执行canny边缘检测。输出的结果影像中具有输入影像相同波段名称,非零区域指示为边缘区域。值为梯度值。
输入参数:
输入影像对象,阈值(浮点型,梯度值的阈值),sigma表示在边缘检测之前,对影像进行高斯平滑的大小。0表示不执行平滑。
- ee.Image.updateMask()
Updates an image's mask at all positions where the existing mask is not zero. The output image retains the metadata and footprint of the input image.
Arguments:
this:image (Image):
Input image.
mask (Image):
New mask for the image, as a floating-point value in the range [0, 1] (invalid = 0, valid = 1). If this image has a single band, it is used for all bands in the input image; otherwise, must have the same number of bands as the input image.
Returns: Image
对影像进行掩膜,非零区域得到保留,影像外边界得到保留。
输入参数:
输入影像对象,掩膜影像。
本例中,为了显示边缘检测的结果,需要增加掩膜这一步骤,然后使用一种颜色(红色)来展示结果