計算距離
「距離轉換」會計算每個像素到某個點的距離,通常是到最近的背景像素。這能讓你判斷物件中哪些點更偏向內部、哪些更靠近邊緣。
在這個練習中,請在 labels 裡的左心室物件上使用「歐幾里得距離」(Euclidean distance)的轉換。
本練習屬於課程
Python 的生醫影像分析
練習說明
- 建立左心室像素的遮罩(在
labels中數值為 1)。 - 使用
ndi.distance_transform_edt()為每個像素計算到背景的距離,並將像素尺寸提供給sampling參數。 - 使用
ndi.maximum與ndi.maximum_position印出最大距離及其座標。 - 將距離圖的一個切片疊加到原始影像上。這部分已為你完成。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Calculate left ventricle distances
lv = np.where(____, 1, 0)
dists = ____
# Report on distances
print('Max distance (mm):', ____)
print('Max location:', ____)
# Plot overlay of distances
overlay = np.where(dists[5] > 0, dists[5], np.nan)
plt.imshow(overlay, cmap='hot')
format_and_render_plot()