跳至內容

MINC/教程/程式設計02

來自華夏公益教科書,開放的書籍,開放的世界

兩個 MINC 座標系。

[編輯 | 編輯原始碼]

MINC 有兩種座標系:體素世界 座標系。體素座標類似於陣列,原點固定在影像的一個角落,並遞增到該維度的長度 - 1。另一方面,世界座標反映了每個體素在現實世界單位中的位置。在一個特定的空間內,例如Talairach 空間,每個特定座標因此具有意義,並且通常在體積之間共享。

前面的示例只是使用體素座標獲得了體素 - 以下是修改後的相同程式碼,用於獲取世界座標系原點的體素。

#include <minc2.h>
#include <stdio.h>

int main(int argc, char **argv) {
  mihandle_t    minc_volume;
  double        voxel;
  int           result, i;
  double        world_location[3];
  double        dvoxel_location[3];
  unsigned long voxel_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);
  }

到目前為止,一切都很好 - 這與第一個示例中的程式碼幾乎相同,只是添加了一些額外的變數。

  /* convert world coordinates to voxel coordinates */
  world_location[0] = world_location[1] = world_location[2] = 0.0;
  miconvert_world_to_voxel(minc_volume, world_location, dvoxel_location);

這就是實際轉換髮生的地方,由miconvert_world_to_voxel 函式處理。它有三個引數:體積結構(由miopen_volume 初始化)、包含要轉換的世界座標的雙精度陣列(在上面的行中初始化為全零),以及將儲存相應體素座標的雙精度陣列。

  /* miconvert_world_to_voxel needs an array of doubles     *
   * but miget_real_value needs unsigned longs - so we cast */
  for (i=0; i<3; i++) { 
    voxel_location[i] = (unsigned long) dvoxel_location[i];
  }

  printf("Voxel location of xyz 0 0 0: %lu %lu %lu\n", 
         voxel_location[0], voxel_location[1], voxel_location[2]);

  /* print the value at that location */
  miget_real_value(minc_volume, voxel_location, 3, &voxel);
  printf("Voxel at xyz 0 0 0 was: %f\n", voxel);

  return(0);
}

其餘的程式碼應該再次看起來很熟悉。裡面有一個有點奇怪的強制轉換,因為miconvert_world_to_voxel 將轉換後的體素座標分配給一個雙精度陣列,但miget_real_value 需要一個無符號長整型陣列。我們可以進行一些真實的插值,但我們會偷懶,只將雙精度數強制轉換為無符號長整型。

程式碼

[編輯 | 編輯原始碼]
#include <minc2.h>
#include <stdio.h>

int main(int argc, char **argv) {
  mihandle_t    minc_volume;
  double        voxel;
  int           result, i;
  double        world_location[3];
  double        dvoxel_location[3];
  unsigned long voxel_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);
  }

  /* convert world coordinates to voxel coordinates */
  world_location[0] = world_location[1] = world_location[2] = 0.0;
  miconvert_world_to_voxel(minc_volume, world_location, dvoxel_location);

  /* miconvert_world_to_voxel needs an array of doubles     *
   * but miget_real_value needs unsigned longs - so we cast */
  for (i=0; i<3; i++) { 
    voxel_location[i] = (unsigned long) dvoxel_location[i];
  }

  printf("Voxel location of xyz 0 0 0: %lu %lu %lu\n", 
	 voxel_location[0], voxel_location[1], voxel_location[2]);

  /* print the value at that location */
  miget_real_value(minc_volume, voxel_location, 3, &voxel);
  printf("Voxel at xyz 0 0 0 was: %f\n", voxel);

  return(0);
}
華夏公益教科書