Skip to content

Instantly share code, notes, and snippets.

@hinerm
Created October 16, 2012 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hinerm/3900888 to your computer and use it in GitHub Desktop.
Save hinerm/3900888 to your computer and use it in GitHub Desktop.
bf-itk-pipe time splitting example
/*
* #%L
* Bio-Formats plugin for the Insight Toolkit.
* %%
* Copyright (C) 2010 - 2012 Insight Software Consortium, and Open Microscopy
* Environment:
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* - University of Dundee
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of any organization.
*
* ----------------------------------------------------------------
* Adapted from the Slicer3 project: http://www.slicer.org/
* http://viewvc.slicer.org/viewcvs.cgi/trunk/Libs/MGHImageIO/
*
* See slicer-license.txt for Slicer3's licensing information.
*
* For more information about the ITK Plugin IO mechanism, see:
* http://www.itk.org/Wiki/Plugin_IO_mechanisms
* #L%
*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include <iostream>
#include "itkBioFormatsImageIO.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
#include "itkRGBPixel.h"
#include "itkMetaDataObject.h"
#include "itkStreamingImageFilter.h"
#include "itkRegionOfInterestImageFilter.h"
#include <itkImageIORegion.h>
#if defined(ITK_USE_MODULAR_BUILD)
#define SPECIFIC_IMAGEIO_MODULE_TEST
#endif
int main( int argc, char * argv [] )
{
if( argc < 2)
{
std::cerr << "Usage: " << argv[0] << " input";
return EXIT_FAILURE;
}
typedef unsigned char PixelType;
const unsigned int Dimension = 5;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageFileReader<ImageType> ReaderType;
typedef itk::ImageIORegion ImageIORegionType;
typedef itk::StreamingImageFilter<ImageType, ImageType> StreamingFilter;
typedef itk::ImageFileWriter<ImageType> WriterType;
typedef itk::RegionOfInterestImageFilter<ImageType, ImageType> FilterType;
itk::BioFormatsImageIO::Pointer io = itk::BioFormatsImageIO::New();
io->DebugOn();
std::string inputFilename = argv[1];
io->SetFileName(argv[1]);
io->ReadImageInformation();
unsigned int numberOfDim = io->GetNumberOfDimensions();
ImageIORegionType region(5);
for( unsigned long i = 0; i < numberOfDim; i++ )
{
std::cout << "Setting index: " << i << " to: " << io->GetDimensions(i) << std::endl;
region.SetIndex( i, 0 );
region.SetSize( i, io->GetDimensions(i) );
}
for( unsigned int i = 0; i < io->GetDimensions( numberOfDim-2 ); i++ )
{
std::cout << i << std::endl;
region.SetIndex( numberOfDim-2, i);
region.SetSize( numberOfDim-2, 1);
io->SetIORegion( region );
std::cout << "IO Region:\n" <<
io->GetIORegion() << std::endl;
std::cout << "Updating reader" << std::endl;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
reader->SetImageIO(io);
reader->Update();
std::cout << "Reader Output Largest Possible Region:\n" <<
reader->GetOutput()->GetLargestPossibleRegion().GetSize() << std::endl;
/*
ImageType::SizeType inSize = reader->GetOutput()->GetLargestPossibleRegion().GetSize();
std::cout << "Got largest input size: " << inSize << std::endl;
FilterType::Pointer filter = FilterType::New();
ImageType::IndexType start;
ImageType::SizeType size;
std::cout << "Setting up filter start, index" << std::endl;
for( unsigned int j = 0; j < 5; j++ )
{
start[j] = 0;
size[j] = inSize[j];
}
start[ numberOfDim-2 ] = i;
size[ numberOfDim-2 ] = 1;
std::cout << "Setting ROI" << std::endl;
ImageType::RegionType desiredRegion;
desiredRegion.SetSize(size);
desiredRegion.SetIndex(start);
std::cout << "Applying ROI to Reader output" << std::endl;
filter->SetRegionOfInterest(desiredRegion);
filter->SetInput(reader->GetOutput());
*/
io->SetIORegion( region );
std::cout << "IO Region:\n" <<
io->GetIORegion() << std::endl;
std::ostringstream istream;
istream << i << ".ome.tiff";
std::string fname = istream.str();
std::cout << "Updating Writer" << std::endl;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( fname.c_str() );
writer->SetImageIO(io);
writer->SetInput(reader->GetOutput());
writer->SetIORegion(region);
io->SetIORegion( region );
std::cout << "Writer IO Region:\n" <<
writer->GetIORegion() << std::endl;
try
{
writer->Write();
}
catch (itk::ExceptionObject &e)
{
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment