# AlignTwoPolyDatas Example

Folks,

Recently, a user wanted to align two models from a longitudinal study. He did not have access to the original image data. If he had the images, he could have used 3DSlicer or Elastix. They both have state-of-the-art intensity driven registration algorithms.

VTK has several “registration” techniques: vtkLandmarkTransform,vtkIterativeClosestPointTransform, vtkThinPlateSplineTransform.
Each of these algorithms requires landmarks to exist in both the source and target models.

I created an example, AlignTwoPolyDatas that uses a vtkOBBTree to create oriented bounding boxes for each model. The example uses the corners of the bounding boxes as landmarks for the landmark transform. Then, that transform is refined with the iterative closest point transform. For the original, oriented bounding boxes and iterative closest point transform, the example computes a metric using the vtkHausdorffDistancePointSetFilter. The example picks the best of the three approaches and displays the aligned models. AlignTwoPolyDatas’ description provides more details.

Here are the results for the user’s time sequence. The technique can also align “similar” objects.

Here is the user’s data:
and a shark and the great white shark:
and finally a cow and a horse: .

Enjoy the VTKExamples,

Bill

5 Likes

The last link AlignTwoPolyDatas is referring to: vtkOBBTree

Fixed, Thanks

Perhaps https://vtk.org/doc/nightly/html/classvtkProcrustesAlignmentFilter.html for “similar” objects?

Since the orientations of the bounding boxes may differ, the AlignBoundingBoxes function tries ten different rotations. For each rotation, it computes the Hausdorff distance between the target’s OBB corners and the transformed source’s OBB corners. Finally, transform the original source using the smallest distance.

How to apply more than 10 rotation???

In the function `BestBoundingBox` change:

``````  auto delta = 90.0;
for (auto i = 0; i < 4; ++i)
{
...
``````

to suit what you want to do.

Remember also that you may need to reorient the target to get a better fit.

How to reorient the target?? sorry I am a beginner .

sir, can you send me your e-mail. That would be great help for me.

You will probably need to transform your target object so that its orientation is similar to the reference object as shown in the images above. Generally this is done using vtkTransform and vtkTransformPolyDataFilter or something similar. To get a feel for what happens, you could open Paraview load a cone source and apply the transform filter.
Here are several examples that also may be of use:

Hi there,

I am a bit new to all of this but keen to learn. I was wondering if someone can advise me on how to save the aligned surface as a vtk from this example? I prefer using the Python version. Thanks in advance.

1 Like

You can do something like this:

``````    writer = vtkPolyDataWriter()
if best_distance == distance_before_align:
writer.SetInputData(original_source_polydata)
elif best_distance == distance_after_align:
writer.SetInputData(source_polydata)
else:
writer.SetInputData(transform.GetOutput())
writer.SetFileName('AlignedSource.vtk')
writer.Write()
writer.SetInputData(tpd.GetOutput())
writer.SetFileName('Target.vtk')
writer.Write()
``````

This will give you the aligned source and target as VTK files that you could load into ParaView.

1 Like

Thank you!