Question about ImageType::Pointer

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

Change the argument of that function to ImageType::Pointer image, as you did for ReadMask.
Inside that function image = reader->GetOutput(), image is a raw pointer, not a smart pointer with a reference counter. So, when the reader goes out of scope, the image is destroyed.

1 Like

Hello Pablo, thank you for your reply! I have changed the argument of the function to ImageType::Pointer image but the result is still the same. Does it has sth to do with the c++ version?

My fault, use a reference to the image pointer: ImageType::Pointer & image. With a copy, you have the same problem with image and reader going out of scope.

1 Like

Thank you! It works! :blush:

2 Likes