I am using the c++ library of ITK and I am trying to fulfill my first script of reading images. I ref https://itk.org/ITKExamples/src/IO/ImageBase/ReadAnImage/Documentation.html?highlight=read%20image for my codes. And I tried to put the the readimage part into a separate function by using the pointer of itkImage. However, it seems that the pointer is not working properly. After calling the readimage function, there is no image data for the corresponding itkimage pointer.
Here are my codes. The size of the image prints correctly when calling inside the ReadImage function (which is [549,778]). When called in the main function, the size is [0,0], which means that inside the ReadImage, the modified pointer is not saved. Any suggestion is welcome!!
#include "itkImage.h"
#include "itkImageFileReader.h"
#include <iostream>
#include <string>
using PixelType = double;
constexpr unsigned int Dimension_img = 2;
using ImageType = itk::Image<PixelType, Dimension_img>;
constexpr unsigned int Dimension_mask = 3;
using MaskType = itk::Image<unsigned int, Dimension_mask>;
static void ReadImage(int num, std::string name, ImageType * image);
static void ReadMask(int num, std::string name, MaskType::Pointer mask);
int main(int argc, char * argv[])
{
if (argc < 2){
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0];
std::cerr << " <InputPatientNO.>";
std::cerr << std::endl;
return EXIT_FAILURE;
}
int num = atoi(argv[1]);
ImageType::Pointer image1 = ImageType::New();
ReadImage(num,"_2CH_ED", image1);
ImageType::Pointer image2 = ImageType::New();
ReadImage(num,"_2CH_ES", image2);
ImageType::RegionType region = image1->GetLargestPossibleRegion();
ImageType::SizeType size = region.GetSize();
std::cout << size << std::endl;
std::cout<<"HELLO ITK BLOCK MATCHING!"<<std::endl;
return 0;
}
static void ReadImage(int num, std::string name, ImageType * image)
{
// name is usually _2C_ED / _2C_ES
std::string prefix = "/data/training/patient";
std::string old_string = std::to_string(num);
std::string filled_num = std::string(4 - old_string.length(), '0') + old_string;
std::string filename = prefix + filled_num + "/patient" + filled_num + name +".mhd";
//std::cout<<filename<<std::endl;
using ReaderType = itk::ImageFileReader<ImageType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(filename);
reader->Update();
image = reader->GetOutput();
ImageType::RegionType region = image->GetLargestPossibleRegion();
ImageType::SizeType size = region.GetSize();
std::cout << size << std::endl;
}
static void ReadMask(int num, std::string name, MaskType::Pointer mask)
{
// name is usually _2C_ED / _2C_ES
std::string prefix = "/data/training/patient";
std::string old_string = std::to_string(num);
std::string filled_num = std::string(4 - old_string.length(), '0') + old_string;
std::string filename = prefix + filled_num + "/patient" + filled_num + name +"_gt.mhd";
//std::cout<<filename<<std::endl;
using ReaderType = itk::ImageFileReader<MaskType>;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(filename);
reader->Update();
mask = reader->GetOutput();
}