Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: ENH: Add BlockMatching registration Python wrappings + example #150

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Comment on lines +7 to +16
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing the following warnings when instantiating any BlockMatching class at runtime:

Template itk::ImageToImageFilter<itk::Image<itk::Vector<double,2>,2>,itk::Image<itk::CovariantVector<double,2>,2>>
already defined as <class 'itk.itkImageToImageFilterAPython.itkImageToImageFilterIVD22ICVD22'>
is redefined as {cl}
Warning: template already defined 'itk::ImageToImageFilter<itk::Image<itk::Vector<double,2>,2>,itk::Image<itk::CovariantVector<double,2>,2>>'
Template itk::ImageToImageFilter<itk::Image<itk::Vector<double,3>,3>,itk::Image<itk::CovariantVector<double,3>,3>>
already defined as <class 'itk.itkImageToImageFilterAPython.itkImageToImageFilterIVD33ICVD33'>
is redefined as {cl}
Warning: template already defined 'itk::ImageToImageFilter<itk::Image<itk::Vector<double,3>,3>,itk::Image<itk::CovariantVector<double,3>,3>>'

The warnings are introduced with these changes, but we are not explicitly wrapping ImageToImageFilter for these templates. Any thoughts on where this could be coming from?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😕 interesting. I do not see anything via inspection, either. 👁️ I will start a local build.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thewtex Were you able to reproduce by any chance?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not reproduce these warnings locally.

I did get

itkBlockMatchingMultiResolutionImageRegistrationMethod: warning(4): ITK type not wrapped, or currently not known: itk::Image< itk::Point< double, 2 >, 2 >
itkBlockMatchingMultiResolutionImageRegistrationMethod: warning(4): ITK type not wrapped, or currently not known: itk::Image< itk::Point< double, 3 >, 3 >

at build time.

And,

>>> itk.Ultrasound.NormalizedCrossCorrelationMetricImageFilter.New()
Warning: Unknown parameter 'itk::SmartPointer<itk::Image<float,2>>' in template 'itk::Image'
Warning: Unknown parameter 'itk::SmartPointer<itk::Image<float,3>>' in template 'itk::Image'
Namespace already has a value for ImageRegistrationMethod, which is not an itkTemplate instance for class itk::BlockMatching::ImageRegistrationMethod. Overwriting old value.
Namespace already has a value for MultiResolutionImageRegistrationMethod, which is not an itkTemplate instance for class itk::BlockMatching::MultiResolutionImageRegistrationMethod. Overwriting old value.
<itk.ITKCommonBasePython.itkObject; proxy of <Swig Object of type 'itkObject *' at 0x7fa4d95d8660> >

at run time.

We could likely replace the SmartPointer's with raw pointers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, thanks for taking a look. I'll try a clean build over the weekend and see if warnings still appear on my end.

My guess is that the point wrapping warnings are related to my having ITK_WRAP_DOUBLE enabled locally, I'll take a closer look.

We could likely replace the SmartPointer's with raw pointers.
Will do. This would occur within the cxx classes as well as the wrapping files, right?


itk_wrap_include("itkBlockMatchingImageRegistrationMethod.h")
itk_wrap_class("itk::BlockMatching::ImageRegistrationMethod" POINTER)
tbirdso marked this conversation as resolved.
Show resolved Hide resolved
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()