1.函数说明
template<typename TPixel, unsigned int VImageDimension = 2> class Image : public itk::ImageBase<VImageDimension>
模板化的 n 维图像类。
图像基于像素类型(对因变量建模)和维度(自变量的数量)进行模板化。像素数据的容器是 ImportImageContainer
。
在像素容器内,图像被建模为数组,由起始索引和大小定义。
Image
的超类 ImageBase
根据图像在物理空间中的位置、图像在物理空间中的方向、像素的大小以及图像本身的范围来定义图像的几何形状。 ImageBase
提供了在索引和物理空间坐标系之间转换的方法。
像素可以使用 SetPixel()
和 GetPixel()
方法直接访问,也可以通过定义它们遍历的图像区域的迭代器访问。
使用 SetRequestedRegion
告诉过滤器只对指定的 ImageRegion
进行操作的关键是在过滤器 GetOutput()
上调用 SetRequestedRegion
。也就是说,要告诉 DerivativeImageFilter
只对一个小区域进行操作,你必须这样做
derivativeFilter->GetOutput()->SetRequestedRegion(smallRegion);
derivativeFilter->Update();
2.代码展示
(1)Python代码
#!/usr/bin/env python
import itk
Dimension = 2
PixelType = itk.F
ImageType = itk.Image[PixelType, Dimension]
smallSize = itk.Size[Dimension]()
smallSize.Fill(10)
index = itk.Index[Dimension]()
index.Fill(0)
region = itk.ImageRegion[Dimension]()
region.SetIndex(index)
region.SetSize(smallSize)
bigSize = itk.Size[Dimension]()
bigSize.Fill(10)
RandomSourceType = itk.RandomImageSource[ImageType]
randomImageSource = RandomSourceType.New()
randomImageSource.SetNumberOfWorkUnits(1) # to produce non-random results
randomImageSource.SetSize(bigSize)
randomImageSource.GetOutput().SetRequestedRegion(region)
print("Created random image.")
DerivativeImageFilterType = itk.DerivativeImageFilter[ImageType, ImageType]
derivativeFilter = DerivativeImageFilterType.New()
derivativeFilter.SetInput(randomImageSource.GetOutput())
derivativeFilter.SetDirection(0) # "x" axis
derivativeFilter.GetOutput().SetRequestedRegion(region)
derivativeFilter.Update()
print("Computed derivative.")
(2)C++代码
#include "itkImage.h"
#include "itkRandomImageSource.h"
#include "itkDerivativeImageFilter.h"
int main(int, char *[])
{
constexpr unsigned int Dimension = 2;
using PixelType = float;
using ImageType = itk::Image<PixelType, Dimension>;
ImageType::SizeType smallSize;
smallSize.Fill(10);
ImageType::IndexType index;
index.Fill(0);
ImageType::RegionType region(index, smallSize);
ImageType::SizeType bigSize;
bigSize.Fill(10000);
using RandomSourceType = itk::RandomImageSource<ImageType>;
RandomSourceType::Pointer randomImageSource = RandomSourceType::New();
randomImageSource->SetNumberOfWorkUnits(1); // to produce non-random results
randomImageSource->SetSize(bigSize);
randomImageSource->GetOutput()->SetRequestedRegion(smallSize);
std::cout << "Created random image." << std::endl;
using DerivativeImageFilterType = itk::DerivativeImageFilter<ImageType, ImageType>;
DerivativeImageFilterType::Pointer derivativeFilter = DerivativeImageFilterType::New();
derivativeFilter->SetInput(randomImageSource->GetOutput());
derivativeFilter->SetDirection(0); // "x" axis
derivativeFilter->GetOutput()->SetRequestedRegion(smallSize);
derivativeFilter->Update();
std::cout << "Computed derivative." << std::endl;
return EXIT_SUCCESS;
}