SimpleITK  
LandmarkRegistration/LandmarkRegistration.cxx
/*=========================================================================
*
* Copyright NumFOCUS
*
* 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.
*
*=========================================================================*/
#include <SimpleITK.h>
#include <iostream>
#include <vector>
namespace sitk = itk::simple;
int
main(int argc, char * argv[])
{
using sitk::operator<<;
// Black image with a small white square in it.
sitk::Image fixedImage(100, 100, sitk::sitkUInt8);
// Paste a white square (9x9 pixels with value 200) at position [11, 11]
pasteFilter.SetSourceSize({ 9, 9 });
pasteFilter.SetSourceIndex({ 0, 0 });
pasteFilter.SetDestinationIndex({ 11, 11 });
fixedImage = pasteFilter.Execute(fixedImage, 200);
// Black image with a small grey square at a different location.
sitk::Image movingImage(100, 100, sitk::sitkUInt8);
// Paste a grey square (9x9 pixels with value 69) at position [51, 51]
pasteFilter.SetDestinationIndex({ 51, 51 });
movingImage = pasteFilter.Execute(movingImage, 69);
// Landmarks are 3 corners of the squares.
// 3 (X, Y) pairs are flattened into 1-d lists.
std::vector<double> fixedLandmarks = { 10, 10, 20, 10, 20, 20 };
std::vector<double> movingLandmarks = { 50, 50, 60, 50, 60, 60 };
// Set up the LandmarkBasedTransformInitializerFilter.
landmarkInitializer.SetFixedLandmarks(fixedLandmarks);
landmarkInitializer.SetMovingLandmarks(movingLandmarks);
// Compute the transform.
sitk::Transform outputTransform = landmarkInitializer.Execute(transform);
std::cout << outputTransform.ToString() << std::endl;
// Resample the transformed moving image onto the fixed image.
sitk::Image outputImage = sitk::Resample(movingImage, fixedImage, outputTransform, sitk::sitkLinear, 150);
// Write the resampled image.
sitk::WriteImage(outputImage, "landmark_example.png");
// Write the transform.
std::string outName;
if (argc > 1)
{
outName = argv[1];
}
else
{
outName = "landmark_transform.tfm";
}
sitk::WriteTransform(outputTransform, outName);
return 0;
}
A rigid 2D transform with rotation in radians around a fixed center with translation.
The Image class for SimpleITK.
Definition sitkImage.h:77
Transform Execute(const Transform &transform)
Paste an image (or a constant value) into another image.
void SetSourceSize(std::vector< unsigned int > SourceSize)
void SetDestinationIndex(std::vector< int > DestinationIndex)
Image Execute(Image &&destinationImage, const Image &sourceImage)
void SetSourceIndex(std::vector< int > SourceIndex)
A simplified wrapper around a variety of ITK transforms.
std::string ToString() const
@ sitkLinear
N-D linear interpolation.
SITKBasicFilters_EXPORT Image Resample(const Image &image1, Transform transform=itk::simple::Transform(), InterpolatorEnum interpolator=itk::simple::sitkLinear, double defaultPixelValue=0.0, PixelIDValueEnum outputPixelType=sitkUnknown, bool useNearestNeighborExtrapolator=false)
itk::simple::ResampleImageFilter Procedural Interface
SITKIO_EXPORT void WriteImage(const Image &image, const PathType &fileName, bool useCompression=false, int compressionLevel=-1)
WriteImage is a procedural interface to the ImageFileWriter. class which is convenient for many image...
SITKCommon_EXPORT void WriteTransform(const Transform &transform, const PathType &filename)