灰度圖讀取介紹
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);
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 對源圖像做重采樣操作,最后結果如下:
這里設定的參數分別為:
- X、Y 軸平移量12.0,13.1;
- 旋轉中心:源圖像中心點;
- 旋轉角度:20°*Π/180;