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;
}