量測視野畸變¶
兩台顯微鏡的影像幾何很少完全一致。像素尺寸差了幾個百分點,或相機光學造成的些微桶形畸變,在單張影像中看不出來,卻會讓同一樣本的兩張影像越往視野邊緣錯開得越多。本教學以
pysrx.register 對照一台參考顯微鏡量測這兩者:縮放誤差與
徑向畸變 \(k_1\),並附上一個不依賴模型的獨立檢查。
同樣的方法曾以 LSM 980 為參考,量測 ZEISS Lightfield 4D 與 Leica THUNDER 轉盤共軛焦;結果在光場頁面。
需要準備的東西¶
- 同一個樣本的兩次擷取:一次在要量測的顯微鏡上,一次在參考顯微鏡上(例如共軛焦,它的掃描視野通常校準得很好)。樣本應該平坦或很薄,而且處處有紋理:例如 Convallaria 這類組織切片、密集的螢光珠玻片,或網格、圖樣玻片。
- 各自的體素尺寸(µm),照擷取軟體回報的數值。結果會告訴你兩者差多少。
- 重疊的視野。 參考視野應盡量涵蓋另一台的視野:畸變越往角落越大,而只有兩台顯微鏡都看得到的區域才會被量測。
- 互資訊步驟可選用 SimpleITK(在你的專案中執行
pixi add simpleitk);沒有安裝時,傳入mutual_information=False。
把每個堆疊載入為 NumPy 陣列,(Z, Y, X) 或 (C, Z, Y, X),並附上 (z, y, x) 體素尺寸。單張影像 (Y, X) 搭配 (y, x) 像素尺寸也可以;此時不擬合 z 位移。
import numpy as np
from pysrx import register as R
ref, ref_sp = ..., (1.0, 0.5, 0.5) # the reference stack (e.g. the confocal) and its voxel size, µm
mov, mov_sp = ..., (2.0, 0.65, 0.65) # the microscope to measure
本頁的數字來自一組答案已知的合成資料,因此可以看出擬合有多接近真值。它模擬一台相機式顯微鏡(312 µm 視野、0.65 µm 像素),帶有 2.5 % 的縮放誤差與視野角落 −0.6 % 的桶形畸變,並以涵蓋中央 280 µm 的共軛焦作為參考:
產生合成資料
from scipy import ndimage as ndi
rng = np.random.default_rng(0)
ref_sp, mov_sp = (1.0, 0.5, 0.5), (2.0, 0.65, 0.65) # µm (z, y, x): confocal, camera microscope
nz, ny, nx = 12, 480, 480 # camera microscope: a 312 µm field, 24 µm deep
fine = np.zeros((4 * nz, ny, nx), np.float32) # blobs in a thin slab, z sampled 4x finer
n = 14000
fine[rng.normal(2 * nz, 0.4 * nz, n).clip(0, 4 * nz - 1).astype(int),
rng.integers(0, ny, n), rng.integers(0, nx, n)] = rng.uniform(0.3, 1.0, n)
mov = ndi.gaussian_filter(fine, (3.0, 1.8, 1.8))[1::4] + 0.002 * rng.standard_normal((nz, ny, nx))
truth = R.FieldTransform.for_fields((560, 560), ref_sp, (ny, nx), mov_sp, ty=14.0, tx=18.0, theta=0.0087, tz=5.0)
truth.s, truth.k1 = 0.975, -0.006 # 2.5 % scale error, barrel distortion
ref = np.nan_to_num(R.resample_to_ref(mov, (16, 560, 560), ref_sp, mov_sp, truth)) # the "confocal": 280 µm
1. 每個視野一張影像¶
配準是在每台顯微鏡的一張 2D 影像上進行:各通道先除以自己的第 99.5 百分位數再相加,堆疊只在對焦的平面範圍內做最大投影,避免空白平面稀釋紋理。
ref_reg, mov_reg = R.registration_image(ref), R.registration_image(mov) # channels -> one image
ref2d, mov2d = R.focus_mip(ref_reg), R.focus_mip(mov_reg) # in-focus band only
2. 擬合模型¶
模型把參考影像中的一點對應到另一台顯微鏡的視野:先做平面內旋轉 \(\theta\) 與平移 \(t\),再以該視野中心為準,套用等向縮放 \(s\) 與 Brown-Conrady 徑向畸變 \(k_1\):
其中 \(u\) 是旋轉、平移後的點,\(c\) 是視野中心,\(r_n\) 是中心到視野角落的距離。以 \(r_n\) 正規化後,\(k_1\) 就是角落處的相對畸變量:負值為桶形,正值為枕形。擬合分步進行,每一步都從上一步的結果出發:
ty, tx = R.coarse_xy(ref2d, mov2d, ref_sp, mov_sp) # step 1: template matching
theta, ty, tx = R.fine_xy(ref2d, mov2d, ref_sp, mov_sp, ty, tx) # step 2: mutual information
rigid = R.FieldTransform.for_fields(ref2d.shape, ref_sp, mov2d.shape, mov_sp, ty=ty, tx=tx, theta=theta)
tf = R.fit_distortion(ref2d, mov2d, ref_sp, mov_sp, rigid) # step 3: NCC over ty, tx, θ, s, k1
tf.tz = R.search_z(ref_reg, mov_reg, ref_sp, mov_sp, tf) # step 4: z offset
coarse_xy在 1 µm/px 下以模板比對找出平移:這裡是 (13.0, 18.0) µm。fine_xy以互資訊做剛體擬合加以修正(SimpleITK,Euler2D,0.5 µm/px): 0.521°、(13.69, 17.75) µm。剛體擬合無法吸收縮放誤差,所以仍有偏差。fit_distortion以 Powell 搜尋,針對平移、旋轉、\(s\) 和 \(k_1\) 最大化正規化互相關(NCC)(有 CuPy 時在 GPU 上執行)。search_z掃描 z 位移,找出使各平面 NCC 平均值最大者。
| \(s\) | \(k_1\) | \(\theta\) | \(t_z, t_y, t_x\) | NCC | |
|---|---|---|---|---|---|
| 擬合 | 0.9751 | −0.0066 | 0.499° | 5.04, 14.00, 18.00 µm | 0.415 → 0.991 |
| 真值 | 0.975 | −0.006 | 0.498° | 5, 14, 18 µm |
NCC 一欄是投影影像在步驟 3 之前(剛體)與之後的一致程度。register
一次執行全部四個步驟以及下面的檢查:
tf, field = R.register(ref, mov, ref_sp, mov_sp) # mutual_information=False without SimpleITK
3. 不靠模型的檢查¶
擬合的模型只會找到它被允許找到的東西。這項檢查直接量測位移場:
displacement_field 只用剛體轉換(不含縮放與 \(k_1\))重新取樣另一張影像,把兩張影像切成 48 µm、每 24 µm 一塊的圖塊,以相位相關法量測每個圖塊的位移。紋理太少、落在視野外,或位移後比對不佳的圖塊會被剔除。
radial_displacement 取出每個位移沿著視野中心徑向的分量。
field = R.displacement_field(ref2d, mov2d, ref_sp, mov_sp, rigid, tile_um=48.0)
r, dr = R.radial_displacement(field, tf) # radius from the field centre, radial displacement (µm)
共有 99 個圖塊通過。若模型正確,dr 會沿著模型的徑向位移
\(r\,(s\,(1 + k_1 r^2 / r_n^2) - 1)\) 分布,只剩下量測的散佈。
4. 解讀結果¶
summary = R.distortion_summary(tf, field)
| 本例 | 真值 | 意義 | |
|---|---|---|---|
scale_error_pct |
−2.49 % | −2.5 % | \(100\,(s - 1)\):受測影像中的距離比參考影像短(負值)或長多少。通常是像素尺寸的校準問題;它隨著與中心的距離線性增加。 |
k1_pct_at_corner |
−0.66 % | −0.60 % | \(100\,k_1\):縮放之外,視野角落的徑向畸變。負值:桶形;正值:枕形。 |
k1_shift_at_corner_um |
−1.45 µm | −1.32 µm | \(k_1 r_n\):單由畸變造成的視野角落位移。拿它和像素尺寸比較。 |
corner_radius_um |
220.6 µm | \(r_n\),受測視野中心到角落的距離。 | |
measured_radius_max_um |
164.5 µm | 有量測圖塊的最大半徑;超出範圍的部分是外推的模型。 | |
fit_rms_um |
0.30 µm | 圖塊徑向位移相對於模型的 RMS:模型解釋不了的散佈。遠小於一個像素,表示模型足以描述這個視野。 | |
n_tiles |
99 | 通過品質檢查的圖塊數。 |
縮放的誤差只有 0.01 %。\(k_1\) 差了 0.06 %,在角落相當於 0.13 µm:參考影像只涵蓋到 221 µm 角落半徑中的 164 µm,而在這個範圍內,桶形多一點、縮放少一點,看起來幾乎一樣。真正決定像素位置的總位移,在整個量測區域內與真值相差不到 0.03 µm。
5. 繪圖¶
徑向位移對半徑作圖,可以同時看到量測與模型:
import matplotlib.pyplot as plt
rr = np.linspace(0, tf.rn, 200)
inside = rr <= summary["measured_radius_max_um"]
fig, ax = plt.subplots(figsize=(5.5, 3.6))
ax.scatter(r, dr, s=10, alpha=0.6, lw=0, label="measured tiles")
ax.plot(rr[inside], tf.radial_model(rr[inside]), "k", lw=2, label="fit (scale + k₁)")
ax.plot(rr[~inside], tf.radial_model(rr[~inside]), "k--", lw=2, label="extrapolated")
ax.axhline(0, color="0.6", lw=0.8)
ax.set(xlabel="radius from field centre (µm)", ylabel="radial displacement (µm)")
ax.legend(frameon=False, loc="lower left")
fig.savefig("fd_radial.png", dpi=160, bbox_inches="tight")

