Để vẽ ngưỡng hystereses trong Matplotlib, chúng ta có thể thực hiện các bước sau:
- Thiết lập kích thước figure và điều chỉnh padding giữa và xung quanh subplots.
- Tạo figure và tập subplots.
- Load một số đồng xu Hy Lạp, đồng xu Hy Lạp từ Pompeii.
- Tìm high, low và edges của ảnh dùng bộ lọc sobel.
- Áp dụng ngưỡng hystereses cho "image".
- Hiển thị dữ liệu như hình ảnh, tức là trên raster 2D đều đặn, dùng imshow().
- Đặt title cho ảnh gốc và ảnh có ngưỡng hystereses.
- Để hiển thị figure, dùng phương thức show().
Ví dụ
import matplotlib.pyplot as plt
from skimage import data, filters
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots(nrows=1, ncols=2)
orig_img = data.coins()
edges = filters.sobel(orig_img)
low = 0.1
high = 0.4
low = (edges > low).astype(int)
height = (edges > high).astype(int)
hyst = filters.apply_hysteresis_threshold(edges, low, high)
ax[0].imshow(height + hyst, cmap='magma')
ax[0].set_xlabel('Hysteresis threshold')
ax[1].imshow(orig_img, cmap='magma')
ax[1].set_xlabel('Original Image')
plt.show()
Kết quả
