1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
// Software Guide : BeginCommandLineArgs
// INPUTS: {FatMRISlice.png}
// OUTPUTS: {ImageRegionIteratorOutput.png}
// ARGUMENTS: 20 70 210 140
// Software Guide : EndCommandLineArgs
// Software Guide : BeginLatex
//
// \index{Iterators!speed}
// The \doxygen{ImageRegionIterator} is optimized for
// iteration speed and is the first choice for iterative, pixel-wise operations
// when location in the image is not
// important. ImageRegionIterator is the least specialized of the ITK
// image iterator classes. It implements all of the methods described in the
// preceding section.
//
// The following example illustrates the use of
// \doxygen{ImageRegionConstIterator} and ImageRegionIterator.
// Most of the code constructs introduced apply to other ITK iterators as
// well. This simple application crops a subregion from an image by copying
// its pixel values into to a second, smaller image.
//
// \index{Iterators!and image regions}
// \index{itk::ImageRegionIterator!example of using|(}
// We begin by including the appropriate header files.
//
// Software Guide : EndLatex
#include "itkImage.h"
// Software Guide : BeginCodeSnippet
#include "itkImageRegionIterator.h"
// Software Guide : EndCodeSnippet
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
int main( int argc, char *argv[] )
{
// Verify the number of parameters on the command line.
if ( argc < 7 )
{
std::cerr << "Missing parameters. " << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << argv[0]
<< " inputImageFile outputImageFile startX startY sizeX sizeY"
<< std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// Next we define a pixel type and corresponding image type. ITK iterator
// classes expect the image type as their template parameter.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const unsigned int Dimension = 2;
typedef unsigned char PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageRegionConstIterator< ImageType > ConstIteratorType;
typedef itk::ImageRegionIterator< ImageType> IteratorType;
// Software Guide : EndCodeSnippet
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
// Software Guide : BeginLatex
//
// Information about the subregion to copy is read from the command line. The
// subregion is defined by an \doxygen{ImageRegion} object, with a starting
// grid index and a size (Section~\ref{sec:ImageSection}).
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::RegionType inputRegion;
ImageType::RegionType::IndexType inputStart;
ImageType::RegionType::SizeType size;
inputStart[0] = ::atoi( argv[3] );
inputStart[1] = ::atoi( argv[4] );
size[0] = ::atoi( argv[5] );
size[1] = ::atoi( argv[6] );
inputRegion.SetSize( size );
inputRegion.SetIndex( inputStart );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The destination region in the output image is defined using the input region
// size, but a different start index. The starting index for the destination
// region is the corner of the newly generated image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::RegionType outputRegion;
ImageType::RegionType::IndexType outputStart;
outputStart[0] = 0;
outputStart[1] = 0;
outputRegion.SetSize( size );
outputRegion.SetIndex( outputStart );
// Software Guide : EndCodeSnippet
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
try
{
reader->Update();
}
catch ( itk::ExceptionObject &err)
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
// Check that the region is contained within the input image.
if ( ! reader->GetOutput()->GetRequestedRegion().IsInside( inputRegion ) )
{
std::cerr << "Error" << std::endl;
std::cerr << "The region " << inputRegion << "is not contained within the input image region "
<< reader->GetOutput()->GetRequestedRegion() << std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// After reading the input image and checking that the desired subregion is,
// in fact, contained in the input, we allocate an output image. It is
// fundamental to set valid values to some of the basic image information
// during the copying process.
// In particular, the starting index of the output region
// is now filled up with zero values and the coordinates of the physical
// origin are computed as a shift from the origin of the input image. This is
// quite important since it will allow us to later
// register the extracted region against the original image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::Pointer outputImage = ImageType::New();
outputImage->SetRegions( outputRegion );
const ImageType::SpacingType& spacing = reader->GetOutput()->GetSpacing();
const ImageType::PointType& inputOrigin = reader->GetOutput()->GetOrigin();
double outputOrigin[ Dimension ];
for(unsigned int i=0; i< Dimension; i++)
{
outputOrigin[i] = inputOrigin[i] + spacing[i] * inputStart[i];
}
outputImage->SetSpacing( spacing );
outputImage->SetOrigin( outputOrigin );
outputImage->Allocate();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// \index{Iterators!construction of} \index{Iterators!and image regions}
// The necessary images and region definitions are now in place. All that is
// left to do is to create the iterators and perform the copy. Note that image
// iterators are not accessed via smart pointers so they are light-weight
// objects that are instantiated on the stack. Also notice how the input and
// output iterators are defined over the \emph{same corresponding region}. Though the
// images are different sizes, they both contain the same target subregion.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ConstIteratorType inputIt( reader->GetOutput(), inputRegion );
IteratorType outputIt( outputImage, outputRegion );
inputIt.GoToBegin();
outputIt.GoToBegin();
while( !inputIt.IsAtEnd() )
{
outputIt.Set( inputIt.Get() );
++inputIt;
++outputIt;
}
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// \index{Iterators!image dimensionality}
// The \code{while} loop above is a common construct in ITK. The beauty of
// these four lines of code is that they are equally valid for one, two,
// three, or even ten dimensional data, and no knowledge of the size of the
// image is necessary. Consider the ugly alternative of ten nested
// \code{for} loops for traversing an image.
//
// Software Guide : EndLatex
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( argv[2] );
writer->SetInput( outputImage );
try
{
writer->Update();
}
catch ( itk::ExceptionObject &err)
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// Let's run this example on the image \code{FatMRISlice.png} found
// in \code{Examples/Data}. The command line arguments specify the
// input and output file names, then the $x$, $y$ origin and the $x$, $y$ size
// of the cropped subregion.
//
// \small
// \begin{verbatim}
// ImageRegionIterator FatMRISlice.png ImageRegionIteratorOutput.png 20 70 210 140
// \end{verbatim}
// \normalsize
//
// The output is the cropped subregion shown in
// Figure~\ref{fig:ImageRegionIteratorOutput}.
//
// \begin{figure}
// \centering
// \includegraphics[width=0.4\textwidth]{FatMRISlice}
// \includegraphics[width=0.3\textwidth]{ImageRegionIteratorOutput}
// \itkcaption[Copying an image subregion using ImageRegionIterator]{Cropping a
// region from an image. The original image is shown at left. The image on
// the right is the result of applying the ImageRegionIterator example code.}
// \protect\label{fig:ImageRegionIteratorOutput}
// \end{figure}
//
// \index{itk::ImageRegionIterator!example of using|)}
//
// Software Guide : EndLatex
return EXIT_SUCCESS;
}
|