ITK  5.4.0 Insight Toolkit
/*=========================================================================
*
*
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
*=========================================================================*/
// Software Guide : BeginLatex
//
// This example illustrates the common task of extracting a 2D slice from a
// 3D volume. This is typically used for display purposes and for expediting
// user feedback in interactive programs. Here we simply read a 3D volume,
// extract one of its slices and save it as a 2D image. Note that caution
// should be used when working with 2D slices from a 3D dataset, since for
// most image processing operations, the application of a filter on an
// extracted slice is not equivalent to first applying the filter in the
// volume and then extracting the slice.
//
// In this example we start by including the appropriate header files.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The filter used to extract a region from an image is the
// \doxygen{ExtractImageFilter}. Its header is included below. This filter
// is capable of extracting $(N-1)$-dimensional images from $N$-dimensional
// ones.
//
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// Software Guide : EndCodeSnippet
#include "itkImage.h"
int
main(int argc, char ** argv)
{
// Verify the number of parameters in the command line
if (argc < 4)
{
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0] << " input3DImageFile output2DImageFile sliceNumber"
<< std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// Image types are defined below. Note that the input image type is $3D$
// and the output image type is $2D$.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
using InputPixelType = short;
using OutputPixelType = short;
using InputImageType = itk::Image<InputPixelType, 3>;
using OutputImageType = itk::Image<OutputPixelType, 2>;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The types for the \doxygen{ImageFileReader} and
// \doxygen{ImageFileWriter} are instantiated using the image types.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// Software Guide : EndCodeSnippet
// Here we recover the file names from the command line arguments
//
const char * inputFilename = argv[1];
const char * outputFilename = argv[2];
// Software Guide : BeginLatex
//
// Below, we create the reader and writer using the \code{New()} method and
// assign the result to a \doxygen{SmartPointer}.
//
// \index{itk::ImageFileWriter!New()}
// \index{itk::ImageFileWriter!SmartPointer}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
auto writer = WriterType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The name of the file to be read or written is passed with the
// \code{SetFileName()} method.
//
// \index{itk::ImageFileWriter!SetFileName()}
// \index{SetFileName()!itk::ImageFileWriter}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
writer->SetFileName(outputFilename);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The ExtractImageFilter type is instantiated using the input and
// output image types. A filter object is created with the \code{New()}
// method and assigned to a \doxygen{SmartPointer}.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
auto filter = FilterType::New();
filter->InPlaceOn();
filter->SetDirectionCollapseToSubmatrix();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The ExtractImageFilter requires a region to be defined by the
// user. The region is specified by an \doxygen{Index} indicating the
// pixel where the region starts and an \doxygen{Size} indicating how many
// pixels the region has along each dimension. In order to extract a $2D$
// image from a $3D$ data set, it is enough to set the size of the region
// to $0$ in one dimension. This will indicate to
// ExtractImageFilter that a dimensional reduction has been
// specified. Here we take the region from the largest possible region of
// the input image. Note that \code{UpdateOutputInformation()} is being
// the output image without actually reading in the bulk-data.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We take the size from the region and collapse the size in the $Z$
// component by setting its value to $0$. This will indicate to the
// ExtractImageFilter that the output image should have a
// dimension less than the input image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
InputImageType::SizeType size = inputRegion.GetSize();
size[2] = 0;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Note that in this case we are extracting a $Z$ slice, and for that
// reason, the dimension to be collapsed is the one with index $2$. You
// may keep in mind the association of index components
// $\{X=0,Y=1,Z=2\}$. If we were interested in extracting a slice
// perpendicular to the $Y$ axis we would have set \code{size[1]=0;}.
//
// Software Guide : EndLatex
// Software Guide : BeginLatex
//
// Then, we take the index from the region and set its $Z$ value to the
// slice number we want to extract. In this example we obtain the slice
// number from the command line arguments.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
InputImageType::IndexType start = inputRegion.GetIndex();
const unsigned int sliceNumber = std::stoi(argv[3]);
start[2] = sliceNumber;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally, an \doxygen{ImageRegion} object is created and initialized with
// the start and size we just prepared using the slice information.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
desiredRegion.SetSize(size);
desiredRegion.SetIndex(start);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Then the region is passed to the filter using the
// \code{SetExtractionRegion()} method.
//
// \index{itk::ExtractImageFilter!SetExtractionRegion()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
filter->SetExtractionRegion(desiredRegion);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Below we connect the reader, filter and writer to form the data
// processing pipeline.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
writer->SetInput(filter->GetOutput());
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally we execute the pipeline by invoking \code{Update()} on the
// writer. The call is placed in a \code{try/catch} block in case
// exceptions are thrown.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
try
{
writer->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
// Software Guide : EndCodeSnippet
return EXIT_SUCCESS;
}
itkExtractImageFilter.h
itk::GTest::TypedefsAndConstructors::Dimension2::SizeType
ImageBaseType::SizeType SizeType
Definition: itkGTestTypedefsAndConstructors.h:49
itkImage.h
Data source that reads image data from a single file.
itk::GTest::TypedefsAndConstructors::Dimension2::IndexType
ImageBaseType::IndexType IndexType
Definition: itkGTestTypedefsAndConstructors.h:50
itk::ImageFileWriter
Writes image data to a single file.
Definition: itkImageFileWriter.h:88
itk::Index::GetIndex
const IndexValueType * GetIndex() const
Definition: itkIndex.h:232
itk::GTest::TypedefsAndConstructors::Dimension2::RegionType
ImageBaseType::RegionType RegionType
Definition: itkGTestTypedefsAndConstructors.h:54
itkImageFileWriter.h
itk::ExtractImageFilter
Decrease the image size by cropping the image to the selected region bounds.
Definition: itkExtractImageFilter.h:118
itk::Image
Templated n-dimensional image class.
Definition: itkImage.h:88
New
static Pointer New()
itk::ImageRegion::SetSize
void SetSize(const SizeType &size)
Definition: itkImageRegion.h:174
itk::Size::GetSize
const SizeValueType * GetSize() const
Definition: itkSize.h:171