MINC/教程/程式設計01
任何值得稱道的程式語言入門都會從一個 Hello World 示例開始。當然,MINC 並不是它自己的程式語言,而是一個 C 庫。因此,本教程將假設對 C 有一定的基本瞭解,並指導讀者完成開啟和讀取 MINC 卷的操作,然後繼續討論更高階的主題,並在此過程中討論 MINC 檔案格式的一些特殊之處。
所有程式碼示例都將使用 MINC 庫版本 2.x(在 2.0.13 上測試)。還有另外兩個庫用於訪問 MINC 檔案中的資料:MINC 版本 1.x 庫和 volume_io。絕大多數現有的程式碼使用這兩個庫中的一個;MINC2 是最近引入的一個新庫。一個重要的區別是 MINC2 只能讀取 2.x 版本的檔案;舊檔案必須使用 mincconvert 命令進行轉換。
在本教程中,我們將把程式碼分解並逐段註釋。每個教程的末尾都可以找到未經修改的原始碼。
程式碼以以下方式編譯(假設 minc-toolkit-v2 安裝在 /opt/minc/1.9.15 中)
gcc -g -o minc2_tutorial1 -I /opt/minc/1.9.15/include -L /opt/minc/1.9.15/lib -lminc2 -lhdf5 -lniftiio -lznz -lz -lm -ldl -lrt -lnetcdf minc2_tutorial1.c
現在開始 Hello World 示例。在這裡,我們開啟一個 MINC2 卷,並獲取第一個體素的值。
#include <minc2.h>
#include <stdio.h>
包含檔案 - 僅需要 minc2.h 來獲取所有 minc2 功能。
int main(int argc, char **argv) {
mihandle_t minc_volume;
double voxel;
int result;
unsigned long location[3];
資料型別:這裡唯一非標準的型別是 mihandle_t,這是一個結構體,幾乎所有需要在 minc2 捲上執行某些操作的 minc2 函式都需要使用它。
/* open the volume - first and only command line argument */
result = miopen_volume(argv[1], MI2_OPEN_READ, &minc_volume);
/* check for error on opening */
if (result != MI_NOERROR) {
fprintf(stderr, "Error opening input file: %d.\n", result);
}
這是第一個 minc2 命令:miopen_volume,正如你所料,它開啟一個由 C 字串指定的 minc2 卷,一個指示檔案是應該開啟進行讀取還是寫入的標誌(在本例中為讀取),以及 mihandle_t 結構體的地址,如果操作成功,它將儲存有關卷的重要資訊。
/* get the first voxel in the image */
location[0] = location[1] = location[2] = 0;
miget_real_value(minc_volume, location, 3, &voxel);
printf("Hello MINC2 - first voxel was: %f\n", voxel);
return(0);
}
現在我們獲取第一個體素,它由體積座標 0,0,0 指定。我們在這裡只檢視三維體積,因此我們建立了一個包含三個元素的 unsigned long 陣列,並將它們都設定為 0。然後,我們呼叫 miget_real_value 函式,指定用於獲取體素的卷控制代碼、位置(unsigned long 陣列)、檔案中維度的數量以及要分配體素值的 double 的地址。然後,我們將其列印到終端。
就是這樣 - 第一個也是非常簡單的 MINC2 程式,實際上只需要兩個庫呼叫:打開卷和獲取體素。
#include <minc2.h>
#include <stdio.h>
int main(int argc, char **argv) {
mihandle_t minc_volume;
double voxel;
int result;
misize_t location[3];
/* open the volume - first and only command line argument */
result = miopen_volume(argv[1], MI2_OPEN_READ, &minc_volume);
/* check for error on opening */
if (result != MI_NOERROR) {
fprintf(stderr, "Error opening input file: %d.\n", result);
}
/* get the first voxel in the image */
location[0] = location[1] = location[2] = 0;
miget_real_value(minc_volume, location, 3, &voxel);
printf("Hello MINC2 - first voxel was: %f\n", voxel);
return(0);
}