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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*=========================================================================
*
* 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.
*
*=========================================================================*/
/**
*
* This program illustrates the use of Adaptors and
* Accessors
*
* The example shows how an Adaptor can be used to
* get acces only to thered component of an RGBPixel image
* giving the appearance of being just a 'float' image
*
* That will allow to pass the red component of this
* image as input or output to any filter that expects
* a float image
*
*/
#include "itkImageAdaptor.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkRedPixelAccessor.h"
//-------------------------------------
// Typedefs for convenience
//-------------------------------------
typedef itk::Image< itk::RGBPixel<float>, 2 > myImageType;
typedef itk::RedPixelAccessor<float> myRedAccessorType;
typedef itk::ImageAdaptor< myImageType, myRedAccessorType > myRedAdaptorType;
typedef itk::ImageRegionIteratorWithIndex< myImageType > myIteratorType;
typedef itk::ImageRegionIteratorWithIndex< myRedAdaptorType > myRedIteratorType;
//-------------------------
//
// Main code
//
//-------------------------
int itkImageAdaptorTest(int, char* []) {
myImageType::SizeType size;
size[0] = 2;
size[1] = 2;
myImageType::IndexType index;
index[0] = 0;
index[1] = 0;
myImageType::RegionType region;
region.SetIndex( index );
region.SetSize( size );
myImageType::Pointer myImage = myImageType::New();
myImage->SetLargestPossibleRegion( region );
myImage->SetBufferedRegion( region );
myImage->SetRequestedRegion( region );
myImage->Allocate();
myIteratorType it1( myImage, myImage->GetRequestedRegion() );
// Value to initialize the pixels
myImageType::PixelType::ComponentType colorInit[3] = {1.0f, 0.5f, 0.5f};
myImageType::PixelType color = colorInit;
// Initializing all the pixel in the image
it1.GoToBegin();
while( !it1.IsAtEnd() )
{
it1.Set(color);
++it1;
}
// Reading the values to verify the image content
std::cout << "--- Before --- " << std::endl;
it1.GoToBegin();
while( !it1.IsAtEnd() )
{
const myImageType::PixelType c( it1.Get() );
std::cout << c.GetRed() << " ";
std::cout << c.GetGreen() << " ";
std::cout << c.GetBlue() << std::endl;
++it1;
}
myRedAdaptorType::Pointer myAdaptor = myRedAdaptorType::New();
myAdaptor->SetImage( myImage );
myRedIteratorType it2( myAdaptor, myAdaptor->GetRequestedRegion() );
// Set the values of the Red component of myImage, using myAdaptor
it2.GoToBegin();
while( !it2.IsAtEnd() )
{
it2.Set( 0.4 );
++it2;
}
std::cout << "--- After --- " << std::endl;
it1.GoToBegin();
while( !it1.IsAtEnd() )
{
const myImageType::PixelType c( it1.Get() );
std::cout << c.GetRed() << " ";
std::cout << c.GetGreen() << " ";
std::cout << c.GetBlue() << std::endl;
++it1;
}
// Test the Set/Get Methods of the adaptor
// First test the get methods of the adaptor
if (myImage->GetPixelContainer() != myAdaptor->GetPixelContainer())
{
std::cout << "image pixel container != adaptor pixel container: "
<< myImage->GetPixelContainer() << " != "
<< myAdaptor->GetPixelContainer()
<< std::endl;
return EXIT_FAILURE;
}
float forigin[2] = {2.0, 3.0};
myImage->SetOrigin(forigin);
if (myImage->GetOrigin() != myAdaptor->GetOrigin())
{
std::cout << "image origin != adaptor origin: "
<< myImage->GetOrigin() << " != "
<< myAdaptor->GetOrigin()
<< std::endl;
return EXIT_FAILURE;
}
double dorigin[2] = {2.0, 3.0};
myImage->SetOrigin(dorigin);
if (myImage->GetOrigin() != myAdaptor->GetOrigin())
{
std::cout << "image origin != adaptor origin: "
<< myImage->GetOrigin() << " != "
<< myAdaptor->GetOrigin()
<< std::endl;
return EXIT_FAILURE;
}
myImageType::PointType imageOrigin;
imageOrigin.Fill(10.0);
myImage->SetOrigin(imageOrigin);
if (myImage->GetOrigin() != myAdaptor->GetOrigin())
{
std::cout << "image origin != adaptor origin: "
<< myImage->GetOrigin() << " != "
<< myAdaptor->GetOrigin()
<< std::endl;
return EXIT_FAILURE;
}
float fspacing[2] = {2.0, 3.0};
myImage->SetSpacing(fspacing);
if (myImage->GetSpacing() != myAdaptor->GetSpacing())
{
std::cout << "image spacing != adaptor spacing: "
<< myImage->GetSpacing() << " != "
<< myAdaptor->GetSpacing()
<< std::endl;
return EXIT_FAILURE;
}
double dspacing[2] = {2.0, 3.0};
myImage->SetSpacing(dspacing);
if (myImage->GetSpacing() != myAdaptor->GetSpacing())
{
std::cout << "image spacing != adaptor spacing: "
<< myImage->GetSpacing() << " != "
<< myAdaptor->GetSpacing()
<< std::endl;
return EXIT_FAILURE;
}
myImageType::SpacingType imageSpacing;
imageSpacing.Fill(10.0);
myImage->SetSpacing(imageSpacing);
if (myImage->GetSpacing() != myAdaptor->GetSpacing())
{
std::cout << "image spacing != adaptor spacing: "
<< myImage->GetSpacing() << " != "
<< myAdaptor->GetSpacing()
<< std::endl;
return EXIT_FAILURE;
}
myImageType::DirectionType imageDirection;
imageDirection.SetIdentity();
imageDirection[1][1]=10.0;
myImage->SetDirection(imageDirection);
if (myImage->GetDirection() != myAdaptor->GetDirection())
{
std::cout << "image direction != adaptor direction: "
<< myImage->GetDirection() << " != "
<< myAdaptor->GetDirection()
<< std::endl;
return EXIT_FAILURE;
}
// Now test the set methods of the adaptor
forigin[0] = 20.0;
myAdaptor->SetOrigin(forigin);
if (myImage->GetOrigin() != myAdaptor->GetOrigin())
{
std::cout << "image origin != adaptor origin: "
<< myImage->GetOrigin() << " != "
<< myAdaptor->GetOrigin()
<< std::endl;
return EXIT_FAILURE;
}
dorigin[0] = 20.0;
myAdaptor->SetOrigin(dorigin);
if (myImage->GetOrigin() != myAdaptor->GetOrigin())
{
std::cout << "image origin != adaptor origin: "
<< myImage->GetOrigin() << " != "
<< myAdaptor->GetOrigin()
<< std::endl;
return EXIT_FAILURE;
}
imageOrigin.Fill(100.0);
myAdaptor->SetOrigin(imageOrigin);
if (myImage->GetOrigin() != myAdaptor->GetOrigin())
{
std::cout << "image origin != adaptor origin: "
<< myImage->GetOrigin() << " != "
<< myAdaptor->GetOrigin()
<< std::endl;
return EXIT_FAILURE;
}
fspacing[0] = 20.0;
myAdaptor->SetSpacing(fspacing);
if (myImage->GetSpacing() != myAdaptor->GetSpacing())
{
std::cout << "image spacing != adaptor spacing: "
<< myImage->GetSpacing() << " != "
<< myAdaptor->GetSpacing()
<< std::endl;
return EXIT_FAILURE;
}
dspacing[0] = 20.0;
myAdaptor->SetSpacing(dspacing);
if (myImage->GetSpacing() != myAdaptor->GetSpacing())
{
std::cout << "image spacing != adaptor spacing: "
<< myImage->GetSpacing() << " != "
<< myAdaptor->GetSpacing()
<< std::endl;
return EXIT_FAILURE;
}
imageSpacing.Fill(100.0);
myAdaptor->SetSpacing(imageSpacing);
if (myImage->GetSpacing() != myAdaptor->GetSpacing())
{
std::cout << "image spacing != adaptor spacing: "
<< myImage->GetSpacing() << " != "
<< myAdaptor->GetSpacing()
<< std::endl;
return EXIT_FAILURE;
}
imageDirection[1][1]=100.0;
myAdaptor->SetDirection(imageDirection);
if (myImage->GetDirection() != myAdaptor->GetDirection())
{
std::cout << "image direction != adaptor direction: "
<< myImage->GetDirection() << " != "
<< myAdaptor->GetDirection()
<< std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|