00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: VectorImage.cxx,v $ 00005 Language: C++ 00006 Date: $Date: 2005/02/08 03:51:53 $ 00007 Version: $Revision: 1.12 $ 00008 00009 Copyright (c) Insight Software Consortium. All rights reserved. 00010 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 #if defined(_MSC_VER) 00018 #pragma warning ( disable : 4786 ) 00019 #endif 00020 00021 // Software Guide : BeginLatex 00022 // 00023 // Many image processing tasks require images of non-scalar pixel type. A 00024 // typical example is an image of vectors. This is the image type required to 00025 // represent the gradient of a scalar image. The following code illustrates 00026 // how to instantiate and use an image whose pixels are of vector type. 00027 // 00028 // For convenience we use the \doxygen{Vector} class to define the pixel 00029 // type. The Vector class is intended to represent a geometrical vector in 00030 // space. It is not intended to be used as an array container like the 00031 // \href{http://www.sgi.com/tech/stl/Vector.html}{\code{std::vector}} in 00032 // \href{http://www.sgi.com/tech/stl/}{STL}. If you are interested in 00033 // containers, the \doxygen{VectorContainer} class may provide the 00034 // functionality you want. 00035 // 00036 // \index{itk::Vector} 00037 // \index{itk::Vector!header} 00038 // 00039 // 00040 // The first step is to include the header file of the Vector class. 00041 // 00042 // Software Guide : EndLatex 00043 00044 // Software Guide : BeginCodeSnippet 00045 #include "itkVector.h" 00046 // Software Guide : EndCodeSnippet 00047 00048 00049 #include "itkImage.h" 00050 00051 int main(int, char *[]) 00052 { 00053 // Software Guide : BeginLatex 00054 // 00055 // The Vector class is templated over the type used to represent 00056 // the coordinate in space and over the dimension of the space. In this example, 00057 // we want the vector dimension to match the image dimension, but this is by 00058 // no means a requirement. We could have defined a four-dimensional image 00059 // with three-dimensional vectors as pixels. 00060 // 00061 // \index{itk::Vector!Instantiation} 00062 // \index{itk::Vector!itk::Image} 00063 // \index{itk::Image!Vector pixel} 00064 // 00065 // Software Guide : EndLatex 00066 00067 // Software Guide : BeginCodeSnippet 00068 typedef itk::Vector< float, 3 > PixelType; 00069 typedef itk::Image< PixelType, 3 > ImageType; 00070 // Software Guide : EndCodeSnippet 00071 00072 // Then the image object can be created 00073 ImageType::Pointer image = ImageType::New(); 00074 00075 // The image region should be initialized 00076 ImageType::IndexType start; 00077 ImageType::SizeType size; 00078 00079 size[0] = 200; // size along X 00080 size[1] = 200; // size along Y 00081 size[2] = 200; // size along Z 00082 00083 start[0] = 0; // first index on X 00084 start[1] = 0; // first index on Y 00085 start[2] = 0; // first index on Z 00086 00087 ImageType::RegionType region; 00088 region.SetSize( size ); 00089 region.SetIndex( start ); 00090 00091 // Pixel data is allocated 00092 image->SetRegions( region ); 00093 image->Allocate(); 00094 00095 // The image buffer is initialized to a particular value 00096 ImageType::PixelType initialValue; 00097 00098 // A vector can initialize all its components to the 00099 // same value by using the Fill() method. 00100 initialValue.Fill( 0.0 ); 00101 00102 // Now the image buffer can be initialized with this 00103 // vector value. 00104 image->FillBuffer( initialValue ); 00105 00106 ImageType::IndexType pixelIndex; 00107 00108 pixelIndex[0] = 27; // x position 00109 pixelIndex[1] = 29; // y position 00110 pixelIndex[2] = 37; // z position 00111 00112 00113 // Software Guide : BeginLatex 00114 // 00115 // The Vector class inherits the operator \code{[]} from the 00116 // \doxygen{FixedArray} class. This makes it possible to access the 00117 // Vector's components using index notation. 00118 // 00119 // Software Guide : EndLatex 00120 00121 // Software Guide : BeginCodeSnippet 00122 ImageType::PixelType pixelValue; 00123 00124 pixelValue[0] = 1.345; // x component 00125 pixelValue[1] = 6.841; // y component 00126 pixelValue[2] = 3.295; // x component 00127 // Software Guide : EndCodeSnippet 00128 00129 00130 // Software Guide : BeginLatex 00131 // 00132 // We can now store this vector in one of the image pixels by defining an 00133 // index and invoking the \code{SetPixel()} method. 00134 // 00135 // Software Guide : EndLatex 00136 00137 // Software Guide : BeginCodeSnippet 00138 image->SetPixel( pixelIndex, pixelValue ); 00139 // Software Guide : EndCodeSnippet 00140 00141 00142 // The GetPixel method can also be used to read Vectors 00143 // pixels from the image 00144 ImageType::PixelType value = image->GetPixel( pixelIndex ); 00145 00146 00147 // Lets repeat that both \code{SetPixel()} and \code{GetPixel()} are 00148 // inefficient and should only be used for debugging purposes or for 00149 // implementing interactions with a graphical user interface such as 00150 // querying pixel value by clicking with the mouse. 00151 00152 return 0; 00153 } 00154