GEE: Sobel operator convolution

Author:CSDN @ _养乐多_

This article will delve into a classic algorithm in edge detection, namely Sobel operator convolution. We will introduce the basic principles of this algorithm and demonstrate how to apply the Sobel operator for image convolution operations in Google Earth Engine. Taking the NDVI of the experimental area as an example, the true color image, NDVI image and Sobel convolution results of the research area are as follows:

Insert image description here



1. Sobel operator convolution

The convolution kernel of Sobel operator convolution is as follows. For detailed introduction, please refer to "Remote Sensing Digital Image Processing Tutorial (Third Edition), Wei Yuchun, Tang Guoan...".

            [0, 0, 24, 24, 0, 0, 0],
            [0, 0, 24, 24, 0, 0, 0],
            [0, 0, 24, 24, 0, 0, 0],
            [0, 0, 24, 24, 24, 24, 24],
            [0, 0, 24, 24, 24, 24, 24],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0]

For a detailed explanation of the function, please see the blog "GEE: Gradient Convolution".

2. Complete code

// 设置感兴趣区域(ROI)并可视化
var roi = geometry; // 定义感兴趣区域
var roiImage = ee.Image().toByte().paint({
    
     featureCollection: roi, color: 'red', width: 1 });
Map.addLayer(roiImage, {
    
     palette: 'red' }, '感兴趣区域'); // 在地图上添加感兴趣区域图层
Map.centerObject(roi, 11); // 将地图中心设置为感兴趣区域的中心位置

// 获取LANDSAT遥感影像数据集
var dataset = ee.ImageCollection('LANDSAT/LC09/C02/T1_L2')
    .filterDate('2022-01-01', '2022-12-31'); // 筛选日期范围

// 应用缩放因子
function applyScaleFactors(image) {
    
    
    var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2); // 光学波段缩放
    var thermalBands = image.select('ST_B.*').multiply(0.00341802).add(149.0); // 热红外波段缩放
    return image.addBands(opticalBands, null, true)
        .addBands(thermalBands, null, true);
}

dataset = dataset.map(applyScaleFactors);

// 从数据集中选择波段创建影像
var imageYLD = dataset.select(['SR_B5', 'SR_B4', 'SR_B3', 'SR_B2'])
    .filterBounds(roi).median()
    .clip(roi);

// 可视化真彩色影像
var visualization = {
    
    
    bands: ['SR_B4', 'SR_B3', 'SR_B2'],
    min: 0.0,
    max: 0.3,
};

Map.addLayer(imageYLD, visualization, '真彩色 (432)');

// 计算归一化植被指数(NDVI)
var ndvi = imageYLD.normalizedDifference(['SR_B5', 'SR_B4']);
var visParam = {
    
    
    palette:
        'FFFFFF, CE7E45, DF923D, F1B555, FCD163, 99B718, 74A901, 66A000, 529400,' +
        '3E8601, 207401, 056201, 004C00, 023B01, 012E01, 011D01, 011301'
};
Map.addLayer(ndvi, visParam, 'NDVI');

// 使用自适应可视化函数
var eevis = require('users/949384116/lib:ImageVisualization/vislib');

// Roberts算子卷积函数
function RobertsConvolve(image) {
    
    
  
    var Roberts_Kernel = ee.Kernel.fixed(7, 7,
        [
            [0, 0, 12, 0, 0, 0, 0],
            [0, 0, 12, 0, 0, 0, 0],
            [0, 0, 12, 0, 0, 0, 0],
            [0, 0, 12, 12, 12, 12, 12],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0]
        ]); // 定义Roberts算子滤波核

    return image.convolve(Roberts_Kernel);
};

// 应用Roberts算子卷积
var convolvedRobertsImage = RobertsConvolve(ndvi);

// 使用自适应可视化函数
eevis.adaptiveVisualization(convolvedRobertsImage, 'Roberts', roi, 'nd', true, 30, 
['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red'], "EPSG:4326", true, 5);

// Sobel算子卷积函数
function SobelConvolve(image) {
    
    
  
    var Sobel_Kernel = ee.Kernel.fixed(7, 7,
        [
            [0, 0, 24, 24, 0, 0, 0],
            [0, 0, 24, 24, 0, 0, 0],
            [0, 0, 24, 24, 0, 0, 0],
            [0, 0, 24, 24, 24, 24, 24],
            [0, 0, 24, 24, 24, 24, 24],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0]
        ]); // 定义RSobel算子滤波核

    return image.convolve(Sobel_Kernel);
};

// 应用Roberts算子卷积
var convolvedSobelImage = SobelConvolve(ndvi);

// 使用自适应可视化函数
eevis.adaptiveVisualization(convolvedSobelImage, 'Sobel', roi, 'nd', true, 30, 
['black', 'blue', 'purple', 'cyan', 'green', 'yellow', 'red'], "EPSG:4326", true, 5);

3. Code link

https://code.earthengine.google.com/2e3c53579a987be81e33943acbaf5f8a?noload=true

Statement:
As an author, I attach great importance to my own works and intellectual property rights. I hereby declare that all my original articles are protected by copyright law and no one may publish them publicly without my authorization.
My articles have been published on some well-known platforms for a fee. I hope that readers can respect intellectual property rights and refrain from infringement. Any act of publishing paid articles on the Internet for free or for a fee (including commercial use) without my authorization will be regarded as infringement of my copyright, and I reserve the right to pursue legal liability.
Thank you readers for your attention and support for my article!

Guess you like

Origin blog.csdn.net/qq_35591253/article/details/134764120