[Insight-users] Reading portion of image file

Matthias Schneider schneider at vision.ee.ethz.ch
Thu Mar 8 10:31:28 EST 2012


Hi.

I'm trying to extract subregions of 2D image files. Ideally, I'd like to 
read the required data only.
In the ITK doc I came across the comment in
itk::ImageFileReader::EnlargeOutputRequestedRegion() saying that:
"ImageFileReader cannot currently read a portion of an image (since the 
ImageIO objects cannot read a portion of an image)"

I'm not quite sure if this still holds. After a quick experiment, I was 
pretty much confident that the readers are in fact able to read portions:

const std::string fname = "/path/to/my/image.nii"; // [2048x2048 px]
typedef itk::Image<Pixel8uType, 2> ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;

ImageType::RegionType roi;
roi.SetSize(0, 512);
roi.SetSize(1, 512);

for (int i = 0; i < N; ++i) {
   std::cout << i << std::endl;
   ReaderType::Pointer reader = ReaderType::New();
   reader->SetFileName(fname);
   reader->GetOutput()->SetRequestedRegion(roi);
   reader->Update();
}

At first glance, this works perfectly fine (I checked the buffered data 
chunk) and runs much faster than reading the entire slice. Choosing N 
very large (say 1024), the program crashes since the reader seems to not 
close the file descriptors properly. I get the following error:

HDF5-DIAG: Error detected in HDF5 (1.8.7) thread 0:
   #000: [...]/ITK/Modules/ThirdParty/HDF5/src/itkhdf5/src/H5F.c line 
794 in H5Fis_hdf5(): unable to open file
     major: Low-level I/O
     minor: Unable to initialize object
   #001: [...]/ITK/Modules/ThirdParty/HDF5/src/itkhdf5/src/H5FD.c line 
1086 in H5FD_open(): open failed
     major: Virtual File Layer
     minor: Unable to initialize object
   #002: [...]/ITK/Modules/ThirdParty/HDF5/src/itkhdf5/src/H5FDsec2.c 
line 348 in H5FD_sec2_open(): unable to open file: name = 
'/path/to/my/image.nii', errno = 24, error message = 'Too many open 
files', flags = 0, o_flags = 0
     major: File accessability
     minor: Unable to open file
terminate called after throwing an instance of 
'itk::ImageFileReaderException'
   what(): 
[...]/ITK/Modules/IO/ImageBase/include/itkImageFileReader.hxx:143:
  Could not create IO object for file /path/to/my/image.nii
The file couldn't be opened for reading.
Filename: /path/to/my/image.nii


I also tried to explicitly use the ExtractImageFilter:

[... same as above ...]
typedef itk::ExtractImageFilter<ImageType, ImageType> ExtractFilterType;

for (int i = 0; i < N; ++i) {
   std::cout << i << std::endl;
   ReaderType::Pointer reader = ReaderType::New();
   reader->SetFileName(fname);
   // reader->Update();

   ExtractFilterType::Pointer extractor =  ExtractFilterType::New();
   extractor->SetInput(reader->GetOutput());
   extractor->SetExtractionRegion(roi);
   extractor->InPlaceOn(true);
   extractor->Update();
}

Without the reader->Update() the program still crashes when choosing N 
sufficiently large (~1024). Updating the reader first of course reads 
the entire slice (what I really want to avoid).
So does this mean that the comment in the manual still holds and the 
ImageIO is not able to correctly handle this case even though the 
buffered data chunks are correctly imported?! This confuses me a bit or 
maybe I'm missing something else?!

I'm happy about any comments or suggestions.

Thanks,
Matthias


More information about the Insight-users mailing list