Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save monogenea/df5ace81753de803b71b313f7069bd87 to your computer and use it in GitHub Desktop.
def run_canny(image, sigma, t_low, t_high, aperture=3):
"""Apply Gaussian blur then Canny edge detection."""
# Kernel size: 2 * ceil(3*sigma) + 1 ensures the kernel covers ±3σ
ksize = 2 * int(np.ceil(3 * sigma)) + 1
blurred = cv2.GaussianBlur(image, (ksize, ksize), sigmaX=sigma)
edges = cv2.Canny(blurred, threshold1=t_low, threshold2=t_high, apertureSize=aperture)
return edges
edges = run_canny(image, sigma=1.4, t_low=50, t_high=150)
fig, axes = plt.subplots(1, 2, figsize=(10, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original')
axes[0].axis('off')
axes[1].imshow(edges, cmap='gray')
axes[1].set_title('Canny edges (σ=1.4, T_low=50, T_high=150)')
axes[1].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