2

What is Distance Transform?What is the theory behind it?if I have 2 similar images but in different positions, how does distance transform help in overlapping them?The results that distance transform function produce are like divided in the middle-is it to find the center of one image so that the other is overlapped just half way?I have looked into the documentation of opencv but it's still not clear.

Steph
  • 609
  • 3
  • 13
  • 32

2 Answers2

7

Look at the picture below (you may want to increase you monitor brightness to see it better). The pictures shows the distance from the red contour depicted with pixel intensities, so in the middle of the image where the distance is maximum the intensities are highest. This is a manifestation of the distance transform. Here is an immediate application - a green shape is a so-called active contour or snake that moves according to the gradient of distances from the contour (and also follows some other constraints) curls around the red outline. Thus one application of distance transform is shape processing.

Another application is text recognition - one of the powerful cues for text is a stable width of a stroke. The distance transform run on segmented text can confirm this. A corresponding method is called stroke width transform (SWT)

As for aligning two rotated shapes, I am not sure how you can use DT. You can find a center of a shape to rotate the shape but you can also rotate it about any point as well. The difference will be just in translation which is irrelevant if you run matchTemplate to match them in correct orientation.

Perhaps if you upload your images it will be more clear what to do. In general you can match them as a whole or by features (which is more robust to various deformations or perspective distortions) or even using outlines/silhouettes if they there are only a few features. Finally you can figure out the orientation of your object (if it has a dominant orientation) by running PCA or fitting an ellipse (as rotated rectangle).

cv::RotatedRect rect = cv::fitEllipse(points2D);
float angle_to_rotate = rect.angle;

enter image description here

Vlad
  • 4,425
  • 1
  • 30
  • 39
  • thanks @Vlad. No I do not need to align them.They have already been aligned. Distance transform is being used for the superimposition of the 2 images. So,if I rightly understood, the middle part of the face is brighter because its distance from the boundary is greatest? – Steph Mar 22 '14 at 02:46
  • yes and this can be used to align silhouettes. You just need to align DT of the silhouette using matchTemplate. But I am sure there are other methods to do the alignment. Showing your images is the key to understanding your task. – Vlad Mar 22 '14 at 02:50
2

The distance transform is an operation that works on a single binary image that fundamentally seeks to measure a value from every empty point (zero pixel) to the nearest boundary point (non-zero pixel).

An example is provided here and here.

The measurement can be based on various definitions, calculated discretely or precisely: e.g. Euclidean, Manhattan, or Chessboard. Indeed, the parameters in the OpenCV implementation allow some of these, and control their accuracy via the mask size.

The function can return the output measurement image (floating point) - as well as a labelled connected components image (a Voronoi diagram). There is an example of it in operation here.

I see from another question you have asked recently you are looking to register two images together. I don't think the distance transform is really what you are looking for here. If you are looking to align a set of points I would instead suggest you look at techniques like Procrustes, Iterative Closest Point, or Ransac.

Community
  • 1
  • 1
timlukins
  • 2,694
  • 21
  • 35
  • I looked at another question link that timlukins provided and recalled it posted by Steph. If your objects are in 3D or rotated out of plane you'd better mention this. ICP and Procrustes will run on 3D point cloud which you don't have. Ransac is a general method which will run with any fitting and select best inlier set. – Vlad Mar 21 '14 at 17:14
  • ICP and Procrustes are relevant up to any dimensionality. Here are some nice 2D examples: http://en.wikipedia.org/wiki/Point_set_registration http://www2.maths.ox.ac.uk/chebfun/examples/geom/html/Procrustes.shtml – timlukins Mar 21 '14 at 17:45
  • That is right. But his or ber points rotate in 3d since she has a front and profile faces in the question you linked. – Vlad Mar 21 '14 at 19:28
  • remark @timlukins: in opencv for every non-zero valued pixel in the binary image, the distance to the next zero valued pixel is computed/approximated. So zero value means non-empty pixel in `cv::distanceTransform` which might be a bit confusing – Micka Mar 21 '14 at 21:41