0

I trying to extract roads from Sentinel-2 images using the python api from google earth engine. I am following an article that calculates a road index and then uses a bottom-hat morphological filter to accentuate the roads before classification.

Since GEE does not have the bottom-hat filter, I tried to implement it but I am not sure it is working correctly, speacilly because the output image I am getting doesn't seem to have any changes, even with a different values in the variable radius.

Considering that Bottom-hat filtering is the equivalent of subtracting the input image from the result of performing a morphological closing operation on the input image, and the closing operation in gee is done using the focalMax(), this is the code I have done:

def bottom_hat_morph_filter(image,radius):
  kernel = ee.Kernel.circle(radius) #pixels
  closing = image.focalMax(kernel=kernel,iterations=1)
  bottom_hat = closing.subtract(image)
  return bottom_hat

Am I missing something?

1 Answer 1

2

The function look OK to me. I'm guessing the problem is elsewhere in your code. I ported it to JavaScript to quickly verify:

function bottom_hat_morph_filter(image, radius) {
  var kernel = ee.Kernel.circle(radius)
  var closing = image.focalMax({kernel: kernel})
  var bottom_hat = closing.subtract(image)
  return bottom_hat
}

var image = ee.Image('COPERNICUS/S2_SR_HARMONIZED/20220629T142731_20220629T142725_T20LNR')

Map.centerObject(image, 14)
var visParams = {bands: 'B4,B3,B2', min: 0, max: 3000, gamma: 1.5}
Map.addLayer(image, visParams, 'original')
Map.addLayer(bottom_hat_morph_filter(image, 3), visParams, '3 px')
Map.addLayer(bottom_hat_morph_filter(image, 10), visParams, '10 px')

https://code.earthengine.google.com/d8579d6ae6abe9d45565085c6933da5d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.