Solution officielle
import numpy as np
def sobel(image_gris):
h, w = image_gris.shape
img = image_gris.astype(np.float64)
gx_kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.float64)
gy_kernel = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]], dtype=np.float64)
résultat = np.zeros((h, w), dtype=np.float64)
for i in range(1, h - 1):
for j in range(1, w - 1):
region = img[i-1:i+2, j-1:j+2]
gx = np.sum(region * gx_kernel)
gy = np.sum(region * gy_kernel)
résultat[i, j] = np.sqrt(gx**2 + gy**2)
if résultat.max() > 0:
résultat = résultat / résultat.max() * 255
return résultat.astype(np.uint8)