How can I plot hysteresis threshold in Matplotlib?

Hysteresis thresholding is an edge detection technique that uses two thresholds to identify strong and weak edges, connecting weak edges to strong ones. In Matplotlib, we can visualize this process using scikit-image filters and display the results.

What is Hysteresis Thresholding?

Hysteresis thresholding works with two threshold values:

  • High threshold: Identifies strong edges (definitely edges)
  • Low threshold: Identifies potential weak edges
  • Weak edges connected to strong edges are kept, isolated weak edges are removed

Complete Example

Here's how to plot hysteresis threshold results using the Sobel edge detector ?

import matplotlib.pyplot as plt
from skimage import data, filters

# Set figure size
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

# Create subplots
fig, ax = plt.subplots(nrows=1, ncols=2)

# Load sample image (Greek coins)
orig_img = data.coins()

# Apply Sobel edge detection
edges = filters.sobel(orig_img)

# Define threshold values
low = 0.1
high = 0.4

# Apply hysteresis thresholding
hyst = filters.apply_hysteresis_threshold(edges, low, high)

# Create binary masks for visualization
low_mask = (edges > low).astype(int)
high_mask = (edges > high).astype(int)

# Display hysteresis result and original image
ax[0].imshow(high_mask + hyst, cmap='magma')
ax[0].set_xlabel('Hysteresis threshold')
ax[0].set_title('Processed Image')

ax[1].imshow(orig_img, cmap='magma')
ax[1].set_xlabel('Original Image')
ax[1].set_title('Greek Coins')

plt.tight_layout()
plt.show()
Displays two side-by-side images:
- Left: Hysteresis thresholded edges in magma colormap
- Right: Original Greek coins image in magma colormap

Key Parameters

Parameter Purpose Typical Range
low Lower threshold for weak edges 0.05 - 0.2
high Upper threshold for strong edges 0.2 - 0.5
edges Edge magnitude image Sobel/Canny output

Step-by-Step Process

  1. Edge Detection: Apply Sobel filter to find edge magnitudes
  2. Threshold Setting: Define low and high threshold values
  3. Hysteresis: Connect weak edges to strong edges
  4. Visualization: Display results using imshow()

Conclusion

Hysteresis thresholding effectively removes noise while preserving important edge connections. Use filters.apply_hysteresis_threshold() with appropriate low and high values for optimal edge detection results.

Updated on: 2026-03-25T23:44:47+05:30

755 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements