灰度图读取介绍
itk 默认读取图片的格式为灰度图,读取过程分为下面三个部分:
-
定义 PixelType,一般 为 float 或 unsigned char;
-
通过 itk::Image<Pixeltype,Dimension> 完成图像类型的定义;
-
利用 itk::FileImageName 创建 reader 实例,并通过文件路径对单通道图片、或 Mha 三维图像读取,
下方为其中的核心代码:
using PixlType = unsigned char;
using ImageType = itk::Image<PixlType,3>;
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(OpenPath);</pre>
RGB 读取流程
与灰度图不同的是,针对 RGB 图像,ITK 提供了一个专有的类 itk::RGBPixel() ,用于像素类型定义,而后面图像类型定义、图像读取与 Gray 图没有太大的区别
为了加深理解应用,这里写了一个例子;代码功能流程如下:
-
读取 RGB 图像
-
图像读取之后沿着X 、Y轴分别平移,以图像中心作为中心进行旋转某一角度;
-
变换后的图像再以 RGB 图像格式进行保存;
#include<itkRGBPixel.h>
#include<itkImage.h>
#include<itkImageFileReader.h>
#include<itkImageFileWriter.h>
#include<itkPNGImageIOFactory.h>
#include<itkResampleImageFilter.h>
#include<itkCenteredRigid2DTransform.h>
#include<iostream>
#include<cmath>
# define PI acos(-1)
using namespace std;
int RGB_Read_main()
{
itk::PNGImageIOFactory::RegisterOneFactory();
string Open_Path = "D:/ceshi1/1/203.png";
string Save_Path = "D:/ceshi1/1/203_1.png";
using PixelType = itk::RGBPixel<unsigned char>;
constexpr unsigned int Dimension = 2;
using ImageType = itk::Image<PixelType, Dimension>;
using ReadType = itk::ImageFileReader<ImageType>;
using WriteType = itk::ImageFileWriter<ImageType>;
ReadType::Pointer reader = ReadType::New();
WriteType::Pointer writer = WriteType::New();
reader->SetFileName(Open_Path);
writer->SetFileName(Save_Path);
reader->Update();
const ImageType::SpacingType& spacing = reader->GetOutput()->GetSpacing();
const ImageType::PointType& origin = reader->GetOutput()->GetOrigin();
const ImageType::SizeType size = reader->GetOutput()->GetLargestPossibleRegion().GetSize();
using TransformType = itk::CenteredRigid2DTransform<double>;
using RescaleImageType = itk::ResampleImageFilter<ImageType, ImageType>;
TransformType::Pointer transform = TransformType::New();
RescaleImageType::Pointer rescale = RescaleImageType::New();
TransformType::OutputVectorType translation1;
translation1[0] = 12.0;
translation1[1] = 13.1;
const double imageCenterx = origin[0] + spacing[0] * size[0] / 2.0;
const double imageCenterY = origin[1] + spacing[1] * size[1] / 2.0;
transform->SetTranslation(translation1);
transform->SetAngle(2 * PI / 180.0);
TransformType::OutputPointType Center;
Center[0] = imageCenterx;
Center[1] = imageCenterY;
transform->SetCenter(Center);
cout << "Center1 ::" << imageCenterx << endl;
cout << "Center2::" << imageCenterY << endl;
rescale->SetDefaultPixelValue(100);
rescale->SetTransform(transform);
rescale->SetSize(reader->GetOutput()->GetLargestPossibleRegion().GetSize());
rescale->SetOutputOrigin(reader->GetOutput()->GetOrigin());
rescale->SetOutputSpacing(reader->GetOutput()->GetSpacing());
rescale->SetOutputDirection(reader->GetOutput()->GetDirection());
rescale->SetInput(reader->GetOutput());
writer->SetInput(rescale->GetOutput());
try
{
writer->Update();
cout << "Sucessfully Converted !" << endl;
return EXIT_SUCCESS;
}
catch (itk::ExceptionObject & e)
{
cout << e.what() << endl;
cout << "Expectation Caught!!!" << endl;
return EXIT_FAILURE;
}
代码中实现图像变换,用到的是 itkCenteredRigid2DTransform.h 类,该类的基本功能就是面向 2D 图像进行中心刚性变换;用 CenteredRigid2DTransform 做变换时,需要提供三个参数:
1,x、y 轴平移量;
2,旋转中心坐标;
3,旋转弧度(弧度 = 角度*Π/180,这里设置的是双精度 );
最后把 transform 矩阵应用到源图像时,需要借助 ResampleImageFilter 对源图像做重采样操作,最后结果如下:
save.png这里设定的参数分别为:
-
X、Y 轴平移量12.0,13.1;
-
旋转中心:源图像中心点;
-
旋转角度:20°*Π/180;
网友评论