跳至内容

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);
}
华夏公益教科书