縮放誤差是曲線的直線部分,\(k_1\) 是往邊緣的彎曲。虛線:最後一個量測圖塊之外,模型是外推的。
畸變後的網格顯示 \(k_1\) 的形狀,並放大到看得見:
E = 10 # exaggeration
H, W = mov2d.shape[0] * mov_sp[1], mov2d.shape[1] * mov_sp[2]
t = np.linspace(0, max(H, W), 200)
fig, ax = plt.subplots(figsize=(4.2, 4.2))
for g in np.arange(0, max(H, W) + 1e-6, 50.0):
for y, x in ((np.full_like(t, g), t), (t, np.full_like(t, g))):
keep = (y <= H) & (x <= W)
dy, dx = y[keep] - tf.dcy, x[keep] - tf.dcx
f = tf.k1 * (dy**2 + dx**2) / tf.rn**2 # k1 alone; scale left out
ax.plot(dx, dy, color="0.75", lw=0.6)
ax.plot(dx * (1 + E * f), dy * (1 + E * f), color="C1", lw=1.0)
corners_y, corners_x = np.array([0, 0, 1, 1, 0]) * ref2d.shape[0] * ref_sp[1], np.array([0, 1, 1, 0, 0]) * ref2d.shape[1] * ref_sp[2]
_, ym, xm = tf.map(0, corners_y, corners_x) # the reference field, in the moving frame
ax.plot(xm - tf.dcx, ym - tf.dcy, "k--", lw=1)
ax.set(xlabel="x from field centre (µm)", ylabel="y from field centre (µm)", aspect="equal")
ax.invert_yaxis()
fig.savefig("fd_grid.png", dpi=160, bbox_inches="tight")

