Skip to content

Commit

Permalink
ENH: Add BlockMatching registration Python wrappings + example
Browse files Browse the repository at this point in the history
  • Loading branch information
tbirdso committed Jul 16, 2021
1 parent fae328c commit 3beeb9c
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env python

# 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.

import os
from urllib.request import urlretrieve

import itk

# Verify wrapping succeeded
assert 'MultiResolutionFixedBlockRadiusCalculator' in dir(itk.Ultrasound)

# Get image input for registration
FIXED_IMAGE_PATH = 'Input/rf_pre15.mha'
MOVING_IMAGE_PATH = 'Input/rf_post15.mha'

if not os.path.exists(FIXED_IMAGE_PATH):
url = 'https://data.kitware.com/api/v1/file/58ee3b778d777f16d095fd8a/download'
urlretrieve(url, FIXED_IMAGE_PATH)

if not os.path.exists(MOVING_IMAGE_PATH):
url = 'https://data.kitware.com/api/v1/file/58ee3b758d777f16d095fd87/download'
urlretrieve(url, MOVING_IMAGE_PATH)

fixed_image = itk.imread(FIXED_IMAGE_PATH, itk.D)
moving_image = itk.imread(MOVING_IMAGE_PATH, itk.D)

dimension = fixed_image.GetImageDimension()
displacement_image_type = itk.Image[itk.Vector[itk.D,dimension],dimension]

block_radius_calculator = itk.Ultrasound.MultiResolutionFixedBlockRadiusCalculator[type(fixed_image)].New()
block_radius_calculator.SetRadius([12,4])

# Create schedule for iterative registration
search_region_source = itk.Ultrasound.MultiResolutionFixedSearchRegionImageSource[type(fixed_image),
type(fixed_image),
displacement_image_type].New()
pyramid_schedule = itk.Array2D[itk.UI]()
pyramid_schedule.SetSize(3,2)
pyramid_schedule.SetElement(0,0,3)
pyramid_schedule.SetElement(0,1,2)
pyramid_schedule.SetElement(1,0,2)
pyramid_schedule.SetElement(1,1,1)
pyramid_schedule.SetElement(2,0,1)
pyramid_schedule.SetElement(2,1,1)

search_region_source.SetPyramidSchedule(pyramid_schedule)
search_region_source.SetSearchRegionRadiusSchedule([50,6])
search_region_source.SetOverlapSchedule(1.0)

metric_image_filter = itk.Ultrasound.NormalizedCrossCorrelationNeighborhoodIteratorMetricImageFilter[type(fixed_image),
type(fixed_image),
type(fixed_image)].New()

level_registration_method = itk.Ultrasound.ImageRegistrationMethod[type(fixed_image),
type(fixed_image),
type(fixed_image),
displacement_image_type,
itk.D].New()
level_registration_method.SetMetricImageFilter(metric_image_filter)

# Set up the multi-resolution registration object
multi_res_registration_method = itk.Ultrasound.MultiResolutionImageRegistrationMethod[type(fixed_image),
type(fixed_image),
type(fixed_image),
displacement_image_type,
itk.D].New()
multi_res_registration_method.SetFixedImage(fixed_image)
multi_res_registration_method.SetMovingImage(moving_image)
multi_res_registration_method.SetBlockRadiusCalculator(block_radius_calculator)
multi_res_registration_method.SetSearchRegionImageSource(search_region_source)
multi_res_registration_method.SetSchedules(pyramid_schedule, pyramid_schedule)
multi_res_registration_method.SetImageRegistrationMethod(level_registration_method)

# Run the actual registration
multi_res_registration_method.Update()

# Write out results
itk.imwrite(multi_res_registration_method.GetOutput(), 'Output/rf_post15_registered.mha')
6 changes: 6 additions & 0 deletions wrapping/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ endif()

