Skip to content

Instantly share code, notes, and snippets.

@monogenea
Last active May 3, 2026 12:55
Show Gist options
  • Select an option

  • Save monogenea/628f59f6e9df52031bbda81c4b154dca to your computer and use it in GitHub Desktop.

Select an option

Save monogenea/628f59f6e9df52031bbda81c4b154dca to your computer and use it in GitHub Desktop.
# Gaussian blur — sigma controlled by ksize (must be odd) and sigmaX
blurred = cv2.GaussianBlur(image, ksize=(5, 5), sigmaX=1.4)
# Sobel gradients in x and y
Gx = cv2.Sobel(blurred, cv2.CV_64F, dx=1, dy=0, ksize=3)
Gy = cv2.Sobel(blurred, cv2.CV_64F, dx=0, dy=1, ksize=3)
# Gradient magnitude
magnitude = np.sqrt(Gx**2 + Gy**2)
magnitude = (magnitude / magnitude.max() * 255).astype(np.uint8)
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
for ax, img, title in zip(axes, [blurred, magnitude], ['Gaussian blur (σ=1.4)', '|∇f| - Sobel magnitude']):
ax.imshow(img, cmap='gray')
ax.set_title(title)
ax.axis('off')
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment