MINC/教程/Python 示例
外觀
秉承其他關於如何使用 MINC 工具包進行程式設計的教程,這裡是一個簡單的例子,演示如何開啟一個體積,為每個體素加一,然後再次寫入。這是 Python,程式碼非常簡潔!
#!/usr/bin/env python
from pyminc.volumes.factory import *
from numpy import *
import sys
if __name__ == "__main__":
# get the input file
infile = volumeFromFile(sys.argv[1])
# get the output file using the same dimension info as the input file
outfile = volumeLikeFile(sys.argv[1], sys.argv[2])
# add one to the data
outfile.data = infile.data + 1
# write out and close the volumes
outfile.writeFile()
outfile.closeVolume()
infile.closeVolume()
程式碼的功能如下。前面的部分是不同模組的匯入 - 唯一非標準的模組是 pyminc.volumes.factory。幾乎所有 pyminc 體積例項的建立都是透過一組工廠方法完成的。它們是
| 函式 | 描述 |
|---|---|
| volumeFromFile | 從現有檔案開啟 MINC 體積 - 用於讀取現有資料。 |
| volumeLikeFile | 從現有檔案建立一個新的 MINC 體積 - 用於寫入資料。 |
| volumeFromInstance | 從現有例項(Python 物件)建立一個新的 MINC 體積。 |
| volumeFromDescription | 透過指定尺寸、大小、起始位置等來建立一個新的 MINC 體積。 |
程式碼的下一行將輸出檔案的資料賦值為輸入檔案的資料加 1。每個 pyminc 體積都具有一個數據屬性,它是一個 numpy 陣列。請注意,直到訪問資料屬性時才實際讀取資料。最後的部分將輸出檔案寫入磁碟並關閉這兩個體積。