itk_wrap_module(Ultrasound)
set(WRAPPER_LIBRARY_GROUPS
itkBlockMatchingMultiResolutionFixedBlockRadiusCalculator
itkBlockMatchingMetricImageToDisplacementCalculator
itkBlockMatchingBayesianRegularizationDisplacementCalculator
itkBlockMatchingMultiResolutionFixedSearchRegionImageSource
itkBlockMatchingNormalizedCrossCorrelationNeighborhoodIteratorMetricImageFilter
itkBlockMatchingMultiResolutionImageRegistrationMethod
itkSpectra1DSupportWindowImageFilter
itkCurvilinearArraySpecialCoordinatesImage
itkFrequencyDomain1DFilterFunction
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(WRAPPER_AUTO_INCLUDE_HEADERS OFF)
itk_wrap_include("itkImage.h")

# Wrap desired class
itk_wrap_include("itkBlockMatchingBayesianRegularizationDisplacementCalculator.h")
itk_wrap_class("itk::BlockMatching::BayesianRegularizationDisplacementCalculator" POINTER)
foreach(t ${WRAP_ITK_REAL})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
itk_wrap_template("I${ITKM_${t}}${d}I${ITKM_V${ITKM_${t}}${d}}${d}"
"itk::Image<${ITKT_${t}},${d}>, itk::Image<${ITKT_V${ITKM_${t}}${d}},${d}>")
endforeach()
endforeach()
itk_end_wrap_class()
13 changes: 13 additions & 0 deletions wrapping/itkBlockMatchingMetricImageToDisplacementCalculator.wrap
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
set(WRAPPER_AUTO_INCLUDE_HEADERS OFF)

itk_wrap_include("itkBlockMatchingMetricImageToDisplacementCalculator.h")
itk_wrap_class("itk::BlockMatching::MetricImageToDisplacementCalculator" POINTER)
foreach(t ${WRAP_ITK_REAL})
foreach(t2 ${WRAP_ITK_REAL})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
itk_wrap_template("I${ITKM_${t}}${d}I${ITKM_V${t2}${d}}${d}"
"${ITKT_I${ITKM_${t}}${d}}, ${ITKT_I${ITKM_V${t2}${d}}${d}}")
endforeach()
endforeach()
endforeach()
itk_end_wrap_class()
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
set(WRAPPER_AUTO_INCLUDE_HEADERS OFF)
itk_wrap_include("itkImage.h")

itk_wrap_include("itkBlockMatchingMultiResolutionBlockRadiusCalculator.h")
itk_wrap_class("itk::BlockMatching::MultiResolutionBlockRadiusCalculator" POINTER)
foreach(t ${WRAP_ITK_SCALAR})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
itk_wrap_template("I${ITKM_${t}}${d}" "itk::Image<${ITKT_${t}},${d}>")
endforeach()
endforeach()
itk_end_wrap_class()

itk_wrap_include("itkBlockMatchingMultiResolutionFixedBlockRadiusCalculator.h")
itk_wrap_class("itk::BlockMatching::MultiResolutionFixedBlockRadiusCalculator" POINTER)
foreach(t ${WRAP_ITK_SCALAR})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
itk_wrap_template("I${ITKM_${t}}${d}" "itk::Image<${ITKT_${t}},${d}>")
endforeach()
endforeach()
itk_end_wrap_class()
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
set(WRAPPER_AUTO_INCLUDE_HEADERS OFF)

itk_wrap_include("itkVector.h")
itk_wrap_include("itkImage.h")
itk_wrap_include("itkImageRegion.h")

# Wrap for class template
itk_wrap_include("itkImageRegion.h")
itk_wrap_class("itk::Image" POINTER)
foreach(d ${ITK_WRAP_IMAGE_DIMS})
itk_wrap_template("IR${d}${d}" "itk::ImageRegion<${d}>, ${d}")
endforeach()
itk_end_wrap_class()

itk_wrap_include("itkImageSource.h")
itk_wrap_class("itk::ImageSource" POINTER)
foreach(d ${ITK_WRAP_IMAGE_DIMS})
itk_wrap_template("IIR${d}${d}" "itk::Image<itk::ImageRegion<${d}>,${d}>")
endforeach()
itk_end_wrap_class()

# Wrap class hierarchy
itk_wrap_include("itkBlockMatchingMultiResolutionSearchRegionImageSource.h")
itk_wrap_class("itk::BlockMatching::MultiResolutionSearchRegionImageSource" POINTER)
foreach(t ${WRAP_ITK_REAL})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
foreach(vt ${WRAP_ITK_VECTOR_REAL})
itk_wrap_template("I${ITKM_${t}}${d}I${ITKM_${t}}${d}I${ITKM_${vt}${d}}${d}"
"itk::Image<${ITKT_${t}},${d}>, itk::Image<${ITKT_${t}},${d}>, itk::Image<${ITKT_${vt}${d}},${d}>")
endforeach()
endforeach()
endforeach()
itk_end_wrap_class()

# Wrap class
itk_wrap_include("itkBlockMatchingMultiResolutionFixedSearchRegionImageSource.h")
itk_wrap_class("itk::BlockMatching::MultiResolutionFixedSearchRegionImageSource" POINTER)
foreach(t ${WRAP_ITK_REAL})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
foreach(vt ${WRAP_ITK_VECTOR_REAL})
itk_wrap_template("I${ITKM_${t}}${d}I${ITKM_${t}}${d}I${ITKM_${vt}${d}}${d}"
"itk::Image<${ITKT_${t}},${d}>, itk::Image<${ITKT_${t}},${d}>, itk::Image<${ITKT_${vt}${d}},${d}>")
endforeach()
endforeach()
endforeach()
itk_end_wrap_class()
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
set(WRAPPER_AUTO_INCLUDE_HEADERS OFF)
itk_wrap_include("itkPoint.h")
itk_wrap_include("itkSmartPointer.h")
itk_wrap_include("itkImage.h")
itk_wrap_include("itkImageRegion.h")

# Wrap base classes for itk::BlockMatching::MetricImageToDisplacementCalculator public members
#itk_wrap_class("itk::SmartPointer")
# foreach(t ${WRAP_ITK_REAL})
# foreach(d ${ITK_WRAP_IMAGE_DIMS})
# itk_wrap_template("I${ITKM_${t}}${d}" "itk::Image<${ITKT_${t}},${d}>")
# endforeach()
# endforeach()
#itk_end_wrap_class()

itk_wrap_class("itk::Image" POINTER)
foreach(t ${WRAP_ITK_REAL})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
itk_wrap_template("P${ITKM_${t}}${d}${d}" "itk::Point<${ITKT_${t}},${d}>, ${d}")
itk_wrap_template("SPI${ITKM_${t}}${d}${d}" "itk::SmartPointer<itk::Image<${ITKT_${t}},${d}>>, ${d}")
endforeach()
endforeach()
itk_end_wrap_class()

# Wrap class hierarchy
itk_wrap_include("itkImageToImageFilter.h")
itk_wrap_class("itk::ImageToImageFilter" POINTER)
foreach(t ${WRAP_ITK_REAL})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
itk_wrap_template("IIR${d}${d}I${ITKM_V${t}${d}}${d}"
"itk::Image<itk::ImageRegion<${d}>,${d}>, itk::Image<${ITKT_V${t}${d}},${d}>")
endforeach()
endforeach()
itk_end_wrap_class()

itk_wrap_include("itkBlockMatchingImageRegistrationMethod.h")
itk_wrap_class("itk::BlockMatching::ImageRegistrationMethod" POINTER)
foreach(t ${WRAP_ITK_REAL})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
foreach(vt ${WRAP_ITK_VECTOR_REAL})
set(img_m "I${ITKM_${t}}${d}")
set(img_t "itk::Image<${ITKT_${t}}, ${d}>")
itk_wrap_template("${img_m}${img_m}${img_m}I${ITKM_${vt}${d}}${d}"
"${img_t}, ${img_t}, ${img_t}, itk::Image<${ITKT_${vt}${d}},${d}>, ${ITKT_${t}}")
endforeach()
endforeach()
endforeach()
itk_end_wrap_class()

# Wrap target class
itk_wrap_include("itkBlockMatchingMultiResolutionImageRegistrationMethod.h")
itk_wrap_class("itk::BlockMatching::MultiResolutionImageRegistrationMethod" POINTER)
foreach(t ${WRAP_ITK_REAL})
foreach(d ${ITK_WRAP_IMAGE_DIMS})
set(img_m "I${ITKM_${t}}${d}")
set(img_t "itk::Image<${ITKT_${t}}, ${d}>")
itk_wrap_template("${img_m}${img_m}${img_m}I${ITKM_${vt}${d}}${d}"
"${img_t}, ${img_t}, ${img_t}, itk::Image<${ITKT_V${t}${d}},${d}>, ${ITKT_${t}}")
endforeach()
endforeach()
itk_end_wrap_class()
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
set(WRAPPER_AUTO_INCLUDE_HEADERS OFF)

itk_wrap_include("itkBlockMatchingMetricImageFilter.h")
itk_wrap_class("itk::BlockMatching::MetricImageFilter" POINTER)
itk_wrap_image_filter("${WRAP_ITK_REAL}" 3 2+)
itk_end_wrap_class()

itk_wrap_include("itkBlockMatchingNormalizedCrossCorrelationMetricImageFilter.h")
itk_wrap_class("itk::BlockMatching::NormalizedCrossCorrelationMetricImageFilter" POINTER)
itk_wrap_image_filter("${WRAP_ITK_REAL}" 3 2+)
itk_end_wrap_class()

itk_wrap_include("itkBlockMatchingNormalizedCrossCorrelationNeighborhoodIteratorMetricImageFilter.h")
itk_wrap_class("itk::BlockMatching::NormalizedCrossCorrelationNeighborhoodIteratorMetricImageFilter" POINTER)
itk_wrap_image_filter("${WRAP_ITK_REAL}" 3 2+)
itk_end_wrap_class()

0 comments on commit 3beeb9c

Please sign in to comment.