ITK入门教程(9)ITK向量图像

1.概述

许多图像处理任务要求非标量像素类型的图像,一个常见的例子就是向量图像。图像类型要求能够表示标量图像的梯度。下面的代码阐述了如何实例化和使用向量类型像素的图像。

方便起见,我们用itk::Vector 类来定义像素类型。向量类用来表示空间中的几何向量,这同STL 中把std::vector 作为数组容器的用法是不同的。如果你对容器感兴趣,itk::VectorContainer 类将提供给你所需要的功能。

  • 1.第一步就是包含向量类的头文件。#include "itkVector.h"
  • 2.在此例中,我们希望向量维数和图像维数相匹配,但是这并不是强制要求的。我们可以用一个三维向量作为像素来定义一个四维的图像。
typedef itk::Vector< float, 3 > PixelType;
typedef itk::Image< PixelType, 3 > ImageType;

由于向量类从itk::FixedArray 类继承了[]操作符,所以也可以使用索引符号来访问向量元素。

ImageType::PixelType pixelValue;
pixelValue[0] = 1.345; // x component
pixelValue[1] = 6.841; // y component
pixelValue[2] = 3.295; // x component

现在可以通过定义一个索引值并调用SetPixel( )方法来将这个向量储存到一个图像像素中。

image->SetPixel( pixelIndex, pixelValue );

2.代码

#include "itkVector.h"//向量类的头文件
#include "itkImage.h"
 
int main(int, char *[])
{
  /*向量类的使用是在基于空间中代表坐标和维数的类型之上进行模板化的。在此例中,向
  量的长度和图像长度相匹配,但是并不是完全相同。我们可以用一个三维的向量作为像素来
  定义一个四维的图像*/
  typedef itk::Vector< float, 3 >       PixelType;
  typedef itk::Image< PixelType, 3 >    ImageType;
  
  ImageType::Pointer image = ImageType::New();
 
  const ImageType::IndexType start = {{0,0,0}}; //First index at {X,Y,Z}
  const ImageType::SizeType  size = {{200,200,200}}; //Size of {X,Y,Z}
 
  ImageType::RegionType region;
  region.SetSize( size );
  region.SetIndex( start );
 
  image->SetRegions( region );
  image->Allocate();
 
  ImageType::PixelType  initialValue;
  initialValue.Fill( 0.0 );
  image->FillBuffer( initialValue );
  const ImageType::IndexType pixelIndex = {{27,29,37}}; //{X,Y,Z}对应像素索引位置
  //由于向量类从 itk::FixedArray 类继承了[] 操作,所以也可以使用 index 符号来访问向量成员
  ImageType::PixelType   pixelValue;
  pixelValue[0] =  1.345;   // x component
  pixelValue[1] =  6.841;   // y component
  pixelValue[2] =  3.295;   // x component
 
  std::cout << "pixelIndex索引处给定的向量值:" << std::endl;
  std::cout << pixelValue << std::endl;
  //通过定义一个标识并调用 SetPixel( ) 方法来将这个向量储存到pixelIndex索引像素中
  image->SetPixel(   pixelIndex,   pixelValue  );
  //获取pixelIndex处像素值value
  ImageType::PixelType value = image->GetPixel( pixelIndex );
  //打印像素值value
  std::cout << "pixelIndex索引处读取的像素值:" << std::endl;
  std::cout << value << std::endl;
 
  return EXIT_SUCCESS;
}

3.结果

在这里插入图片描述

参考目录

https://blog.csdn.net/qq_32809093/article/details/116886260

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值