50 µm 網格(灰色)與擬合出的 \(k_1\),放大十倍(橘色)。虛線:參考影像涵蓋的區域,依擬合結果旋轉。
光場頁面以 LSM 980 為參考,為四次真實擷取畫出了同樣的兩張圖。
注意事項¶
- 樣本要平坦。 不擬合傾斜。厚或傾斜的樣本會讓視野各處對焦在不同深度,可能被誤讀為畸變;請用薄切片或玻片,並使用對焦範圍的投影。
- 處處要有足夠紋理。 擬合和圖塊都需要整個視野都有結構。空白區域產生不了圖塊,而畸變最大的角落最重要。
- 視野涵蓋範圍。 只有兩台顯微鏡都看得到的區域會被量測。參考視野小時,縮放與 \(k_1\) 會互相抵換(如上例);移動樣本重複量測,以涵蓋角落。
- 量測範圍外的 \(k_1\) 是外推的,角落位移也是。請同時參考
measured_radius_max_um。 - 是相對值,不是絕對值。 結果只說明兩台顯微鏡差多少,而不說明哪一台才正確。要判斷這點,請使用經過校準的參考(或載物台測微尺)。
- 擬合 RMS 遠大於一個像素,表示有模型沒涵蓋的因素:傾斜、光軸偏離中心,或比對錯誤。請檢查徑向位移圖。