| 1 |
#ifndef ANTS_AFFINE_REGISTRATION_H_ |
| 2 |
#define ANTS_AFFINE_REGISTRATION_H_ |
| 3 |
|
| 4 |
#include <vector> |
| 5 |
#include <cstdlib> |
| 6 |
#include <ctime> |
| 7 |
#include "itkImage.h" |
| 8 |
#include "itkPoint.h" |
| 9 |
#include "itkCastImageFilter.h" |
| 10 |
#include "itkImageMaskSpatialObject.h" |
| 11 |
#include "itkCenteredEuler3DTransform.h" |
| 12 |
#include "itkRegularStepGradientDescentOptimizer.h" |
| 13 |
#include "itkGradientDescentOptimizer.h" |
| 14 |
#include "itkCenteredTransformInitializer.h" |
| 15 |
#include "itkMattesMutualInformationImageToImageMetric.h" |
| 16 |
#include "itkMultiResolutionImageRegistrationMethod.h" |
| 17 |
#include "itkLinearInterpolateImageFunction.h" |
| 18 |
#include "itkANTSAffine3DTransform.h" |
| 19 |
#include "itkCenteredRigid2DTransform.h" |
| 20 |
#include "itkANTSCenteredAffine2DTransform.h" |
| 21 |
#include "itkTransformFactory.h" |
| 22 |
#include "itkTransformFileReader.h" |
| 23 |
#include "itkTransformFileWriter.h" |
| 24 |
#include "itkImageRegionIteratorWithIndex.h" |
| 25 |
#include "itkResampleImageFilter.h" |
| 26 |
#include "itkLinearInterpolateImageFunction.h" |
| 27 |
#include "itkWarpImageFilter.h" |
| 28 |
#include "itkWarpImageWAffineFilter.h" |
| 29 |
|
| 30 |
template <typename StringType, typename CastTransformPointerType> |
| 31 |
void |
| 32 |
read_transform_file(StringType filename, CastTransformPointerType & transform); |
| 33 |
|
| 34 |
template <typename TransformPointerType, typename StringType> |
| 35 |
void |
| 36 |
write_transform_file(TransformPointerType & transform, StringType str); |
| 37 |
|
| 38 |
template <typename ImagePointerType, typename MaskImagePointerType, typename TransformPointerType> |
| 39 |
void |
| 40 |
compute_single_affine_transform_3d(ImagePointerType I_fixed, |
| 41 |
ImagePointerType I_moving, |
| 42 |
MaskImagePointerType mask_fixed, |
| 43 |
TransformPointerType & transform, |
| 44 |
TransformPointerType & transform_initial); |
| 45 |
|
| 46 |
template <typename ImagePointerType, typename MaskImagePointerType, typename TransformPointerType> |
| 47 |
void |
| 48 |
compute_single_affine_transform_2d(ImagePointerType I_fixed, |
| 49 |
ImagePointerType I_moving, |
| 50 |
MaskImagePointerType mask_fixed, |
| 51 |
TransformPointerType & transform, |
| 52 |
TransformPointerType & transform_initial); |
| 53 |
|
| 54 |
template <typename ImagePointerType, typename MaskImagePointerType, typename TransformPointerType> |
| 55 |
void |
| 56 |
compute_single_affine_transform(ImagePointerType fixedImage, |
| 57 |
ImagePointerType movingImage, |
| 58 |
MaskImagePointerType maskImage, |
| 59 |
TransformPointerType & transform, |
| 60 |
TransformPointerType & transform_initial); |
| 61 |
|
| 62 |
// void compute_single_affine_transform_2d(itk::Image<float, 2>::Pointer I_fixed, |
| 63 |
// itk::Image<float, 2>::Pointer I_moving, |
| 64 |
// itk::Image<float, 2>::Pointer mask_fixed, |
| 65 |
// itk::CenteredAffine2DTransform<double>::Pointer &transform); |
| 66 |
template <typename DisplacementFieldPointerType> |
| 67 |
void |
| 68 |
create_deformation_field_byref(const DisplacementFieldPointerType & ref, DisplacementFieldPointerType & field); |
| 69 |
|
| 70 |
// this is obsolet, use itkWarpImageWAffineFilter |
| 71 |
template <typename TransformPointerType, typename DisplacementFieldPointerType> |
| 72 |
void |
| 73 |
compose_affine_with_field(const TransformPointerType & aff, |
| 74 |
const DisplacementFieldPointerType & field, |
| 75 |
DisplacementFieldPointerType & field_output); |
| 76 |
|
| 77 |
template <typename ImagePointerType, typename DisplacementFieldPointerType> |
| 78 |
void |
| 79 |
warp_image_field(const ImagePointerType & img_input, |
| 80 |
const DisplacementFieldPointerType & field, |
| 81 |
ImagePointerType & img_output); |
| 82 |
|
| 83 |
template <typename ImagePointerType, typename TransformPointerType, typename DisplacementFieldPointerType> |
| 84 |
void |
| 85 |
warp_image_field_waffine(const ImagePointerType & img_input, |
| 86 |
const TransformPointerType & aff, |
| 87 |
const DisplacementFieldPointerType & field, |
| 88 |
ImagePointerType & img_output); |
| 89 |
|
| 90 |
template <typename ImageTypePointer, typename RefImageTypePointer, typename TransformTypePointer> |
| 91 |
void |
| 92 |
affine_image(const ImageTypePointer & input_image, |
| 93 |
const TransformTypePointer & transform, |
| 94 |
const RefImageTypePointer & ref_image, |
| 95 |
ImageTypePointer & img_aff); |
| 96 |
|
| 97 |
template <typename TransformPointerType, typename StringType> |
| 98 |
void |
| 99 |
write_transform_file(TransformPointerType & transform, StringType filename) |
| 100 |
{ |
| 101 |
itk::TransformFileWriter::Pointer transform_writer; |
| 102 |
|
| 103 |
transform_writer = itk::TransformFileWriter::New(); |
| 104 |
transform_writer->SetFileName(filename); |
| 105 |
transform_writer->SetInput(transform); |
| 106 |
#if ITK_VERSION_MAJOR >= 5 |
| 107 |
transform_writer->SetUseCompression(true); |
| 108 |
#endif |
| 109 |
|
| 110 |
try |
| 111 |
{ |
| 112 |
transform_writer->Update(); |
| 113 |
} |
| 114 |
catch (const itk::ExceptionObject & err) |
| 115 |
{ |
| 116 |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl |
| 117 |
<< "Exception in writing transform file: " << std::endl |
| 118 |
<< filename << std::endl; |
| 119 |
return; |
| 120 |
} |
| 121 |
|
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
template <typename StringType, typename CastTransformPointerType> |
| 126 |
void |
| 127 |
read_transform_file(StringType filename, CastTransformPointerType & transform) |
| 128 |
{ |
| 129 |
typedef typename CastTransformPointerType::ObjectType CastTransformType; |
| 130 |
// const unsigned int InputSpaceDimension = CastTransformType::InputSpaceDimension; |
| 131 |
// const unsigned int OutputSpaceDimension = CastTransformType::OutputSpaceDimension; |
| 132 |
|
| 133 |
itk::TransformFactory<CastTransformType>::RegisterTransform(); |
| 134 |
itk::TransformFactory<itk::ANTSAffine3DTransform<double>>::RegisterTransform(); |
| 135 |
|
| 136 |
typedef typename itk::TransformFileReader TranReaderType; |
| 137 |
TranReaderType::Pointer tran_reader = TranReaderType::New(); |
| 138 |
tran_reader->SetFileName(filename); |
| 139 |
|
| 140 |
try |
| 141 |
{ |
| 142 |
tran_reader->Update(); |
| 143 |
} |
| 144 |
catch (const itk::ExceptionObject & err) |
| 145 |
{ |
| 146 |
std::cout << err << std::endl; |
| 147 |
std::cout << "Exception caught in reading tran para file: " << filename << std::endl; |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
transform = dynamic_cast<CastTransformType *>((tran_reader->GetTransformList())->front().GetPointer()); |
| 152 |
|
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
template <typename TransformPointerType, typename DisplacementFieldType> |
| 157 |
void |
| 158 |
convert_affine_para_to_deformation_field(TransformPointerType & transform, DisplacementFieldType & def) |
| 159 |
{ |
| 160 |
return; |
| 161 |
} |
| 162 |
|
| 163 |
template <typename ParaType, unsigned Dimension = 3> |
| 164 |
typename SEARCH_POINT_TYPE |
| 165 |
{ |
| 166 |
public: |
| 167 |
ParaType para0; // seed parameter |
| 168 |
ParaType para1; // local optimal parameter |
| 169 |
itk::Point<double, Dimension> center; // transformation center |
| 170 |
double rval; // registered value |
| 171 |
unsigned int index; // the index in the list, //for sorting |
| 172 |
int number_of_iteration; // the number of iteration used for gradient descent |
| 173 |
}; |
| 174 |
|
| 175 |
template <typename SEARCH_LIST, typename ParaType> |
| 176 |
void |
| 177 |
generate_search_seed_3d(SEARCH_LIST & search_list, ParaType & ret); |
| 178 |
|
| 179 |
template <typename SEARCH_LIST, typename ParaType> |
| 180 |
void |
| 181 |
generate_search_seed_2d(SEARCH_LIST & search_list, ParaType & ret); |
| 182 |
|
| 183 |
template <typename SEARCH_LIST, typename SEARCH_POINT> |
| 184 |
void |
| 185 |
get_best_search_point(SEARCH_LIST & search_list, SEARCH_POINT & spt); |
| 186 |
|
| 187 |
template <typename SEARCH_LIST, typename SEARCH_POINT> |
| 188 |
void |
| 189 |
add_search_point(SEARCH_LIST & search_list, SEARCH_POINT spt); |
| 190 |
|
| 191 |
// use moment of the image initializer to get cx and cy only |
| 192 |
template <typename ImagePointerType, typename ParaType> |
| 193 |
bool |
| 194 |
register_image_cxyz(ImagePointerType fixed_image, ImagePointerType moving_image, ParaType & para1, double & rval); |
| 195 |
|
| 196 |
// use an optional mask for the fixed image |
| 197 |
template <typename ImagePointerType, typename ImageMaskSpatialObjectPointerType, typename ParaType> |
| 198 |
bool |
| 199 |
register_image_affine3d_mres_mask(ImagePointerType fixed_image, |
| 200 |
ImagePointerType moving_image, |
| 201 |
ImageMaskSpatialObjectPointerType mask_fixed_object, |
| 202 |
ParaType para0, |
| 203 |
itk::Point<double, 3> center, |
| 204 |
int number_of_iteration, |
| 205 |
int MI_bins, |
| 206 |
int MI_samples, |
| 207 |
ParaType & para1, |
| 208 |
double & rval); |
| 209 |
|
| 210 |
template <typename ImagePointerType, typename ImageMaskSpatialObjectPointerType, typename ParaType> |
| 211 |
bool |
| 212 |
register_image_affine2d_mres_mask(ImagePointerType fixed_image, |
| 213 |
ImagePointerType moving_image, |
| 214 |
ImageMaskSpatialObjectPointerType mask_fixed_object, |
| 215 |
ParaType para0, |
| 216 |
itk::Point<double, 2> center, |
| 217 |
int number_of_iteration, |
| 218 |
int MI_bins, |
| 219 |
int MI_samples, |
| 220 |
ParaType & para1, |
| 221 |
double & rval); |
| 222 |
|
| 223 |
template <typename ImagePointerType, typename ParaType, typename PointType, typename TransformTypePointer> |
| 224 |
double |
| 225 |
get_cost_value_mmi(ImagePointerType fixedImage, |
| 226 |
ImagePointerType movingImage, |
| 227 |
ParaType para, |
| 228 |
PointType center, |
| 229 |
TransformTypePointer null_transform); |
| 230 |
|
| 231 |
template <typename SEARCH_POINT> |
| 232 |
bool |
| 233 |
compare_search_point(const SEARCH_POINT & lhs, const SEARCH_POINT & rhs) |
| 234 |
{ |
| 235 |
return lhs.rval < rhs.rval; |
| 236 |
} |
| 237 |
|
| 238 |
template <typename ImagePointerType, typename MaskImagePointerType, typename TransformPointerType> |
| 239 |
void |
| 240 |
compute_single_affine_transform_3d(ImagePointerType I_fixed, |
| 241 |
ImagePointerType I_moving, |
| 242 |
MaskImagePointerType mask_fixed, |
| 243 |
TransformPointerType & transform, |
| 244 |
TransformPointerType & transform_initial) |
| 245 |
{ |
| 246 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 247 |
const int ImageDimension = ImageType::ImageDimension; |
| 248 |
typedef typename ImageType::IOPixelType PixelType; |
| 249 |
typedef typename MaskImagePointerType::ObjectType MaskImageType; |
| 250 |
typedef typename MaskImageType::IOPixelType MaskPixelType; |
| 251 |
|
| 252 |
typedef typename TransformPointerType::ObjectType TransformType; |
| 253 |
typedef typename TransformType::ParametersType ParaType; |
| 254 |
|
| 255 |
// option parameters |
| 256 |
int number_of_seeds = 0; |
| 257 |
int number_of_iteration = 10000; |
| 258 |
int MI_bins = 32; |
| 259 |
int MI_samples = 6000; |
| 260 |
unsigned int time_seed = (unsigned int)time(nullptr); |
| 261 |
srand(time_seed); |
| 262 |
// TODO: need to fix here |
| 263 |
bool b_use_mask = 0; // (mask_fixed == nullptr); |
| 264 |
|
| 265 |
std::cout << "number_of_seeds: " << number_of_seeds << std::endl; |
| 266 |
std::cout << "rand_time_seed: " << time_seed << std::endl; |
| 267 |
std::cout << "number_of_iteration: " << number_of_iteration << std::endl; |
| 268 |
std::cout << "MI_bins: " << MI_bins << std::endl; |
| 269 |
std::cout << "MI_samples: " << MI_samples << std::endl; |
| 270 |
std::cout << "use mask: " << b_use_mask << std::endl; |
| 271 |
|
| 272 |
// memory of searched results |
| 273 |
typedef SEARCH_POINT_TYPE<ParaType, ImageDimension> SEARCH_POINT; |
| 274 |
typedef std::vector<SEARCH_POINT> SEARCH_LIST; |
| 275 |
|
| 276 |
SEARCH_LIST search_list; |
| 277 |
|
| 278 |
// typedef itk::ImageMaskSpatialObject<ImageDimension, MaskPixelType> ImageMaskSpatialObject; |
| 279 |
typedef itk::ImageMaskSpatialObject<ImageDimension> ImageMaskSpatialObject; |
| 280 |
typename ImageMaskSpatialObject::Pointer mask_fixed_object = 0; |
| 281 |
if (b_use_mask) |
| 282 |
{ |
| 283 |
typedef itk::Image<unsigned char, ImageDimension> CharMaskImageType; |
| 284 |
typedef itk::CastImageFilter<MaskImageType, CharMaskImageType> CastFilterType; |
| 285 |
typename CastFilterType::Pointer cast_filter = CastFilterType::New(); |
| 286 |
cast_filter->SetInput(mask_fixed); |
| 287 |
cast_filter->Update(); |
| 288 |
typename CharMaskImageType::Pointer mask_fixed_char = cast_filter->GetOutput(); |
| 289 |
|
| 290 |
mask_fixed_object = ImageMaskSpatialObject::New(); |
| 291 |
mask_fixed_object->SetImage(mask_fixed_char); |
| 292 |
} |
| 293 |
|
| 294 |
typename ImageType::PointType origin; |
| 295 |
origin.Fill(0); |
| 296 |
I_moving->SetOrigin(origin); |
| 297 |
I_fixed->SetOrigin(origin); |
| 298 |
|
| 299 |
typename TransformType::Pointer trans = TransformType::New(); |
| 300 |
ParaType para0(trans->GetNumberOfParameters()), para1(trans->GetNumberOfParameters()); |
| 301 |
|
| 302 |
double rval; |
| 303 |
SEARCH_POINT spt; |
| 304 |
|
| 305 |
typedef itk::CenteredEuler3DTransform<double> TransformType_Euler3D; |
| 306 |
ParaType para_cxy(TransformType_Euler3D::New()->GetNumberOfParameters()); // translated from pre registration |
| 307 |
|
| 308 |
// find initial center |
| 309 |
bool is_ok = false; |
| 310 |
itk::Point<double, 3> center; |
| 311 |
|
| 312 |
if (transform_initial.IsNull()) |
| 313 |
{ |
| 314 |
is_ok = register_image_cxyz(I_fixed, I_moving, para_cxy, rval); |
| 315 |
if (!is_ok) |
| 316 |
{ |
| 317 |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl << "initial affine registeration falied" << std::endl; |
| 318 |
std::exception(); |
| 319 |
} |
| 320 |
center[0] = para_cxy[3]; |
| 321 |
center[1] = para_cxy[4]; |
| 322 |
center[2] = para_cxy[5]; |
| 323 |
} |
| 324 |
else |
| 325 |
{ |
| 326 |
center[0] = transform_initial->GetCenter()[0]; |
| 327 |
center[1] = transform_initial->GetCenter()[1]; |
| 328 |
center[2] = transform_initial->GetCenter()[2]; |
| 329 |
} |
| 330 |
|
| 331 |
std::cout << std::endl; |
| 332 |
for (int n = 0; (number_of_seeds > 0) ? (n < number_of_seeds) : (n <= number_of_seeds); n++) |
| 333 |
{ |
| 334 |
if (n == 0) |
| 335 |
{ |
| 336 |
if (transform_initial.IsNull()) |
| 337 |
{ |
| 338 |
para0[0] = 0.0; |
| 339 |
para0[1] = 0.0; |
| 340 |
para0[2] = 0.0; |
| 341 |
para0[3] = 1.0; |
| 342 |
para0[4] = 1.0; |
| 343 |
para0[5] = 1.0; |
| 344 |
para0[6] = 1.0; |
| 345 |
para0[7] = 0.0; |
| 346 |
para0[8] = 0.0; |
| 347 |
para0[9] = 0.0; |
| 348 |
para0[10] = 0.0; |
| 349 |
para0[11] = 0.0; |
| 350 |
para0[12] = 0.0; |
| 351 |
} |
| 352 |
else // use input as intial |
| 353 |
{ |
| 354 |
for (unsigned int i = 0; i < transform_initial->GetParameters().Size(); i++) |
| 355 |
{ |
| 356 |
para0[i] = transform_initial->GetParameters()[i]; |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
else |
| 361 |
{ |
| 362 |
generate_search_seed_3d(search_list, para0); |
| 363 |
} |
| 364 |
|
| 365 |
// main registration using affine transform |
| 366 |
is_ok = register_image_affine3d_mres_mask( |
| 367 |
I_fixed, I_moving, mask_fixed_object, para0, center, number_of_iteration, MI_bins, MI_samples, para1, rval); |
| 368 |
if (!is_ok) |
| 369 |
{ |
| 370 |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl |
| 371 |
<< "affine registration failed!" << std::endl |
| 372 |
<< "use the initial parameters" << std::endl; |
| 373 |
// return -1; |
| 374 |
} |
| 375 |
|
| 376 |
spt.para0 = para0; |
| 377 |
spt.para1 = para1; |
| 378 |
spt.center = center; |
| 379 |
spt.rval = rval; |
| 380 |
spt.index = n; |
| 381 |
spt.number_of_iteration = number_of_iteration; |
| 382 |
|
| 383 |
std::cout << "para0: " << para0 << std::endl; |
| 384 |
std::cout << "para1: " << para1 << std::endl; |
| 385 |
std::cout << "center: " << center << std::endl; |
| 386 |
std::cout << "rval: " << rval << std::endl; |
| 387 |
std::cout << "add the search result to the list ... seed [" << n << "]" << std::endl << std::endl; |
| 388 |
|
| 389 |
add_search_point(search_list, spt); |
| 390 |
} |
| 391 |
get_best_search_point(search_list, spt); |
| 392 |
|
| 393 |
std::cout << std::endl << "History: " << std::endl; |
| 394 |
for (int ii = 0; ii < search_list.size(); ii++) |
| 395 |
{ |
| 396 |
std::cout << "[" << ii << "]: " << search_list[ii].rval << std::endl; |
| 397 |
std::cout << "center: " << search_list[ii].center << std::endl; |
| 398 |
std::cout << "para0: " << search_list[ii].para0 << std::endl; |
| 399 |
std::cout << "para1: " << search_list[ii].para1 << std::endl; |
| 400 |
} |
| 401 |
|
| 402 |
typename TransformType::Pointer transform_final = TransformType::New(); |
| 403 |
transform_final->SetParameters(spt.para1); |
| 404 |
transform_final->SetCenter(center); |
| 405 |
|
| 406 |
std::cout << "final transform parameters = " << transform_final->GetParameters() << std::endl; |
| 407 |
|
| 408 |
transform = transform_final; |
| 409 |
} |
| 410 |
|
| 411 |
double |
| 412 |
myrand(double l, double u) |
| 413 |
{ |
| 414 |
// std::cout<<"in myrand" << std::endl; |
| 415 |
double r = l + (u - l) * rand() / (RAND_MAX + 1.0); |
| 416 |
|
| 417 |
return r; |
| 418 |
// std::cout<<"out myrand" << std::endl; |
| 419 |
} |
| 420 |
|
| 421 |
template <typename ParaType> |
| 422 |
double |
| 423 |
dist2_search_point(ParaType para0, ParaType para1) |
| 424 |
{ |
| 425 |
// use theta / scale / skew to compare two nodes |
| 426 |
double d2 = 0; |
| 427 |
|
| 428 |
// double scale[4] = {1.0, 1.0, 1.0, 1.0}; |
| 429 |
for (int ii = 0; ii < 12; ii++) |
| 430 |
{ |
| 431 |
double a = para0[ii] - para1[ii]; |
| 432 |
d2 += a * a; // * scale[ii]; |
| 433 |
} |
| 434 |
return d2; |
| 435 |
} |
| 436 |
|
| 437 |
template <typename SEARCH_LIST, typename ParaType> |
| 438 |
void |
| 439 |
generate_search_seed_3d(SEARCH_LIST & search_list, ParaType & para) |
| 440 |
{ |
| 441 |
bool b_found = 0; |
| 442 |
double s1, s2, s3, k1, k2, k3 = 0; |
| 443 |
double a = 0; // rotation alpha |
| 444 |
double u = 0, v = 0, w = 0; // rotation axis |
| 445 |
|
| 446 |
// ParaType para(13); |
| 447 |
|
| 448 |
constexpr double scale_upper = 1.5; |
| 449 |
const double scale_lower = 1 / 1.5; |
| 450 |
const double skew_lower = -1.0; |
| 451 |
constexpr double skew_upper = 1.0; |
| 452 |
constexpr double dist2_thres = 0.3; |
| 453 |
constexpr double mypi = 3.1415926536; |
| 454 |
const double rot_angle_upper = mypi * (0.5); |
| 455 |
const double rot_angle_lower = mypi * (-0.5); |
| 456 |
|
| 457 |
unsigned int iteration = 0; |
| 458 |
unsigned int maxiteration = 50; |
| 459 |
|
| 460 |
while (b_found == 0 && iteration < maxiteration) |
| 461 |
{ |
| 462 |
iteration++; |
| 463 |
s1 = myrand(scale_lower, scale_upper); |
| 464 |
s2 = myrand(scale_lower, scale_upper); |
| 465 |
s3 = myrand(scale_lower, scale_upper); |
| 466 |
k1 = myrand(skew_lower, skew_upper); |
| 467 |
k2 = myrand(skew_lower, skew_upper); |
| 468 |
k3 = myrand(skew_lower, skew_upper); |
| 469 |
a = myrand(rot_angle_lower, rot_angle_upper); |
| 470 |
u = myrand(-1, 1); |
| 471 |
v = myrand(-1, 1); |
| 472 |
w = myrand(-1, 1); |
| 473 |
|
| 474 |
double n1 = 1.0 / sqrt(u * u + v * v + w * w); |
| 475 |
double sin_half_a = sin(a * 0.5); |
| 476 |
|
| 477 |
para.Fill(0.0); |
| 478 |
|
| 479 |
para[0] = sin_half_a * u * n1; |
| 480 |
para[1] = sin_half_a * v * n1; |
| 481 |
para[2] = sin_half_a * w * n1; |
| 482 |
para[3] = cos(a * 0.5); |
| 483 |
para[4] = s1; |
| 484 |
para[5] = s2; |
| 485 |
para[6] = s3; |
| 486 |
para[7] = k1; |
| 487 |
para[8] = k2; |
| 488 |
para[9] = k3; |
| 489 |
|
| 490 |
// |
| 491 |
std::cout << "test rand: " << para << " iteration " << iteration << std::endl; |
| 492 |
|
| 493 |
// search nearby search points |
| 494 |
bool bfar = 1; |
| 495 |
for (int i = 0; (bfar) & (i < search_list.size()); i++) |
| 496 |
{ |
| 497 |
ParaType para0 = search_list[i].para0; |
| 498 |
ParaType para1 = search_list[i].para1; |
| 499 |
double d0 = dist2_search_point(para0, para); |
| 500 |
double d1 = dist2_search_point(para1, para); |
| 501 |
|
| 502 |
// std::cout << "compare with para0: " << d0 << para0 << std::endl; |
| 503 |
// std::cout << "compare with para1: " << d1 << para1 << std::endl; |
| 504 |
|
| 505 |
bfar = bfar & (d0 > dist2_thres) & (d1 > dist2_thres); |
| 506 |
} |
| 507 |
|
| 508 |
b_found = bfar; |
| 509 |
// std::cout << "b_found = " << b_found << " bfar = " << bfar << std::endl; |
| 510 |
} |
| 511 |
|
| 512 |
// ret = para; |
| 513 |
} |
| 514 |
|
| 515 |
template <typename SEARCH_LIST, typename ParaType> |
| 516 |
void |
| 517 |
generate_search_seed_2d(SEARCH_LIST & search_list, ParaType & para) |
| 518 |
{ |
| 519 |
bool b_found = 0; |
| 520 |
double r1, s1, s2, k = 0; |
| 521 |
|
| 522 |
constexpr double pi = 3.1415927; |
| 523 |
const double theta_upper = pi / 4; |
| 524 |
const double theta_lower = -pi / 4; |
| 525 |
constexpr double scale_upper = 1.5; |
| 526 |
const double scale_lower = 1 / 1.5; |
| 527 |
const double skew_lower = -1.0; |
| 528 |
constexpr double skew_upper = 1.0; |
| 529 |
constexpr double dist2_thres = 0.1; |
| 530 |
|
| 531 |
unsigned int iteration = 0; |
| 532 |
unsigned int maxiteration = 50; |
| 533 |
|
| 534 |
while (b_found == 0 && iteration < maxiteration) |
| 535 |
{ |
| 536 |
// for(;~b_found; ){ |
| 537 |
// std::cout << "b_found = " << b_found << std::endl; |
| 538 |
iteration++; |
| 539 |
r1 = myrand(theta_lower, theta_upper); |
| 540 |
s1 = myrand(scale_lower, scale_upper); |
| 541 |
s2 = myrand(scale_lower, scale_upper); |
| 542 |
k = myrand(skew_lower, skew_upper); |
| 543 |
para.Fill(0.0); |
| 544 |
para[0] = r1; |
| 545 |
para[1] = s1; |
| 546 |
para[2] = s2; |
| 547 |
para[3] = k; |
| 548 |
|
| 549 |
// |
| 550 |
// std::cout << "test rand: " << para << " iteration " << iteration << std::endl; |
| 551 |
|
| 552 |
// search nearby search points |
| 553 |
bool bfar = 1; |
| 554 |
for (int i = 0; (bfar) & (i < search_list.size()); i++) |
| 555 |
{ |
| 556 |
ParaType para0 = search_list[i].para0; |
| 557 |
ParaType para1 = search_list[i].para1; |
| 558 |
double d0 = dist2_search_point(para0, para); |
| 559 |
double d1 = dist2_search_point(para1, para); |
| 560 |
|
| 561 |
// std::cout << "compare with para0: " << d0 << para0 << std::endl; |
| 562 |
// std::cout << "compare with para1: " << d1 << para1 << std::endl; |
| 563 |
|
| 564 |
bfar = bfar & (d0 > dist2_thres) & (d1 > dist2_thres); |
| 565 |
} |
| 566 |
|
| 567 |
b_found = bfar; |
| 568 |
// std::cout << "b_found = " << b_found << " bfar = " << bfar << std::endl; |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
template <typename SEARCH_LIST, typename SEARCH_POINT> |
| 573 |
void |
| 574 |
get_best_search_point(SEARCH_LIST & search_list, SEARCH_POINT & spt) |
| 575 |
{ |
| 576 |
std::sort(search_list.begin(), search_list.end(), compare_search_point<SEARCH_POINT>); |
| 577 |
|
| 578 |
spt = search_list[0]; |
| 579 |
} |
| 580 |
|
| 581 |
template <typename SEARCH_LIST, typename SEARCH_POINT> |
| 582 |
void |
| 583 |
add_search_point(SEARCH_LIST & search_list, SEARCH_POINT spt) |
| 584 |
{ |
| 585 |
search_list.push_back(spt); |
| 586 |
} |
| 587 |
|
| 588 |
template <typename ImagePointerType, typename ParaType, typename PointType, typename TransformTypePointer> |
| 589 |
double |
| 590 |
get_cost_value_mmi(ImagePointerType fixedImage, |
| 591 |
ImagePointerType movingImage, |
| 592 |
ParaType para, |
| 593 |
PointType center, |
| 594 |
TransformTypePointer null_transform) |
| 595 |
{ |
| 596 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 597 |
typedef typename TransformTypePointer::ObjectType TransformType; |
| 598 |
|
| 599 |
typename TransformType::Pointer transform = TransformType::New(); |
| 600 |
transform->SetCenter(center); |
| 601 |
// transform->SetParameters(para); |
| 602 |
|
| 603 |
typedef typename itk::MattesMutualInformationImageToImageMetric<ImageType, ImageType> mattesMutualInfoMetricType; |
| 604 |
typename mattesMutualInfoMetricType::Pointer mattesMutualInfo = mattesMutualInfoMetricType::New(); |
| 605 |
|
| 606 |
typedef typename itk::LinearInterpolateImageFunction<ImageType, double> InterpolatorType; |
| 607 |
typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); |
| 608 |
|
| 609 |
interpolator->SetInputImage(movingImage); |
| 610 |
|
| 611 |
mattesMutualInfo->SetFixedImage(fixedImage); |
| 612 |
mattesMutualInfo->SetMovingImage(movingImage); |
| 613 |
mattesMutualInfo->SetFixedImageRegion(fixedImage->GetBufferedRegion()); |
| 614 |
// mattesMutualInfo->SetMovingImage(outputImage); |
| 615 |
mattesMutualInfo->SetTransform(transform); |
| 616 |
mattesMutualInfo->SetInterpolator(interpolator); |
| 617 |
mattesMutualInfo->SetNumberOfHistogramBins(32); |
| 618 |
mattesMutualInfo->SetNumberOfSpatialSamples(5000); |
| 619 |
mattesMutualInfo->SetTransformParameters(para); |
| 620 |
mattesMutualInfo->Initialize(); |
| 621 |
double rval = 0; |
| 622 |
try |
| 623 |
{ |
| 624 |
rval = mattesMutualInfo->GetValue(para); |
| 625 |
} |
| 626 |
catch (const itk::ExceptionObject & err) |
| 627 |
{ |
| 628 |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl |
| 629 |
<< "Exception caught in computing mattesMutualInfo after registration" << std::endl |
| 630 |
<< "Maybe: Too many samples map outside moving image buffer" << std::endl |
| 631 |
<< "Set the cost value = 0 (max for MutualInfo) " << std::endl; |
| 632 |
rval = 0; |
| 633 |
} |
| 634 |
|
| 635 |
// test the cost before registration |
| 636 |
transform->SetIdentity(); |
| 637 |
transform->SetCenter(center); |
| 638 |
ParaType para0 = transform->GetParameters(); |
| 639 |
mattesMutualInfo->SetTransformParameters(para0); |
| 640 |
mattesMutualInfo->Initialize(); |
| 641 |
double rval0 = 0; |
| 642 |
try |
| 643 |
{ |
| 644 |
rval0 = mattesMutualInfo->GetValue(para0); |
| 645 |
} |
| 646 |
catch (const itk::ExceptionObject & err) |
| 647 |
{ |
| 648 |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl |
| 649 |
<< "Exception caught in computing mattesMutualInfo before registration" << std::endl |
| 650 |
<< "Maybe: Too many samples map outside moving image buffer" << std::endl |
| 651 |
<< "Set the cost value = 0 (max for MutualInfo) " << std::endl; |
| 652 |
rval0 = 0; |
| 653 |
} |
| 654 |
|
| 655 |
std::cout << "in cost: before register: cost = " << rval0 << std::endl; |
| 656 |
std::cout << "in cost: after register: cost = " << rval << std::endl; |
| 657 |
|
| 658 |
return rval; |
| 659 |
} |
| 660 |
|
| 661 |
template <typename ImagePointerType, typename ParaType> |
| 662 |
bool |
| 663 |
register_image_cxy(ImagePointerType fixed_image, ImagePointerType moving_image, ParaType & para1, double & rval) |
| 664 |
{ |
| 665 |
// for 3d image use CenteredRigid2DTransform |
| 666 |
typedef itk::RegularStepGradientDescentOptimizer OptimizerType; |
| 667 |
typedef itk::CenteredRigid2DTransform<double> TransformType_Rigid2D; |
| 668 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 669 |
typename TransformType_Rigid2D::Pointer transform = TransformType_Rigid2D::New(); |
| 670 |
|
| 671 |
typedef itk::CenteredTransformInitializer<TransformType_Rigid2D, ImageType, ImageType> TransformInitializerType; |
| 672 |
|
| 673 |
typename TransformInitializerType::Pointer initializer = TransformInitializerType::New(); |
| 674 |
|
| 675 |
try |
| 676 |
{ |
| 677 |
transform->SetIdentity(); |
| 678 |
initializer->SetTransform(transform); |
| 679 |
initializer->SetFixedImage(fixed_image); |
| 680 |
initializer->SetMovingImage(moving_image); |
| 681 |
initializer->MomentsOn(); |
| 682 |
initializer->InitializeTransform(); |
| 683 |
transform->SetAngle(0.0); |
| 684 |
} |
| 685 |
catch (const itk::ExceptionObject & err) |
| 686 |
{ |
| 687 |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!1" << std::endl << "Exception in InitializeTransform" << std::endl; |
| 688 |
return false; |
| 689 |
} |
| 690 |
|
| 691 |
para1 = transform->GetParameters(); |
| 692 |
|
| 693 |
std::cout << "finish initialize cx/cy/cz ..." << std::endl; |
| 694 |
std::cout << "cx/cy parameters (Euler3D): " << para1 << std::endl; |
| 695 |
|
| 696 |
return true; |
| 697 |
} |
| 698 |
|
| 699 |
template <typename ImagePointerType, typename ParaType> |
| 700 |
bool |
| 701 |
register_image_cxyz(ImagePointerType fixed_image, ImagePointerType moving_image, ParaType & para1, double & rval) |
| 702 |
{ |
| 703 |
// for 3d image use CenteredRigid2DTransform |
| 704 |
typedef itk::RegularStepGradientDescentOptimizer OptimizerType; |
| 705 |
typedef itk::CenteredEuler3DTransform<double> TransformType_Euler3D; |
| 706 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 707 |
|
| 708 |
TransformType_Euler3D::Pointer transform = TransformType_Euler3D::New(); |
| 709 |
|
| 710 |
typedef itk::CenteredTransformInitializer<TransformType_Euler3D, ImageType, ImageType> TransformInitializerType; |
| 711 |
|
| 712 |
typename TransformInitializerType::Pointer initializer = TransformInitializerType::New(); |
| 713 |
|
| 714 |
try |
| 715 |
{ |
| 716 |
transform->SetIdentity(); |
| 717 |
initializer->SetTransform(transform); |
| 718 |
initializer->SetFixedImage(fixed_image); |
| 719 |
initializer->SetMovingImage(moving_image); |
| 720 |
initializer->MomentsOn(); |
| 721 |
initializer->InitializeTransform(); |
| 722 |
} |
| 723 |
catch (const itk::ExceptionObject & err) |
| 724 |
{ |
| 725 |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!1" << std::endl << "Exception in InitializeTransform" << std::endl; |
| 726 |
return false; |
| 727 |
} |
| 728 |
|
| 729 |
para1 = transform->GetParameters(); |
| 730 |
|
| 731 |
std::cout << "finish initialize cx/cy/cz ..." << std::endl; |
| 732 |
std::cout << "cx/cy parameters (Euler3D): " << para1 << std::endl; |
| 733 |
|
| 734 |
return true; |
| 735 |
} |
| 736 |
|
| 737 |
template <typename ImagePointerType, typename ImageMaskSpatialObjectPointerType, typename ParaType> |
| 738 |
bool |
| 739 |
register_image_affine3d_mres_mask(ImagePointerType fixed_image, |
| 740 |
ImagePointerType moving_image, |
| 741 |
ImageMaskSpatialObjectPointerType mask_fixed_object, |
| 742 |
ParaType para0, |
| 743 |
itk::Point<double, 3> center, |
| 744 |
int number_of_iteration, |
| 745 |
int MI_bins, |
| 746 |
int MI_samples, |
| 747 |
ParaType & para1, |
| 748 |
double & rval) |
| 749 |
{ |
| 750 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 751 |
typedef itk::RegularStepGradientDescentOptimizer OptimizerType; |
| 752 |
typedef itk::MattesMutualInformationImageToImageMetric<ImageType, ImageType> MetricType; |
| 753 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 754 |
|
| 755 |
typename MetricType::Pointer metric = MetricType::New(); |
| 756 |
|
| 757 |
// for mattesMutualInfo |
| 758 |
metric->SetNumberOfHistogramBins(MI_bins); /** 32 BA */ |
| 759 |
metric->SetNumberOfSpatialSamples(MI_samples); /** 6000 BA */ |
| 760 |
if (mask_fixed_object) |
| 761 |
{ |
| 762 |
metric->SetFixedImageMask(mask_fixed_object); |
| 763 |
std::cout << mask_fixed_object << std::endl; |
| 764 |
std::cout << mask_fixed_object->GetImage() << std::endl; |
| 765 |
} |
| 766 |
|
| 767 |
// typedef TransformType_Rigid2D TransformType_Pre; |
| 768 |
|
| 769 |
typedef itk::LinearInterpolateImageFunction<ImageType, double> InterpolatorType; |
| 770 |
typedef itk::MultiResolutionImageRegistrationMethod<ImageType, ImageType> RegistrationType; |
| 771 |
|
| 772 |
typedef itk::MultiResolutionPyramidImageFilter<ImageType, ImageType> fPyramidType; |
| 773 |
typename fPyramidType::Pointer fixedImagePyramid = fPyramidType::New(); |
| 774 |
typename fPyramidType::Pointer movingImagePyramid = fPyramidType::New(); |
| 775 |
|
| 776 |
typename ImageType::PointType origin; |
| 777 |
origin.Fill(0); |
| 778 |
moving_image->SetOrigin(origin); |
| 779 |
fixed_image->SetOrigin(origin); |
| 780 |
|
| 781 |
typename OptimizerType::Pointer optimizer = OptimizerType::New(); |
| 782 |
typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); |
| 783 |
typename RegistrationType::Pointer registration = RegistrationType::New(); |
| 784 |
|
| 785 |
typedef OptimizerType::ScalesType OptimizerScalesType; |
| 786 |
|
| 787 |
/*******************************************/ |
| 788 |
/* translate to para0 here */ |
| 789 |
|
| 790 |
std::cout << "pre registration para :" << para0 << std::endl; |
| 791 |
|
| 792 |
typedef itk::ANTSAffine3DTransform<double> TransformType_ANTSAffine3D; |
| 793 |
typename TransformType_ANTSAffine3D::Pointer transform_a = TransformType_ANTSAffine3D::New(); |
| 794 |
transform_a->SetCenter(center); |
| 795 |
std::cout << "initial center: " << transform_a->GetCenter() << std::endl; |
| 796 |
|
| 797 |
// typedef OptimizerType::ScalesType OptimizerScalesType; |
| 798 |
OptimizerScalesType optimizerScales_a(transform_a->GetNumberOfParameters()); |
| 799 |
const double translationScale = 1.0 / 1.e4; /** BA */ // /1.e6; // 1.e6; //1.e6; //1.0 / 1000.0; |
| 800 |
optimizerScales_a[0] = 1.0; // quaternion |
| 801 |
optimizerScales_a[1] = 1.0; // quaternion |
| 802 |
optimizerScales_a[2] = 1.0; // quaternion |
| 803 |
optimizerScales_a[3] = 1.0; // quaternion |
| 804 |
optimizerScales_a[4] = 1.0; // s1 |
| 805 |
optimizerScales_a[5] = 1.0; // s2 |
| 806 |
optimizerScales_a[6] = 1.0; // s3 |
| 807 |
optimizerScales_a[7] = 1.0; // k1 |
| 808 |
optimizerScales_a[8] = 1.0; // k2 |
| 809 |
optimizerScales_a[9] = 1.0; // k3 |
| 810 |
optimizerScales_a[10] = translationScale; |
| 811 |
optimizerScales_a[11] = translationScale; |
| 812 |
optimizerScales_a[12] = translationScale; |
| 813 |
optimizer->SetScales(optimizerScales_a); |
| 814 |
|
| 815 |
optimizer->SetMaximumStepLength(0.1); |
| 816 |
optimizer->SetMinimumStepLength(1.e-5); |
| 817 |
// optimizer->SetNumberOfIterations( 1000 ); |
| 818 |
// optimizer->SetNumberOfIterations( 500 ); |
| 819 |
optimizer->SetNumberOfIterations(number_of_iteration); // (500); /** BA */ |
| 820 |
optimizer->MinimizeOn(); |
| 821 |
|
| 822 |
// Create the Command observer and register it with the optimizer. |
| 823 |
// |
| 824 |
// CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New(); |
| 825 |
// optimizer->AddObserver( itk::IterationEvent(), observer ); |
| 826 |
|
| 827 |
registration = RegistrationType::New(); |
| 828 |
|
| 829 |
registration->SetNumberOfLevels(3); |
| 830 |
registration->SetFixedImagePyramid(fixedImagePyramid); |
| 831 |
registration->SetMovingImagePyramid(movingImagePyramid); |
| 832 |
registration->SetMetric(metric); |
| 833 |
registration->SetOptimizer(optimizer); |
| 834 |
registration->SetInterpolator(interpolator); |
| 835 |
registration->SetFixedImage(fixed_image); |
| 836 |
registration->SetMovingImage(moving_image); |
| 837 |
registration->SetFixedImageRegion(fixed_image->GetLargestPossibleRegion()); |
| 838 |
registration->SetTransform(transform_a); // reset the parameter |
| 839 |
// registration->SetInitialTransformParameters(para_pre ); |
| 840 |
registration->SetInitialTransformParameters(para0); |
| 841 |
|
| 842 |
std::cout << "reset initial transform parameters" << std::endl; |
| 843 |
std::cout << "para_pre: " << para0 << std::endl; |
| 844 |
// std::cout << "transform_a: " << transform_a << std::endl; |
| 845 |
|
| 846 |
rval = get_cost_value_mmi(fixed_image, moving_image, para0, center, transform_a); |
| 847 |
std::cout << "init measure value: rval = " << rval << std::endl; |
| 848 |
|
| 849 |
// rval = optimizer->GetValue(para0); |
| 850 |
// rval = metric->GetValue(para0); |
| 851 |
|
| 852 |
bool bsuc = 1; |
| 853 |
try |
| 854 |
{ |
| 855 |
registration->Update(); |
| 856 |
} |
| 857 |
catch (const itk::ExceptionObject & err) |
| 858 |
{ |
| 859 |
std::cout << "ExceptionObject caught !" << std::endl; |
| 860 |
std::cout << err << std::endl; |
| 861 |
bsuc = 0; |
| 862 |
// std::exception(); |
| 863 |
} |
| 864 |
|
| 865 |
if (bsuc) |
| 866 |
{ |
| 867 |
// OptimizerType::ParametersType |
| 868 |
// finalParameters = registration->GetLastTransformParameters(); |
| 869 |
para1 = registration->GetLastTransformParameters(); |
| 870 |
// para1 = finalParameters; |
| 871 |
// rval = optimizer->GetValue(para1); |
| 872 |
// rval = metric->GetValue(para1); |
| 873 |
// rval = registration->GetMetric()->GetValue(para1); |
| 874 |
|
| 875 |
rval = get_cost_value_mmi(fixed_image, moving_image, para1, center, transform_a); |
| 876 |
// double rval2 = optimizer->GetValue(); |
| 877 |
// std::cout << "measure value: rval2 = " << rval2 << std::endl; |
| 878 |
} |
| 879 |
else |
| 880 |
{ |
| 881 |
// register failed |
| 882 |
para1 = para0; // registration->GetLastTransformParameters(); |
| 883 |
rval = get_cost_value_mmi(fixed_image, moving_image, para1, center, transform_a); |
| 884 |
} |
| 885 |
|
| 886 |
std::cout << "final affine3d registration para :" << para1 << std::endl; |
| 887 |
std::cout << "use iteration: " << optimizer->GetNumberOfIterations() << std::endl; |
| 888 |
std::cout << "measure value: rval = " << rval << std::endl; |
| 889 |
std::cout << "finish register..." << std::endl; |
| 890 |
|
| 891 |
return bsuc; |
| 892 |
} |
| 893 |
|
| 894 |
template <typename ImagePointerType, typename ImageMaskSpatialObjectPointerType, typename ParaType> |
| 895 |
bool |
| 896 |
register_image_affine2d_mres_mask(ImagePointerType fixed_image, |
| 897 |
ImagePointerType moving_image, |
| 898 |
ImageMaskSpatialObjectPointerType mask_fixed_object, |
| 899 |
ParaType para0, |
| 900 |
itk::Point<double, 2> center, |
| 901 |
int number_of_iteration, |
| 902 |
int MI_bins, |
| 903 |
int MI_samples, |
| 904 |
ParaType & para1, |
| 905 |
double & rval) |
| 906 |
{ |
| 907 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 908 |
typedef itk::RegularStepGradientDescentOptimizer OptimizerType; |
| 909 |
typedef itk::MattesMutualInformationImageToImageMetric<ImageType, ImageType> MetricType; |
| 910 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 911 |
|
| 912 |
typename MetricType::Pointer metric = MetricType::New(); |
| 913 |
|
| 914 |
// for mattesMutualInfo |
| 915 |
metric->SetNumberOfHistogramBins(MI_bins); /** 32 BA */ |
| 916 |
metric->SetNumberOfSpatialSamples(MI_samples); /** 6000 BA */ |
| 917 |
if (mask_fixed_object) |
| 918 |
{ |
| 919 |
metric->SetFixedImageMask(mask_fixed_object); |
| 920 |
std::cout << mask_fixed_object << std::endl; |
| 921 |
std::cout << mask_fixed_object->GetImage() << std::endl; |
| 922 |
} |
| 923 |
|
| 924 |
// typedef TransformType_Rigid2D TransformType_Pre; |
| 925 |
|
| 926 |
typedef itk::LinearInterpolateImageFunction<ImageType, double> InterpolatorType; |
| 927 |
typedef itk::MultiResolutionImageRegistrationMethod<ImageType, ImageType> RegistrationType; |
| 928 |
|
| 929 |
typedef itk::MultiResolutionPyramidImageFilter<ImageType, ImageType> fPyramidType; |
| 930 |
typename fPyramidType::Pointer fixedImagePyramid = fPyramidType::New(); |
| 931 |
typename fPyramidType::Pointer movingImagePyramid = fPyramidType::New(); |
| 932 |
|
| 933 |
typename ImageType::PointType origin; |
| 934 |
origin.Fill(0); |
| 935 |
moving_image->SetOrigin(origin); |
| 936 |
fixed_image->SetOrigin(origin); |
| 937 |
|
| 938 |
typename OptimizerType::Pointer optimizer = OptimizerType::New(); |
| 939 |
typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); |
| 940 |
typename RegistrationType::Pointer registration = RegistrationType::New(); |
| 941 |
|
| 942 |
typedef OptimizerType::ScalesType OptimizerScalesType; |
| 943 |
|
| 944 |
/*******************************************/ |
| 945 |
/* translate to para0 here */ |
| 946 |
|
| 947 |
std::cout << "pre registration para :" << para0 << std::endl; |
| 948 |
|
| 949 |
// typedef itk::CenteredAffine2DTransform<double> TransformType; |
| 950 |
// typedef itk::CenteredAffine2DTransform<double> TransformType_GSAffine2D; |
| 951 |
// typename TransformType_GSAffine2D::Pointer transform_a = TransformType_GSAffine2D::New(); |
| 952 |
typedef itk::ANTSCenteredAffine2DTransform<double> TransformType_ANTSAffine2D; |
| 953 |
typename TransformType_ANTSAffine2D::Pointer transform_a = TransformType_ANTSAffine2D::New(); |
| 954 |
// transform_a->SetCenter(center); |
| 955 |
// std::cout<<"initial center: " << transform_a->GetCenter() << std::endl; |
| 956 |
|
| 957 |
OptimizerScalesType optimizerScales_a(transform_a->GetNumberOfParameters()); |
| 958 |
const double translationScale = 1.0 / 1000.0; |
| 959 |
optimizerScales_a[0] = 1.0; |
| 960 |
optimizerScales_a[1] = 1.0; |
| 961 |
optimizerScales_a[2] = 1.0; |
| 962 |
optimizerScales_a[3] = 1.0; |
| 963 |
optimizerScales_a[4] = translationScale; |
| 964 |
optimizerScales_a[5] = translationScale; |
| 965 |
optimizerScales_a[6] = translationScale; |
| 966 |
optimizerScales_a[7] = translationScale; |
| 967 |
optimizer->SetScales(optimizerScales_a); |
| 968 |
|
| 969 |
optimizer->SetMaximumStepLength(0.1); |
| 970 |
optimizer->SetMinimumStepLength(0.01); |
| 971 |
optimizer->SetNumberOfIterations(number_of_iteration); |
| 972 |
optimizer->MinimizeOn(); |
| 973 |
|
| 974 |
// Create the Command observer and register it with the optimizer. |
| 975 |
// |
| 976 |
// CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New(); |
| 977 |
// optimizer->AddObserver( itk::IterationEvent(), observer ); |
| 978 |
|
| 979 |
registration = RegistrationType::New(); |
| 980 |
|
| 981 |
registration->SetNumberOfLevels(3); |
| 982 |
registration->SetFixedImagePyramid(fixedImagePyramid); |
| 983 |
registration->SetMovingImagePyramid(movingImagePyramid); |
| 984 |
registration->SetMetric(metric); |
| 985 |
registration->SetOptimizer(optimizer); |
| 986 |
registration->SetInterpolator(interpolator); |
| 987 |
registration->SetFixedImage(fixed_image); |
| 988 |
registration->SetMovingImage(moving_image); |
| 989 |
registration->SetFixedImageRegion(fixed_image->GetLargestPossibleRegion()); |
| 990 |
registration->SetTransform(transform_a); // reset the parameter |
| 991 |
// registration->SetInitialTransformParameters(para_pre ); |
| 992 |
registration->SetInitialTransformParameters(para0); |
| 993 |
|
| 994 |
std::cout << "reset initial transform parameters" << std::endl; |
| 995 |
std::cout << "para_pre: " << para0 << std::endl; |
| 996 |
// std::cout << "transform_a: " << transform_a << std::endl; |
| 997 |
|
| 998 |
rval = get_cost_value_mmi(fixed_image, moving_image, para0, center, transform_a); |
| 999 |
std::cout << "init measure value: rval = " << rval << std::endl; |
| 1000 |
|
| 1001 |
// rval = optimizer->GetValue(para0); |
| 1002 |
// rval = metric->GetValue(para0); |
| 1003 |
|
| 1004 |
bool bsuc = 1; |
| 1005 |
try |
| 1006 |
{ |
| 1007 |
registration->Update(); |
| 1008 |
} |
| 1009 |
catch (const itk::ExceptionObject & err) |
| 1010 |
{ |
| 1011 |
std::cout << "ExceptionObject caught !" << std::endl; |
| 1012 |
std::cout << err << std::endl; |
| 1013 |
bsuc = 0; |
| 1014 |
// std::exception(); |
| 1015 |
} |
| 1016 |
|
| 1017 |
if (bsuc) |
| 1018 |
{ |
| 1019 |
// OptimizerType::ParametersType |
| 1020 |
// finalParameters = registration->GetLastTransformParameters(); |
| 1021 |
para1 = registration->GetLastTransformParameters(); |
| 1022 |
// para1 = finalParameters; |
| 1023 |
// rval = optimizer->GetValue(para1); |
| 1024 |
// rval = metric->GetValue(para1); |
| 1025 |
// rval = registration->GetMetric()->GetValue(para1); |
| 1026 |
|
| 1027 |
rval = get_cost_value_mmi(fixed_image, moving_image, para1, center, transform_a); |
| 1028 |
// double rval2 = optimizer->GetValue(); |
| 1029 |
// std::cout << "measure value: rval2 = " << rval2 << std::endl; |
| 1030 |
} |
| 1031 |
else |
| 1032 |
{ |
| 1033 |
// register failed |
| 1034 |
para1 = para0; // registration->GetLastTransformParameters(); |
| 1035 |
rval = get_cost_value_mmi(fixed_image, moving_image, para1, center, transform_a); |
| 1036 |
} |
| 1037 |
|
| 1038 |
std::cout << "final affine2d registration para :" << para1 << std::endl; |
| 1039 |
std::cout << "use iteration: " << optimizer->GetNumberOfIterations() << std::endl; |
| 1040 |
std::cout << "measure value: rval = " << rval << std::endl; |
| 1041 |
std::cout << "finish register..." << std::endl; |
| 1042 |
|
| 1043 |
return bsuc; |
| 1044 |
} |
| 1045 |
|
| 1046 |
// template<typename ImagePointerType, typename MaskImagePointerType, typename TransformPointerType> |
| 1047 |
// void compute_single_affine_transform_2d(ImagePointerType I_fixed, ImagePointerType I_moving, MaskImagePointerType |
| 1048 |
// mask_fixed, TransformPointerType &transform){ |
| 1049 |
|
| 1050 |
template <typename ImagePointerType, typename MaskImagePointerType, typename TransformPointerType> |
| 1051 |
void |
| 1052 |
compute_single_affine_transform_2d(ImagePointerType I_fixed, |
| 1053 |
ImagePointerType I_moving, |
| 1054 |
MaskImagePointerType mask_fixed, |
| 1055 |
TransformPointerType & transform, |
| 1056 |
TransformPointerType & transform_initial) |
| 1057 |
{ |
| 1058 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 1059 |
const int ImageDimension = ImageType::ImageDimension; |
| 1060 |
typedef typename ImageType::IOPixelType PixelType; |
| 1061 |
typedef typename MaskImagePointerType::ObjectType MaskImageType; |
| 1062 |
typedef typename MaskImageType::IOPixelType MaskPixelType; |
| 1063 |
|
| 1064 |
typedef typename TransformPointerType::ObjectType TransformType; |
| 1065 |
typedef typename TransformType::ParametersType ParaType; |
| 1066 |
|
| 1067 |
// option parameters |
| 1068 |
int number_of_seeds = 0; |
| 1069 |
int number_of_iteration = 500; |
| 1070 |
int MI_bins = 32; |
| 1071 |
int MI_samples = 6000; |
| 1072 |
unsigned int time_seed = (unsigned int)time(nullptr); |
| 1073 |
srand(time_seed); |
| 1074 |
// TODO: need to fix here |
| 1075 |
bool b_use_mask = 0; // (mask_fixed == nullptr); |
| 1076 |
|
| 1077 |
std::cout << "number_of_seeds: " << number_of_seeds << std::endl; |
| 1078 |
std::cout << "rand_time_seed: " << time_seed << std::endl; |
| 1079 |
std::cout << "number_of_iteration: " << number_of_iteration << std::endl; |
| 1080 |
std::cout << "MI_bins: " << MI_bins << std::endl; |
| 1081 |
std::cout << "MI_samples: " << MI_samples << std::endl; |
| 1082 |
std::cout << "use mask: " << b_use_mask << std::endl; |
| 1083 |
|
| 1084 |
// memory of searched results |
| 1085 |
typedef SEARCH_POINT_TYPE<ParaType, ImageDimension> SEARCH_POINT; |
| 1086 |
typedef std::vector<SEARCH_POINT> SEARCH_LIST; |
| 1087 |
|
| 1088 |
SEARCH_LIST search_list; |
| 1089 |
|
| 1090 |
// typedef itk::ImageMaskSpatialObject<ImageDimension, MaskPixelType> ImageMaskSpatialObject; |
| 1091 |
typedef typename itk::ImageMaskSpatialObject<ImageDimension> ImageMaskSpatialObject; |
| 1092 |
typename ImageMaskSpatialObject::Pointer mask_fixed_object = 0; |
| 1093 |
if (b_use_mask) |
| 1094 |
{ |
| 1095 |
typedef typename itk::Image<unsigned char, ImageDimension> CharMaskImageType; |
| 1096 |
typedef typename itk::CastImageFilter<MaskImageType, CharMaskImageType> CastFilterType; |
| 1097 |
typename CastFilterType::Pointer cast_filter = CastFilterType::New(); |
| 1098 |
cast_filter->SetInput(mask_fixed); |
| 1099 |
cast_filter->Update(); |
| 1100 |
typename CharMaskImageType::Pointer mask_fixed_char = cast_filter->GetOutput(); |
| 1101 |
|
| 1102 |
mask_fixed_object = ImageMaskSpatialObject::New(); |
| 1103 |
mask_fixed_object->SetImage(mask_fixed_char); |
| 1104 |
} |
| 1105 |
|
| 1106 |
typename ImageType::PointType origin; |
| 1107 |
origin.Fill(0); |
| 1108 |
I_moving->SetOrigin(origin); |
| 1109 |
I_fixed->SetOrigin(origin); |
| 1110 |
|
| 1111 |
typename TransformType::Pointer trans = TransformType::New(); |
| 1112 |
ParaType para0(trans->GetNumberOfParameters()), para1(trans->GetNumberOfParameters()); |
| 1113 |
|
| 1114 |
double rval; |
| 1115 |
SEARCH_POINT spt; |
| 1116 |
|
| 1117 |
typedef typename itk::CenteredRigid2DTransform<double> TransformType_Rigid2D; |
| 1118 |
ParaType para_cxy(TransformType_Rigid2D::New()->GetNumberOfParameters()); // translated from pre registration |
| 1119 |
|
| 1120 |
// find initial center |
| 1121 |
bool is_ok = false; |
| 1122 |
itk::Point<double, 2> center; |
| 1123 |
|
| 1124 |
if (transform_initial.IsNull()) |
| 1125 |
{ |
| 1126 |
// if (1) { |
| 1127 |
is_ok = register_image_cxy(I_fixed, I_moving, para_cxy, rval); |
| 1128 |
if (!is_ok) |
| 1129 |
{ |
| 1130 |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl << "initial affine registeration falied" << std::endl; |
| 1131 |
std::exception(); |
| 1132 |
} |
| 1133 |
|
| 1134 |
center[0] = para_cxy[1]; |
| 1135 |
center[1] = para_cxy[2]; |
| 1136 |
|
| 1137 |
std::cout << "is_ok=" << is_ok << "para_cxy:" << para_cxy << std::endl; |
| 1138 |
} |
| 1139 |
else |
| 1140 |
{ |
| 1141 |
center[0] = transform_initial->GetCenter()[0]; |
| 1142 |
center[1] = transform_initial->GetCenter()[1]; |
| 1143 |
std::cout << "input transform: " << transform_initial << std::endl; |
| 1144 |
} |
| 1145 |
|
| 1146 |
std::cout << "initial center: (" << center[0] << "," << center[1] << ")" << std::endl; |
| 1147 |
for (int n = 0; (number_of_seeds > 0) ? (n < number_of_seeds) : (n <= number_of_seeds); n++) |
| 1148 |
{ |
| 1149 |
if (n == 0) |
| 1150 |
{ |
| 1151 |
// if (1) { |
| 1152 |
if (transform_initial.IsNull()) |
| 1153 |
{ |
| 1154 |
para0[0] = 0; // para1[0]; // theta |
| 1155 |
para0[1] = 1.0; // s1 |
| 1156 |
para0[2] = 1.0; // s2 |
| 1157 |
para0[3] = 0.0; // k |
| 1158 |
para0[4] = center[0]; // para1[1]; //c1 |
| 1159 |
para0[5] = center[1]; // para1[2]; //c2 |
| 1160 |
para0[6] = para_cxy[3]; // 0;//para1[3]; //t1 |
| 1161 |
para0[7] = para_cxy[4]; // 0; //para1[4]; //t2 |
| 1162 |
|
| 1163 |
std::cout << "ABC: " << std::endl; |
| 1164 |
} |
| 1165 |
else |
| 1166 |
{ |
| 1167 |
for (unsigned int i = 0; i < transform_initial->GetParameters().Size(); i++) |
| 1168 |
{ |
| 1169 |
para0[i] = transform_initial->GetParameters()[i]; |
| 1170 |
} |
| 1171 |
std::cout << "DEF: " << std::endl; |
| 1172 |
} |
| 1173 |
} |
| 1174 |
else |
| 1175 |
{ |
| 1176 |
generate_search_seed_2d(search_list, para0); |
| 1177 |
} |
| 1178 |
|
| 1179 |
// main registration using affine transform |
| 1180 |
is_ok = register_image_affine2d_mres_mask( |
| 1181 |
I_fixed, I_moving, mask_fixed_object, para0, center, number_of_iteration, MI_bins, MI_samples, para1, rval); |
| 1182 |
if (!is_ok) |
| 1183 |
{ |
| 1184 |
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl |
| 1185 |
<< "affine registration failed!" << std::endl |
| 1186 |
<< "use the initial parameters" << std::endl; |
| 1187 |
// return -1; |
| 1188 |
} |
| 1189 |
|
| 1190 |
spt.para0 = para0; |
| 1191 |
spt.para1 = para1; |
| 1192 |
spt.center = center; |
| 1193 |
spt.rval = rval; |
| 1194 |
spt.index = n; |
| 1195 |
spt.number_of_iteration = number_of_iteration; |
| 1196 |
|
| 1197 |
std::cout << "para0: " << para0 << std::endl; |
| 1198 |
std::cout << "para1: " << para1 << std::endl; |
| 1199 |
std::cout << "center: " << center << std::endl; |
| 1200 |
std::cout << "rval: " << rval << std::endl; |
| 1201 |
std::cout << "add the search result to the list ... seed [" << n << "]" << std::endl << std::endl; |
| 1202 |
|
| 1203 |
add_search_point(search_list, spt); |
| 1204 |
} |
| 1205 |
get_best_search_point(search_list, spt); |
| 1206 |
|
| 1207 |
std::cout << std::endl << "History: " << std::endl; |
| 1208 |
for (int ii = 0; ii < search_list.size(); ii++) |
| 1209 |
{ |
| 1210 |
std::cout << "[" << ii << "]: " << search_list[ii].rval << std::endl; |
| 1211 |
std::cout << "center: " << search_list[ii].center << std::endl; |
| 1212 |
std::cout << "para0: " << search_list[ii].para0 << std::endl; |
| 1213 |
std::cout << "para1: " << search_list[ii].para1 << std::endl; |
| 1214 |
} |
| 1215 |
|
| 1216 |
typename TransformType::Pointer transform_final = TransformType::New(); |
| 1217 |
transform_final->SetParameters(spt.para1); |
| 1218 |
transform_final->SetCenter(center); |
| 1219 |
|
| 1220 |
std::cout << "final transform parameters = " << transform_final->GetParameters() << std::endl; |
| 1221 |
|
| 1222 |
transform = transform_final; |
| 1223 |
} |
| 1224 |
|
| 1225 |
template <typename ImagePointerType, typename MaskImagePointerType, typename TransformPointerType> |
| 1226 |
void |
| 1227 |
compute_single_affine_transform(ImagePointerType fixedImage, |
| 1228 |
ImagePointerType movingImage, |
| 1229 |
MaskImagePointerType maskImage, |
| 1230 |
TransformPointerType & transform, |
| 1231 |
TransformPointerType & transform_initial) |
| 1232 |
{ |
| 1233 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 1234 |
const int ImageDimension = ImageType::ImageDimension; |
| 1235 |
typedef typename ImageType::IOPixelType PixelType; |
| 1236 |
typedef typename MaskImagePointerType::ObjectType MaskImageType; |
| 1237 |
typedef typename MaskImageType::IOPixelType MaskPixelType; |
| 1238 |
typedef typename TransformPointerType::ObjectType TransformType; |
| 1239 |
|
| 1240 |
std::cout << "transform_initial: IsNotNull():" << transform_initial.IsNotNull() << std::endl; |
| 1241 |
|
| 1242 |
if (ImageDimension == 2) |
| 1243 |
{ |
| 1244 |
typedef itk::ANTSCenteredAffine2DTransform<double> RunningAffineTransformType; |
| 1245 |
RunningAffineTransformType::Pointer transform_running = RunningAffineTransformType::New(); |
| 1246 |
RunningAffineTransformType::Pointer transform_running_initial; // = RunningAffineTransformType::New(); |
| 1247 |
|
| 1248 |
std::cout << "1: transform_running_initial: IsNotNull():" << transform_running_initial.IsNotNull() << std::endl; |
| 1249 |
|
| 1250 |
if (transform_initial.IsNotNull()) |
| 1251 |
{ |
| 1252 |
transform_running_initial->SetCenter(*(reinterpret_cast<typename RunningAffineTransformType::InputPointType *>( |
| 1253 |
const_cast<typename TransformType::InputPointType *>(&(transform_initial->GetCenter()))))); |
| 1254 |
transform_running_initial->SetMatrix(*(reinterpret_cast<typename RunningAffineTransformType::MatrixType *>( |
| 1255 |
const_cast<typename TransformType::MatrixType *>(&(transform_initial->GetMatrix()))))); |
| 1256 |
transform_running_initial->SetTranslation( |
| 1257 |
*(reinterpret_cast<typename RunningAffineTransformType::OutputVectorType *>( |
| 1258 |
const_cast<typename TransformType::OutputVectorType *>(&(transform_initial->GetTranslation()))))); |
| 1259 |
} |
| 1260 |
|
| 1261 |
// Use type casting |
| 1262 |
typedef typename itk::Image<PixelType, 2>::Pointer R_ImagePointerType; |
| 1263 |
R_ImagePointerType & R_fixedImage = reinterpret_cast<R_ImagePointerType &>(fixedImage); |
| 1264 |
R_ImagePointerType & R_movingImage = reinterpret_cast<R_ImagePointerType &>(movingImage); |
| 1265 |
R_ImagePointerType & R_maskImage = reinterpret_cast<R_ImagePointerType &>(maskImage); |
| 1266 |
|
| 1267 |
std::cout << "2: transform_running_initial: IsNotNull():" << transform_running_initial.IsNotNull() << std::endl; |
| 1268 |
|
| 1269 |
compute_single_affine_transform_2d( |
| 1270 |
R_fixedImage, R_movingImage, R_maskImage, transform_running, transform_running_initial); |
| 1271 |
|
| 1272 |
// TODO: |
| 1273 |
transform->SetCenter(*(reinterpret_cast<typename TransformType::InputPointType *>( |
| 1274 |
const_cast<typename RunningAffineTransformType::InputPointType *>(&(transform_running->GetCenter()))))); |
| 1275 |
transform->SetTranslation(*(reinterpret_cast<typename TransformType::OutputVectorType *>( |
| 1276 |
const_cast<typename RunningAffineTransformType::OutputVectorType *>(&(transform_running->GetTranslation()))))); |
| 1277 |
transform->SetMatrix(*(reinterpret_cast<typename TransformType::MatrixType *>( |
| 1278 |
const_cast<typename RunningAffineTransformType::MatrixType *>(&(transform_running->GetMatrix()))))); |
| 1279 |
|
| 1280 |
// transform->SetFixedParameters(transform_running->GetFixedParameters()); |
| 1281 |
// transform->SetParameters(transform_running->GetParameters()); |
| 1282 |
} |
| 1283 |
else if (ImageDimension == 3) |
| 1284 |
{ |
| 1285 |
typedef itk::ANTSAffine3DTransform<double> RunningAffineTransformType; |
| 1286 |
RunningAffineTransformType::Pointer transform_running = RunningAffineTransformType::New(); |
| 1287 |
RunningAffineTransformType::Pointer transform_running_initial = RunningAffineTransformType::New(); |
| 1288 |
; |
| 1289 |
// compute_single_affine_transform_3d(fixedImage, movingImage, maskImage, transform_running); |
| 1290 |
|
| 1291 |
if (transform_initial.IsNotNull()) |
| 1292 |
{ |
| 1293 |
transform_running_initial->SetCenter(*(reinterpret_cast<typename RunningAffineTransformType::InputPointType *>( |
| 1294 |
const_cast<typename TransformType::InputPointType *>(&(transform_initial->GetCenter()))))); |
| 1295 |
transform_running_initial->SetMatrix(*(reinterpret_cast<typename RunningAffineTransformType::MatrixType *>( |
| 1296 |
const_cast<typename TransformType::MatrixType *>(&(transform_initial->GetMatrix()))))); |
| 1297 |
transform_running_initial->SetTranslation( |
| 1298 |
*(reinterpret_cast<typename RunningAffineTransformType::OutputVectorType *>( |
| 1299 |
const_cast<typename TransformType::OutputVectorType *>(&(transform_initial->GetTranslation()))))); |
| 1300 |
} |
| 1301 |
|
| 1302 |
// Use type casting |
| 1303 |
typedef typename itk::Image<PixelType, 3>::Pointer R_ImagePointerType; |
| 1304 |
R_ImagePointerType & R_fixedImage = reinterpret_cast<R_ImagePointerType &>(fixedImage); |
| 1305 |
R_ImagePointerType & R_movingImage = reinterpret_cast<R_ImagePointerType &>(movingImage); |
| 1306 |
R_ImagePointerType & R_maskImage = reinterpret_cast<R_ImagePointerType &>(maskImage); |
| 1307 |
|
| 1308 |
compute_single_affine_transform_3d( |
| 1309 |
R_fixedImage, R_movingImage, R_maskImage, transform_running, transform_running_initial); |
| 1310 |
|
| 1311 |
// TODO: |
| 1312 |
transform->SetCenter(*(reinterpret_cast<typename TransformType::InputPointType *>( |
| 1313 |
const_cast<typename RunningAffineTransformType::InputPointType *>(&(transform_running->GetCenter()))))); |
| 1314 |
transform->SetTranslation(*(reinterpret_cast<typename TransformType::OutputVectorType *>( |
| 1315 |
const_cast<typename RunningAffineTransformType::OutputVectorType *>(&(transform_running->GetTranslation()))))); |
| 1316 |
transform->SetMatrix(*(reinterpret_cast<typename TransformType::MatrixType *>( |
| 1317 |
const_cast<typename RunningAffineTransformType::MatrixType *>(&(transform_running->GetMatrix()))))); |
| 1318 |
|
| 1319 |
// transform->SetFixedParameters(transform_running->GetFixedParameters()); |
| 1320 |
// transform->SetParameters(transform_running->GetParameters()); |
| 1321 |
} |
| 1322 |
else |
| 1323 |
{ |
| 1324 |
std::cout << "Unsupported, not 2D/ 3D" << std::endl; |
| 1325 |
return; |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
template <typename DisplacementFieldPointerType> |
| 1330 |
void |
| 1331 |
create_deformation_field_byref(const DisplacementFieldPointerType & ref, DisplacementFieldPointerType & field) |
| 1332 |
{ |
| 1333 |
typedef typename DisplacementFieldPointerType::ObjectType DisplacementFieldType; |
| 1334 |
// field = DisplacementFieldType::New(); |
| 1335 |
|
| 1336 |
typename DisplacementFieldType::RegionType region; |
| 1337 |
region.SetSize(ref->GetLargestPossibleRegion().GetSize()); |
| 1338 |
region.SetIndex(ref->GetLargestPossibleRegion().GetIndex()); |
| 1339 |
field->SetRegions(region); |
| 1340 |
field->SetSpacing(ref->GetSpacing()); |
| 1341 |
field->SetOrigin(ref->GetOrigin()); |
| 1342 |
field->AllocateInitialized(); |
| 1343 |
} |
| 1344 |
|
| 1345 |
// compose affine transform (in a matrix format A: (Ax+b)) with a deformation field F: |
| 1346 |
// the new field is: F_new (x) = F ( A (x) ) |
| 1347 |
// output should be allocated outside |
| 1348 |
template <typename TransformPointerType, typename DisplacementFieldPointerType> |
| 1349 |
void |
| 1350 |
compose_affine_with_field(const TransformPointerType & aff, |
| 1351 |
const DisplacementFieldPointerType & field, |
| 1352 |
DisplacementFieldPointerType & field_output) |
| 1353 |
{ |
| 1354 |
typedef typename DisplacementFieldPointerType::ObjectType DisplacementFieldType; |
| 1355 |
typedef itk::ImageRegionIteratorWithIndex<DisplacementFieldType> FieldIterator; |
| 1356 |
typedef typename DisplacementFieldType::IndexType IndexType; |
| 1357 |
typedef typename DisplacementFieldType::PointType PointType; |
| 1358 |
typedef typename DisplacementFieldType::PixelType VectorType; |
| 1359 |
|
| 1360 |
const unsigned int ImageDimension = DisplacementFieldType::ImageDimension; |
| 1361 |
|
| 1362 |
// PointType zeroorigin; |
| 1363 |
// zeroorigin.Fill(0); |
| 1364 |
// field->SetOrigin(zeroorigin); |
| 1365 |
// field_output->SetOrigin(zeroorigin); |
| 1366 |
|
| 1367 |
PointType pointIn1; |
| 1368 |
PointType pointIn2; |
| 1369 |
PointType pointIn3; |
| 1370 |
|
| 1371 |
// iterate through field_output finding the points that it maps to via field. |
| 1372 |
// then take the difference from the original point and put it in the output field. |
| 1373 |
// std::cout << " begin iteration " << std::endl; |
| 1374 |
FieldIterator iter_field(field, field->GetLargestPossibleRegion()); |
| 1375 |
|
| 1376 |
// std::cout << field_output->GetLargestPossibleRegion() << std::endl; |
| 1377 |
|
| 1378 |
int cnt = 0; |
| 1379 |
for (iter_field.GoToBegin(); !iter_field.IsAtEnd(); ++iter_field) |
| 1380 |
{ |
| 1381 |
IndexType index = iter_field.GetIndex(); |
| 1382 |
field_output->TransformIndexToPhysicalPoint(index, pointIn1); |
| 1383 |
VectorType disp = iter_field.Get(); |
| 1384 |
for (int jj = 0; jj < ImageDimension; jj++) |
| 1385 |
{ |
| 1386 |
pointIn2[jj] = disp[jj] + pointIn1[jj]; |
| 1387 |
} |
| 1388 |
|
| 1389 |
// use affine transform |
| 1390 |
pointIn3 = aff->TransformPoint(pointIn2); |
| 1391 |
|
| 1392 |
VectorType out; |
| 1393 |
for (int jj = 0; jj < ImageDimension; jj++) |
| 1394 |
{ |
| 1395 |
out[jj] = pointIn3[jj] - pointIn1[jj]; |
| 1396 |
} |
| 1397 |
|
| 1398 |
field_output->SetPixel(iter_field.GetIndex(), out); |
| 1399 |
} |
| 1400 |
|
| 1401 |
// std::cout << " end iteration " << std::endl; |
| 1402 |
} |
| 1403 |
|
| 1404 |
// this is obsolet, use itkWarpImageWAffineFilter |
| 1405 |
template <typename ImagePointerType, typename DisplacementFieldPointerType> |
| 1406 |
void |
| 1407 |
warp_image_field(const ImagePointerType & img_input, |
| 1408 |
const DisplacementFieldPointerType & field, |
| 1409 |
ImagePointerType & img_output) |
| 1410 |
{ |
| 1411 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 1412 |
typedef typename DisplacementFieldPointerType::ObjectType DisplacementFieldType; |
| 1413 |
|
| 1414 |
typedef typename itk::WarpImageFilter<ImageType, ImageType, DisplacementFieldType> WarperType; |
| 1415 |
typename WarperType::Pointer warper = WarperType::New(); |
| 1416 |
|
| 1417 |
warper->SetInput(img_input); |
| 1418 |
warper->SetDisplacementField(field); |
| 1419 |
warper->SetEdgePaddingValue(0); |
| 1420 |
warper->SetOutputSpacing(field->GetSpacing()); |
| 1421 |
warper->SetOutputOrigin(field->GetOrigin()); |
| 1422 |
warper->Update(); |
| 1423 |
|
| 1424 |
img_output = warper->GetOutput(); |
| 1425 |
} |
| 1426 |
|
| 1427 |
template <typename ImageTypePointer, typename RefImageTypePointer, typename TransformPointerType> |
| 1428 |
void |
| 1429 |
affine_image(const ImageTypePointer & input_image, |
| 1430 |
const TransformPointerType & transform, |
| 1431 |
const RefImageTypePointer & ref_image, |
| 1432 |
ImageTypePointer & img_aff) |
| 1433 |
{ |
| 1434 |
typedef typename ImageTypePointer::ObjectType ImageType; |
| 1435 |
typedef typename TransformPointerType::ObjectType TransformType; |
| 1436 |
|
| 1437 |
// apply the transform |
| 1438 |
typedef itk::ResampleImageFilter<ImageType, ImageType> ResampleFilterType; |
| 1439 |
typename ResampleFilterType::Pointer resampler = ResampleFilterType::New(); |
| 1440 |
|
| 1441 |
typedef itk::LinearInterpolateImageFunction<ImageType, double> InterpolatorType; |
| 1442 |
typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); |
| 1443 |
resampler->SetInterpolator(interpolator); |
| 1444 |
resampler->SetInput(input_image); |
| 1445 |
resampler->SetSize(ref_image->GetLargestPossibleRegion().GetSize()); |
| 1446 |
resampler->SetOutputOrigin(ref_image->GetOrigin()); |
| 1447 |
resampler->SetOutputSpacing(ref_image->GetSpacing()); |
| 1448 |
resampler->SetDefaultPixelValue(0); |
| 1449 |
resampler->SetTransform(transform); |
| 1450 |
|
| 1451 |
resampler->Update(); |
| 1452 |
img_aff = resampler->GetOutput(); |
| 1453 |
} |
| 1454 |
|
| 1455 |
template <typename ImagePointerType, typename TransformPointerType, typename DisplacementFieldPointerType> |
| 1456 |
void |
| 1457 |
warp_image_field_waffine(const ImagePointerType & img_input, |
| 1458 |
const TransformPointerType & aff, |
| 1459 |
const DisplacementFieldPointerType & field, |
| 1460 |
ImagePointerType & img_output) |
| 1461 |
{ |
| 1462 |
// TODO: add a new WarpImageFilter to support affine as an input |
| 1463 |
// temporary solution: |
| 1464 |
typedef typename DisplacementFieldPointerType::ObjectType DisplacementFieldType; |
| 1465 |
typename DisplacementFieldType::Pointer field_comp = DisplacementFieldType::New(); |
| 1466 |
|
| 1467 |
// create_deformation_field_byref(field, field_comp); |
| 1468 |
// compose_affine_with_field(aff, field, field_comp); |
| 1469 |
// warp_image_field(img_input, field_comp, img_output); |
| 1470 |
|
| 1471 |
typedef typename TransformPointerType::ObjectType TransformType; |
| 1472 |
typedef typename ImagePointerType::ObjectType ImageType; |
| 1473 |
typedef itk::WarpImageWAffineFilter<ImageType, ImageType, DisplacementFieldType, TransformType> WarperType; |
| 1474 |
typename WarperType::Pointer warper = WarperType::New(); |
| 1475 |
|
| 1476 |
warper->SetInput(img_input); |
| 1477 |
warper->SetDisplacementField(field); |
| 1478 |
warper->SetAffineTransform(aff); |
| 1479 |
warper->SetEdgePaddingValue(0); |
| 1480 |
warper->SetOutputSpacing(field->GetSpacing()); |
| 1481 |
warper->SetOutputOrigin(field->GetOrigin()); |
| 1482 |
warper->SetOutputSize(field->GetLargestPossibleRegion().GetSize()); |
| 1483 |
warper->Update(); |
| 1484 |
|
| 1485 |
img_output = warper->GetOutput(); |
| 1486 |
return; |
| 1487 |
} |
| 1488 |
|
| 1489 |
// TODO: use my own code to implement all the optimization codes |
| 1490 |
|
| 1491 |
#endif /*ANTS_AFFINE_REGISTRATION_H_*/ |