Skip to content
Snippets Groups Projects
Commit 8586d6ce authored by Jordi Inglada's avatar Jordi Inglada
Browse files

Examples DataRepresentation/Image

parent 9437ebbb
No related branches found
No related tags found
No related merge requests found
......@@ -6,14 +6,14 @@ SET(TEMP ${ITK_BINARY_DIR}/Testing/Temporary)
ADD_EXECUTABLE(Image1 Image1.cxx )
TARGET_LINK_LIBRARIES(Image1 ITKCommon)
#ADD_EXECUTABLE(Image2 Image2.cxx )
#TARGET_LINK_LIBRARIES(Image2 ITKCommon ITKIO)
ADD_EXECUTABLE(Image2 Image2.cxx )
TARGET_LINK_LIBRARIES(Image2 OTBCommon ITKCommon OTBIO ITKIO)
#ADD_EXECUTABLE(Image3 Image3.cxx )
#TARGET_LINK_LIBRARIES(Image3 ITKCommon)
ADD_EXECUTABLE(Image3 Image3.cxx )
TARGET_LINK_LIBRARIES(Image3 OTBCommon ITKCommon)
#ADD_EXECUTABLE(Image4 Image4.cxx )
#TARGET_LINK_LIBRARIES(Image4 ITKCommon)
ADD_EXECUTABLE(Image4 Image4.cxx )
TARGET_LINK_LIBRARIES(Image4 OTBCommon ITKCommon)
#ADD_EXECUTABLE(Image5 Image5.cxx )
#TARGET_LINK_LIBRARIES(Image5 ITKCommon ITKIO)
......
......@@ -24,8 +24,8 @@
// class. The following is the minimal code needed to instantiate, declare
// and create the image class.
//
// \index{itk::Image!Instantiation}
// \index{itk::Image!Header}
// \index{Image!Instantiation}
// \index{Image!Header}
//
// First, the header file of the Image class must be included.
//
......@@ -45,12 +45,13 @@ int main(int, char *[])
// Then we must decide with what type to represent the pixels
// and what the dimension of the image will be. With these two
// parameters we can instantiate the image class. Here we create
// a 3D image with \code{unsigned short} pixel data.
// a 2D image, which is what we often use in remote sensing
// applications, anyway, with \code{unsigned short} pixel data.
//
// Software Guide : EndLatex
//
// Software Guide : BeginCodeSnippet
typedef itk::Image< unsigned short, 3 > ImageType;
typedef itk::Image< unsigned short, 2 > ImageType;
// Software Guide : EndCodeSnippet
......@@ -60,8 +61,8 @@ int main(int, char *[])
// from the corresponding image type and assigning the result
// to a \doxygen{SmartPointer}.
//
// \index{itk::Image!Pointer}
// \index{itk::Image!New()}
// \index{Image!Pointer}
// \index{Image!New()}
//
// Software Guide : EndLatex
//
......@@ -72,18 +73,18 @@ int main(int, char *[])
// Software Guide : BeginLatex
//
// In ITK, images exist in combination with one or more
// In OTB, images exist in combination with one or more
// \emph{regions}. A region is a subset of the image and indicates a
// portion of the image that may be processed by other classes in
// the system. One of the most common regions is the
// \emph{LargestPossibleRegion}, which defines the image in its
// entirety. Other important regions found in ITK are the
// entirety. Other important regions found in OTB are the
// \emph{BufferedRegion}, which is the portion of the image actually
// maintained in memory, and the \emph{RequestedRegion}, which is
// the region requested by a filter or other class when operating on
// the image.
//
// In ITK, manually creating an image requires that the image is
// In OTB, manually creating an image requires that the image is
// instantiated as previously shown, and that regions describing the image are
// then associated with it.
//
......@@ -102,8 +103,8 @@ int main(int, char *[])
// that is an n-dimensional array where each component is an integer
// indicating the grid coordinates of the initial pixel of the image.
//
// \index{itk::Image!Size}
// \index{itk::Image!SizeType}
// \index{Image!Size}
// \index{Image!SizeType}
//
// Software Guide : EndLatex
//
......@@ -112,7 +113,6 @@ int main(int, char *[])
start[0] = 0; // first index on X
start[1] = 0; // first index on Y
start[2] = 0; // first index on Z
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
......@@ -122,8 +122,8 @@ int main(int, char *[])
// unsigned integers indicating the extent in pixels of the image along
// every dimension.
//
// \index{itk::Image!Index}
// \index{itk::Image!IndexType}
// \index{Image!Index}
// \index{Image!IndexType}
//
// Software Guide : EndLatex
//
......@@ -132,7 +132,6 @@ int main(int, char *[])
size[0] = 200; // size along X
size[1] = 200; // size along Y
size[2] = 200; // size along Z
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
......@@ -142,8 +141,8 @@ int main(int, char *[])
// encapsulates both concepts. The region is initialized with the
// starting index and size of the image.
//
// \index{itk::Image!itk::ImageRegion}
// \index{itk::Image!RegionType}
// \index{Image!itk::ImageRegion}
// \index{Image!RegionType}
//
// Software Guide : EndLatex
......@@ -156,17 +155,18 @@ int main(int, char *[])
// Software Guide : BeginLatex
//
// Finally, the region is passed to the \code{Image} object in order to define its
// extent and origin. The \code{SetRegions} method sets the
// LargestPossibleRegion, BufferedRegion, and RequestedRegion
// simultaneously. Note that none of the operations performed to this point
// have allocated memory for the image pixel data. It is necessary to
// invoke the \code{Allocate()} method to do this. Allocate does not
// require any arguments since all the information needed for memory
// allocation has already been provided by the region.
//
// \index{itk::Image!Allocate()}
// \index{itk::Image!SetRegions()}
// Finally, the region is passed to the \code{Image} object in
// order to define its extent and origin. The \code{SetRegions}
// method sets the LargestPossibleRegion, BufferedRegion, and
// RequestedRegion simultaneously. Note that none of the operations
// performed to this point have allocated memory for the image pixel
// data. It is necessary to invoke the \code{Allocate()} method to
// do this. Allocate does not require any arguments since all the
// information needed for memory allocation has already been
// provided by the region.
//
// \index{Image!Allocate()}
// \index{Image!SetRegions()}
//
// Software Guide : EndLatex
......
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: Image2.cxx,v $
Language: C++
Date: $Date: 2005/02/08 03:51:52 $
Version: $Revision: 1.19 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkImage.h"
// Software Guide : BeginLatex
//
// The first thing required to read an image from a file is to include
// the header file of the \doxygen{otb::ImageFileReader} class.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "otbImageFileReader.h"
// Software Guide : EndCodeSnippet
int main( int , char * argv[])
{
// Software Guide : BeginLatex
//
// Then, the image type should be defined by specifying the
// type used to represent pixels and the dimensions of the image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef unsigned char PixelType;
const unsigned int Dimension = 2;
typedef itk::Image< PixelType, Dimension > ImageType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Using the image type, it is now possible to instantiate the image reader
// class. The image type is used as a template parameter to define how the
// data will be represented once it is loaded into memory. This type does
// not have to correspond exactly to the type stored in the file. However,
// a conversion based on C-style type casting is used, so the type chosen
// to represent the data on disk must be sufficient to characterize it
// accurately. Readers do not apply any transformation to the pixel data
// other than casting from the pixel type of the file to the pixel type of
// the ImageFileReader. The following illustrates a typical
// instantiation of the ImageFileReader type.
//
// \index{otb::ImageFileReader!Instantiation}
// \index{otb::Image!read}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef otb::ImageFileReader< ImageType > ReaderType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The reader type can now be used to create one reader object. A
// \doxygen{SmartPointer} (defined by the \code{::Pointer}
// notation) is used to receive the reference to the newly created
// reader. The \code{New()}
// method is invoked to create an instance of the image reader.
//
// \index{otb::ImageFileReader!New()}
// \index{otb::ImageFileReader!Pointer}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ReaderType::Pointer reader = ReaderType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The minimum information required by the reader is the filename
// of the image to be loaded in memory. This is provided through
// the \code{SetFileName()} method. The file format here is inferred
// from the filename extension. The user may also explicitly specify the
// data format explicitly using the \doxygen{ImageIO} (See
// Chapter~\ref{sec:ImagReadWrite} \pageref{sec:ImagReadWrite} for more
// information
//
// \index{otb::ImageFileReader!SetFileName()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const char * filename = argv[1];
reader->SetFileName( filename );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Reader objects are referred to as pipeline source objects; they
// respond to pipeline update requests and initiate the data flow in the
// pipeline. The pipeline update mechanism ensures that the reader only
// executes when a data request is made to the reader and the reader has
// not read any data. In the current example we explicitly invoke the
// \code{Update()} method because the output of the reader is not connected
// to other filters. In normal application the reader's output is connected
// to the input of an image filter and the update invocation on the filter
// triggers an update of the reader. The following line illustrates how an
// explicit update is invoked on the reader.
//
// \index{otb::ImageFileReader!Update()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
reader->Update();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Access to the newly read image can be gained by calling the
// \code{GetOutput()} method on the reader. This method can also be called
// before the update request is sent to the reader. The reference to the
// image will be valid even though the image will be empty until the reader
// actually executes.
//
// \index{otb::ImageFileReader!GetOutput()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::Pointer image = reader->GetOutput();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Any attempt to access image data before the reader executes will yield
// an image with no pixel data. It is likely that a program crash will
// result since the image will not have been properly initialized.
//
// Software Guide : EndLatex
return 0;
}
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: Image3.cxx,v $
Language: C++
Date: $Date: 2005/02/08 03:51:52 $
Version: $Revision: 1.19 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
// Software Guide : BeginLatex
//
// This example illustrates the use of the \code{SetPixel()} and
// \code{GetPixel()} methods. These two methods provide direct access to the
// pixel data contained in the image. Note that these two methods are
// relatively slow and should not be used in situations where
// high-performance access is required. Image iterators are the appropriate
// mechanism to efficiently access image pixel data. (See
// Chapter~\ref{sec:ImageIteratorsChapter} on page
// \pageref{sec:ImageIteratorsChapter} for information about image
// iterators.)
//
// Software Guide : EndLatex
#include "itkImage.h"
int main(int, char *[])
{
// First the image type should be declared
typedef itk::Image< unsigned short, 2 > ImageType;
// Then the image object can be created
ImageType::Pointer image = ImageType::New();
// The image region should be initialized
ImageType::IndexType start;
ImageType::SizeType size;
size[0] = 200; // size along X
size[1] = 200; // size along Y
start[0] = 0; // first index on X
start[1] = 0; // first index on Y
ImageType::RegionType region;
region.SetSize( size );
region.SetIndex( start );
// Pixel data is allocated
image->SetRegions( region );
image->Allocate();
// The image buffer is initialized to a particular value
ImageType::PixelType initialValue = 0;
image->FillBuffer( initialValue );
// Software Guide : BeginLatex
//
// The individual position of a pixel inside the image is identified by a
// unique index. An index is an array of integers that defines the position
// of the pixel along each coordinate dimension of the image. The IndexType
// is automatically defined by the image and can be accessed using the
// scope operator like \doxygen{Index}. The length of the array will match
// the dimensions of the associated image.
//
// The following code illustrates the declaration of an index variable and
// the assignment of values to each of its components. Please note that
// \code{Index} does not use SmartPointers to access it. This is because
// \code{Index} is a light-weight object that is not intended to be shared
// between objects. It is more efficient to produce multiple copies of
// these small objects than to share them using the SmartPointer
// mechanism.
//
// The following lines declare an instance of the index type and initialize
// its content in order to associate it with a pixel position in the image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::IndexType pixelIndex;
pixelIndex[0] = 27; // x position
pixelIndex[1] = 29; // y position
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Having defined a pixel position with an index, it is then possible to
// access the content of the pixel in the image. The \code{GetPixel()}
// method allows us to get the value of the pixels.
//
// \index{otb::Image!GetPixel()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::PixelType pixelValue = image->GetPixel( pixelIndex );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The \code{SetPixel()} method allows us to set the value of the pixel.
//
// \index{otb::Image!SetPixel()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
image->SetPixel( pixelIndex, pixelValue+1 );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Please note that \code{GetPixel()} returns the pixel value using copy
// and not reference semantics. Hence, the method cannot be used to
// modify image data values.
//
// Remember that both \code{SetPixel()} and \code{GetPixel()} are inefficient
// and should only be used for debugging or for supporting interactions like
// querying pixel values by clicking with the mouse.
//
// Software Guide : EndLatex
return 0;
}
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: Image4.cxx,v $
Language: C++
Date: $Date: 2005/02/08 03:51:53 $
Version: $Revision: 1.30 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
// Software Guide : BeginLatex
//
// Even though OTB can be used to perform
// general image processing tasks, the primary purpose of the toolkit is the
// processing of remote sensing image data. In that respect, additional
// information about the images is considered mandatory. In particular the
// information associated with the physical spacing between pixels and the
// position of the image in space with respect to some world coordinate
// system are extremely important.
//
// Image origin and spacing are fundamental to many
// applications. Registration, for example, is performed in physical
// coordinates. Improperly defined spacing and origins will result in
// inconsistent results in such processes. Remote sensing images with no spatial
// information should not be used for image analysis,
// feature extraction, GIS input, etc. In
// other words, remote sensing images lacking spatial information are not only
// useless but also hazardous.
//
// \begin{figure} \center
// \includegraphics[width=\textwidth]{ImageOriginAndSpacing.eps}
// \itkcaption[ITK Image Geometrical Concepts]{Geometrical concepts associated
// with the OTB image.}
// \label{fig:ImageOriginAndSpacing}
// \end{figure}
//
// Figure \ref{fig:ImageOriginAndSpacing}\textbf{FIXME: update for OTB
// images}illustrates the main geometrical
// concepts associated with the \doxygen{otb::Image}. In this figure,
// circles are
// used to represent the center of pixels. The value of the pixel is assumed
// to exist as a Dirac Delta Function located at the pixel center. Pixel
// spacing is measured between the pixel centers and can be different along
// each dimension. The image origin is associated with the coordinates of the
// first pixel in the image. A \emph{pixel} is considered to be the
// rectangular region surrounding the pixel center holding the data
// value. This can be viewed as the Voronoi region of the image grid, as
// illustrated in the right side of the figure. Linear interpolation of
// image values is performed inside the Delaunay region whose corners
// are pixel centers.
//
// Software Guide : EndLatex
#include "itkImage.h"
#include "itkPoint.h"
int main(int, char *[])
{
typedef itk::Image< unsigned short, 2 > ImageType;
ImageType::Pointer image = ImageType::New();
ImageType::IndexType start;
ImageType::SizeType size;
size[0] = 200; // size along X
size[1] = 200; // size along Y
start[0] = 0; // first index on X
start[1] = 0; // first index on Y
ImageType::RegionType region;
region.SetSize( size );
region.SetIndex( start );
image->SetRegions( region );
image->Allocate();
image->FillBuffer( 0 );
// Software Guide : BeginLatex
//
// Image spacing is represented in a \code{FixedArray}
// whose size matches the dimension of the image. In order to manually set
// the spacing of the image, an array of the corresponding type must be
// created. The elements of the array should then be initialized with the
// spacing between the centers of adjacent pixels. The following code
// illustrates the methods available in the Image class for dealing with
// spacing and origin.
//
// \index{otb::Image!Spacing}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::SpacingType spacing;
// Note: measurement units (e.g., meters, feet, etc.) are defined by the application.
spacing[0] = 0.70; // spacing along X
spacing[1] = 0.70; // spacing along Y
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The array can be assigned to the image using
// the \code{SetSpacing()} method.
//
// \index{otb::Image!SetSpacing()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
image->SetSpacing( spacing );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The spacing information can be retrieved from an image by using the
// \code{GetSpacing()} method. This method returns a reference to a
// \code{FixedArray}. The returned object can then be used to read the
// contents of the array. Note the use of the \code{const} keyword to indicate
// that the array will not be modified.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const ImageType::SpacingType& sp = image->GetSpacing();
std::cout << "Spacing = ";
std::cout << sp[0] << ", " << sp[1] << std::endl;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The image origin is managed in a similar way to the spacing. A
// \code{Point} of the appropriate dimension must first be
// allocated. The coordinates of the origin can then be assigned to
// every component. These coordinates correspond to the position of
// the first pixel of the image with respect to an arbitrary
// reference system in physical space. It is the user's
// responsibility to make sure that multiple images used in the same
// application are using a consistent reference system. This is
// extremely important in image registration applications.
//
// The following code illustrates the creation and assignment of a variable
// suitable for initializing the image origin.
//
// \index{otb::Image!origin}
// \index{otb::Image!SetOrigin()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::PointType origin;
origin[0] = 0.0; // coordinates of the
origin[1] = 0.0; // first pixel in 2-D
image->SetOrigin( origin );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The origin can also be retrieved from an image by using the
// \code{GetOrigin()} method. This will return a reference to a
// \code{Point}. The reference can be used to read the contents of
// the array. Note again the use of the \code{const} keyword to indicate
// that the array contents will not be modified.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const ImageType::PointType& orgn = image->GetOrigin();
std::cout << "Origin = ";
std::cout << orgn[0] << ", " << orgn[1] << std::endl;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Once the spacing and origin of the image have been initialized, the image
// will correctly map pixel indices to and from physical space
// coordinates. The following code illustrates how a point in physical
// space can be mapped into an image index for the purpose of reading the
// content of the closest pixel.
//
// First, a \doxygen{Point} type must be declared. The point type is
// templated over the type used to represent coordinates and over the
// dimension of the space. In this particular case, the dimension of the
// point must match the dimension of the image.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::Point< double, ImageType::ImageDimension > PointType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The Point class, like an \doxygen{itk::Index}, is a relatively small and
// simple object. For this reason, it is not reference-counted like the
// large data objects in OTB. Consequently, it is also not manipulated
// with \doxygen{itk::SmartPointer}s. Point objects are simply declared as
// instances of any other C++ class. Once the point is declared, its
// components can be accessed using traditional array notation. In
// particular, the \code{[]} operator is available. For efficiency reasons,
// no bounds checking is performed on the index used to access a particular
// point component. It is the user's responsibility to make sure that the
// index is in the range $\{0,Dimension-1\}$.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
PointType point;
point[0] = 1.45; // x coordinate
point[1] = 7.21; // y coordinate
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The image will map the point to an index using the values of the
// current spacing and origin. An index object must be provided to
// receive the results of the mapping. The index object can be
// instantiated by using the \code{IndexType} defined in the Image
// type.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::IndexType pixelIndex;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The \code{TransformPhysicalPointToIndex()} method of the image class
// will compute the pixel index closest to the point provided. The method
// checks for this index to be contained inside the current buffered pixel
// data. The method returns a boolean indicating whether the resulting
// index falls inside the buffered region or not. The output index should
// not be used when the returned value of the method is \code{false}.
//
// The following lines illustrate the point to index mapping and the
// subsequent use of the pixel index for accessing pixel data from the
// image.
//
// \index{otb::Image!TransformPhysicalPointToIndex()}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
bool isInside = image->TransformPhysicalPointToIndex( point, pixelIndex );
if ( isInside )
{
ImageType::PixelType pixelValue = image->GetPixel( pixelIndex );
pixelValue += 5;
image->SetPixel( pixelIndex, pixelValue );
}
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Remember that \code{GetPixel()} and \code{SetPixel()} are very
// inefficient methods for accessing pixel data. Image iterators should be
// used when massive access to pixel data is required.
//
// Software Guide : EndLatex
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment