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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
/*=========================================================================
*
* 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 : BeginLatex
//
// This example illustrates the use of the
// \doxygen{SpatialObjectToImageFilter}. This filter expect a
// \doxygen{SpatialObject} as input, and rasterize it in order to generate an
// output image. This is particularly useful for generating synthetic images,
// in particular binary images containing a mask.
//
// \index{itk::SpatialObjectToImageFilter|textbf}
//
// Software Guide : EndLatex
// Software Guide : BeginLatex
//
// The first step required for using this filter is to include its header file
//
// \index{itk::SpatialObjectToImageFilter!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkSpatialObjectToImageFilter.h"
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// This filter takes as input a SpatialObject. However, SpatialObject can be
// grouped together in a hierarchical structure in order to produce more
// complex shapes. In this case, we illustrate how to aggregate multiple basic
// shapes. We should, therefore, include the headers of the individual elementary
// SpatialObjects.
//
// \index{itk::EllipseSpatialObject!header}
// \index{itk::CylinderSpatialObject!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkEllipseSpatialObject.h"
#include "itkCylinderSpatialObject.h"
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Then we include the header of the \doxygen{GroupSpatialObject} that will
// group together these instances of SpatialObjects.
//
// \index{itk::GroupSpatialObject!header}
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
#include "itkGroupSpatialObject.h"
// Software Guide : EndCodeSnippet
#include "itkImageFileWriter.h"
int main( int argc, char *argv[] )
{
if( argc != 2 )
{
std::cerr << "Usage: " << argv[0] << " outputimagefile " << std::endl;
return EXIT_FAILURE;
}
// Software Guide : BeginLatex
//
// We declare the pixel type and dimension of the image to be produced as
// output.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef signed short PixelType;
const unsigned int Dimension = 3;
typedef itk::Image< PixelType, Dimension > ImageType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Using the same dimension, we instantiate the types of the elementary
// SpatialObjects that we plan to group, and we instantiate as well the type
// of the SpatialObject that will hold the group together.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::EllipseSpatialObject< Dimension > EllipseType;
typedef itk::CylinderSpatialObject CylinderType;
typedef itk::GroupSpatialObject< Dimension > GroupType;
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We instantiate the SpatialObjectToImageFilter type by using as template
// arguments the input SpatialObject and the output image types.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::SpatialObjectToImageFilter<
GroupType, ImageType > SpatialObjectToImageFilterType;
SpatialObjectToImageFilterType::Pointer imageFilter =
SpatialObjectToImageFilterType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The SpatialObjectToImageFilter requires that the user defines the grid
// parameters of the output image. This includes the number of pixels along
// each dimension, the pixel spacing, image direction and
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ImageType::SizeType size;
size[ 0 ] = 50;
size[ 1 ] = 50;
size[ 2 ] = 150;
imageFilter->SetSize( size );
// Software Guide : EndCodeSnippet
// Software Guide : BeginCodeSnippet
ImageType::SpacingType spacing;
spacing[0] = 100.0 / size[0];
spacing[1] = 100.0 / size[1];
spacing[2] = 300.0 / size[2];
imageFilter->SetSpacing( spacing );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// We create the elementary shapes that are going to be composed into the
// group spatial objects.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
EllipseType::Pointer ellipse = EllipseType::New();
CylinderType::Pointer cylinder1 = CylinderType::New();
CylinderType::Pointer cylinder2 = CylinderType::New();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The Elementary shapes have internal parameters of their own. These
// parameters define the geometrical characteristics of the basic shapes.
// For example, a cylinder is defined by its radius and height.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
ellipse->SetRadius( size[0] * 0.2 * spacing[0] );
cylinder1->SetRadius( size[0] * 0.2 * spacing[0] );
cylinder2->SetRadius( size[0] * 0.2 * spacing[0] );
cylinder1->SetHeight( size[2] * 0.30 * spacing[2]);
cylinder2->SetHeight( size[2] * 0.30 * spacing[2]);
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Each one of these components will be placed in a different position and
// orientation. We define transforms in order to specify those relative
// positions and orientations.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef GroupType::TransformType TransformType;
TransformType::Pointer transform1 = TransformType::New();
TransformType::Pointer transform2 = TransformType::New();
TransformType::Pointer transform3 = TransformType::New();
transform1->SetIdentity();
transform2->SetIdentity();
transform3->SetIdentity();
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Then we set the specific values of the transform parameters, and we
// assign the transforms to the elementary shapes.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
TransformType::OutputVectorType translation;
TransformType::CenterType center;
translation[ 0 ] = size[0] * spacing[0] / 2.0;
translation[ 1 ] = size[1] * spacing[1] / 4.0;
translation[ 2 ] = size[2] * spacing[2] / 2.0;
transform1->Translate( translation, false );
translation[ 1 ] = size[1] * spacing[1] / 2.0;
translation[ 2 ] = size[2] * spacing[2] * 0.22;
transform2->Rotate( 1, 2, itk::Math::pi / 2.0 );
transform2->Translate( translation, false );
translation[ 2 ] = size[2] * spacing[2] * 0.78;
transform3->Rotate( 1, 2, itk::Math::pi / 2.0 );
transform3->Translate( translation, false );
ellipse->SetObjectToParentTransform( transform1 );
cylinder1->SetObjectToParentTransform( transform2 );
cylinder2->SetObjectToParentTransform( transform3 );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// The elementary shapes are aggregated in a parent group, that in turn is
// passed as input to the filter.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
GroupType::Pointer group = GroupType::New();
group->AddSpatialObject( ellipse );
group->AddSpatialObject( cylinder1 );
group->AddSpatialObject( cylinder2 );
imageFilter->SetInput( group );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// By default, the filter will rasterize the aggregation of elementary
// shapes and will assign a pixel value to locations that fall inside of any
// of the elementary shapes, and a different pixel value to locations that
// fall outside of all of the elementary shapes. It is possible, however, to
// generate richer images if we allow the filter to use the values that the
// elementary spatial objects return via their \code{ValueAt} methods. This
// is what we choose to do in this example, by using the following code.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
const PixelType airHounsfieldUnits = -1000;
const PixelType boneHounsfieldUnits = 800;
ellipse->SetDefaultInsideValue( boneHounsfieldUnits );
cylinder1->SetDefaultInsideValue( boneHounsfieldUnits );
cylinder2->SetDefaultInsideValue( boneHounsfieldUnits );
ellipse->SetDefaultOutsideValue( airHounsfieldUnits );
cylinder1->SetDefaultOutsideValue( airHounsfieldUnits );
cylinder2->SetDefaultOutsideValue( airHounsfieldUnits );
imageFilter->SetUseObjectValue( true );
imageFilter->SetOutsideValue( airHounsfieldUnits );
// Software Guide : EndCodeSnippet
// Software Guide : BeginLatex
//
// Finally we are ready to run the filter. We use the typical invocation of
// the \code{Update} method, and we instantiate an \code{ImageFileWriter} in
// order to save the generated image into a file.
//
// Software Guide : EndLatex
// Software Guide : BeginCodeSnippet
typedef itk::ImageFileWriter< ImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( argv[1] );
writer->SetInput( imageFilter->GetOutput() );
try
{
imageFilter->Update();
writer->Update();
}
catch( itk::ExceptionObject & excp )
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
// Software Guide : EndCodeSnippet
return EXIT_SUCCESS;
}
|