Created
October 16, 2012 17:55
-
-
Save hinerm/3900888 to your computer and use it in GitHub Desktop.
bf-itk-pipe time splitting example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* #%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 <fstream> | |
#include "itkBioFormatsImageIO.h" | |
#include "itkImageFileReader.h" | |
#include "itkImageFileWriter.h" | |
#include "itkImage.h" | |
#include "itkRGBPixel.h" | |
#include "itkMetaDataObject.h" | |
#include "itkExtractImageFilter.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::ImageFileWriter<ImageType> WriterType; | |
typedef itk::ExtractImageFilter<ImageType, ImageType> ExtractImageFilter; | |
// This ImageIO won't actually be used, it's just a reference to remember how many | |
// T-slices there are, as the extracted ImageIO will be truncated. | |
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(); | |
// Similarly this region will be reused to ensure when we know how big the image is when | |
// we try to extract a single T-slice | |
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) ); | |
} | |
ReaderType::Pointer reader = ReaderType::New(); | |
reader->SetFileName(argv[1]); | |
reader->UseStreamingOn(); | |
WriterType::Pointer writer = WriterType::New(); | |
for( unsigned int i = 0; i < region.GetSize( numberOfDim-2 ); i++ ) | |
{ | |
std::cout << i << std::endl; | |
// When I was persisting these objects between iterations I ran into | |
// I ran into errors based on remembering the wrong region sizes | |
itk::BioFormatsImageIO::Pointer imageIO = itk::BioFormatsImageIO::New(); | |
ExtractImageFilter::Pointer extractor = ExtractImageFilter::New(); | |
imageIO->DebugOn(); | |
imageIO->SetFileName(argv[1]); | |
imageIO->SetUseStreamedReading(true); | |
imageIO->SetUseStreamedWriting(true); | |
// reset to the largest possible region, so that the extraction region is contained | |
imageIO->SetIORegion( region ); | |
imageIO->ReadImageInformation(); | |
reader->SetImageIO(imageIO); | |
std::cout << "ImageIO Region: " << imageIO << std::endl; | |
ImageType::IndexType start; | |
ImageType::SizeType size; | |
for( unsigned int j = 0; j < 5; j++ ) | |
{ | |
start[j] = 0; | |
size[j] = io->GetDimensions(j); | |
} | |
start[ numberOfDim-2 ] = i; | |
size[ numberOfDim-2 ] = 1; | |
ImageType::RegionType desiredRegion; | |
desiredRegion.SetSize(size); | |
desiredRegion.SetIndex(start); | |
extractor->SetExtractionRegion(desiredRegion); | |
std::cout << "Region of extraction: " << extractor->GetExtractionRegion() << std::endl; | |
std::cout << "Largest possible reader region, pre-update: " << reader->GetImageIO()->GetIORegion() << std::endl; | |
extractor->SetInput(reader->GetOutput()); | |
extractor->Update(); | |
std::cout << "Largest possible reader region, post-update: " << reader->GetImageIO()->GetIORegion() << std::endl; | |
std::ostringstream istream; | |
istream << i << ".ome.tiff"; | |
std::string fname = istream.str(); | |
writer->SetFileName( fname.c_str() ); | |
writer->SetImageIO(imageIO); | |
writer->SetInput(extractor->GetOutput()); | |
try | |
{ | |
writer->Update(); | |
} | |
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