[ITK-dev] Fwd: Streamed Read/Write Truncating Data
Maryana
nexnaru at gmail.com
Mon Sep 21 18:32:08 EDT 2015
Hi!
If anyone would like to repeat the test, this is my test file:
https://www.dropbox.com/s/7hpen46znk1h58l/brain_8bit.nii?dl=0
If I convert that file to MetaIO without streams, and
use ExtractImageFilter to read one file perpendicular to Y (as in the code
I'm sending bellow) it works fine. However if I convert it to MetaIO using
streams and use the same code to read the same slice, I end up with a
truncated image. I have the same result when I use Paraview for
visualization. By the way, for what I've read from ITK documentation NIfti
supposedly supports streamed reading. So I'm guessing that using Nifti in
this case would be ok. Is this correct? Would anyone have an idea on
what's wrong?
Thank you!
Code for reading slice:
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkExtractImageFilter.h"
#include "itkImage.h"
int main( int argc, char ** argv )
{
typedef unsigned char InputPixelType;
typedef unsigned char OutputPixelType;
typedef itk::Image< InputPixelType, 3 > InputImageType;
typedef itk::Image< OutputPixelType, 2 > OutputImageType;
typedef itk::ImageFileReader< InputImageType > ReaderType;
typedef itk::ImageFileWriter< OutputImageType > WriterType;
std::string fileIn = "mri2_stream.mha";
std::string fileOut = "slice.tif";
const char * inputFilename = fileIn.c_str();
const char * outputFilename = fileOut.c_str();
ReaderType::Pointer reader = ReaderType::New();
WriterType::Pointer writer = WriterType::New();
reader->SetFileName( inputFilename );
writer->SetFileName( outputFilename );
typedef itk::ExtractImageFilter< InputImageType, OutputImageType >
FilterType;
FilterType::Pointer filter = FilterType::New();
filter->InPlaceOn();
filter->SetDirectionCollapseToSubmatrix();
reader->UpdateOutputInformation();
InputImageType::RegionType inputRegion =
reader->GetOutput()->GetLargestPossibleRegion();
InputImageType::SizeType size = inputRegion.GetSize();
std::cout << "Size: " << std::endl;
std::cout << size[0] << std::endl;
std::cout << size[1] << std::endl;
std::cout << size[2] << std::endl;
InputImageType::IndexType start = inputRegion.GetIndex();
const unsigned int sliceNumber = 800;
std::cout << "Start: " << std::endl;
std::cout << start[0] << std::endl;
std::cout << start[1] << std::endl;
std::cout << start[2] << std::endl;
size[1] = 0;
start[1] = sliceNumber;
InputImageType::RegionType desiredRegion;
desiredRegion.SetSize( size );
desiredRegion.SetIndex( start );
filter->SetExtractionRegion( desiredRegion );
filter->SetInput( reader->GetOutput() );
writer->SetInput( filter->GetOutput() );
try
{
writer->Update();
}
catch( itk::ExceptionObject & err )
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
On Mon, Sep 21, 2015 at 12:07 PM, Maryana <nexnaru at gmail.com> wrote:
> Hi,
>
> I use Linux Mint 64bits, GCC 4.8, Cmake 2.8. I'm compiling to this
> platform.
>
> Thank you!
>
> On Mon, Sep 21, 2015 at 11:55 AM, Bradley Lowekamp <blowekamp at mail.nih.gov
> > wrote:
>
>> Hello Maryana,
>>
>> What compiler, platform and architecture are you compiling for?
>>
>> Brad
>>
>> On Sep 21, 2015, at 2:54 PM, Maryana <nexnaru at gmail.com> wrote:
>>
>> >
>> > Hi ITK Users/Devs,
>> >
>> > I'm new to ITK and I need to write a small program to convert Nifti
>> images to MetaIO. However, I need to use the streamed reader/writer because
>> my image volumes can have several gigabytes. I was doing some tests with
>> the following code but I've realized that whenever I use the stream I end
>> up with a truncated volume. I'm using ITK 4.8. I'm sure my volume is of
>> unsigned char type. Also, when I comment out the
>> writer->SetNumberOfStreamDivisions(20); line I works fine. Does anyone
>> knows what is wrong?
>> >
>> > Thank you!
>> >
>> > #include <cstdlib>
>> > #include <string>
>> > #include <iostream>
>> >
>> > #include "itkImage.h"
>> > #include "itkImageFileReader.h"
>> > #include "itkImageFileWriter.h"
>> > #include "itkImageRegionIterator.h"
>> > #include "itkNiftiImageIO.h"
>> > #include "itkMetaImageIO.h"
>> >
>> >
>> > int main(int argc, char const *argv[]){
>> >
>> > std::string fileIn = "brain_8bit.nii";
>> > std::string fileOut = "mri2_stream.mha";
>> >
>> > typedef itk::Image<unsigned char,3> Image3D;
>> >
>> > //Reader
>> > typedef itk::ImageFileReader<Image3D> ReaderType;
>> > ReaderType::Pointer reader = ReaderType::New();
>> > reader->SetFileName(fileIn);
>> >
>> > //Writer
>> > typedef itk::ImageFileWriter<Image3D> WriterType;
>> > WriterType::Pointer writer = WriterType::New();
>> > writer->SetFileName(fileOut);
>> > writer->SetNumberOfStreamDivisions(20);
>> > writer->SetInput(reader->GetOutput());
>> >
>> >
>> > std::cout << "Input: " << fileIn << std::endl;
>> > std::cout << "Output: " << fileOut << std::endl;
>> >
>> > std::cout << "Writing..." << std::endl;
>> > try
>> > {
>> > writer->Update();
>> > }
>> > catch( itk::ExceptionObject & err )
>> > {
>> > std::cerr << "ExceptionObject caught !" << std::endl;
>> > std::cerr << err << std::endl;
>> > return EXIT_FAILURE;
>> > }
>> > std::cout << "Finished." << std::endl;
>> >
>> >
>> > return EXIT_SUCCESS;
>> > }
>> >
>> >
>> >
>> >
>> >
>> > --
>> > "Tudo no mundo começou com um sim. Uma molécula disse sim a outra
>> molécula e nasceu a vida" - Clarice Lispector
>> > _______________________________________________
>> > Powered by www.kitware.com
>> >
>> > Visit other Kitware open-source projects at
>> > http://www.kitware.com/opensource/opensource.html
>> >
>> > Kitware offers ITK Training Courses, for more information visit:
>> > http://kitware.com/products/protraining.php
>> >
>> > Please keep messages on-topic and check the ITK FAQ at:
>> > http://www.itk.org/Wiki/ITK_FAQ
>> >
>> > Follow this link to subscribe/unsubscribe:
>> > http://public.kitware.com/mailman/listinfo/insight-developers
>>
>>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-developers/attachments/20150921/f3239b0b/attachment.html>
More information about the Insight-developers
mailing list