# How to find position, scale and rotation of a known shape/contour in an image?

I have an image of an object. The image shows a high contrast outline of the object. It is guaranteed that the image shows exactly one object.

The shape/contour of that outline is known. It is not simple (e.g. a circle) but not too complex either. It is made up of (circular) arcs and straight lines. Think of the outline of the head of mickey mouse in terms of complexity.

Given the image and the knowledge about the shape, how do I determine the position, rotation and scale of the shape and thus the object in the image?

I did some research and Hough transforms seem to be the way to go, but only for lines.

The wikipedia article states that

Altering the algorithm to detect circular shapes instead of lines is relatively straightforward.

I have the impression that as long as I can describe the shape mathematically, I can adopt the algorithm to that shape, because the mathematical model is what defines the transformation. Is that impression (while very rough) correct?

The article states further

For more complicated shapes in the plane (i.e., shapes that cannot be represented analytically in some 2D space), the Generalised Hough transform  is used, which allows a feature to vote for a particular position, orientation and/or scaling of the shape using a predefined look-up table.

Is my Mickey Mouse-shape a complicated shape? Do I need the Generalised Hough transform?

Am I on the right track with this hough transform?

I found an implementation in openCV, which lacks scale detection:

finds arbitrary template in the grayscale image using Generalized Hough Transform

Detects position, translation and rotation

Basically speaking, I have an image and some shape description (think of a vector graphic of the outline) and I want to know where it is in the image. Does something like that exists?

I cannot share the image.

Here's something that should work:

1) Find the center of mass of all your points.

2) Sweep a radius around this center.

3) Measure a few metrics along each radius, generating a set of "periodic signals"

4) Take the DFT of the signals to determine magnification and rotation (as phase shifts).

Off the top of my head and similar to this:

Auto Detection of Rotation Angle on Arbitrary Image with Orthogonal Features

Actually, you shouldn't need to take the whole DFT, just the DC bin and bin 1, perhaps bin 2 for some redundancy.

Some metrics might be:

2. Point weighted average along the radius.

2A) weighted by distance to the center

2B) weighted by distance squared

1. Minimum distance to a point along the radius

2. Maximum distance for a point along the radius

3. The average of #3 and #4

• This is not exactly what I'm going to do, but coming up with a custom solution instead of finding an existing do-it-all algorithm seems to be the way to go. Jan 10, 2020 at 8:47
• @user11398730 Agreed. A fixed size wrench is less of a knuckle buster than an adjustable one. Can you give a hint at what you are going to do? Jan 10, 2020 at 15:18
• Eventually the goal is to measure shape deviation. This recognition step merely serves as the starting point to find the transformation of the coordinate system of the shape in the image. With that reference established, more specific measurements are taken. Jan 13, 2020 at 7:40
• @user11398730 You may have done all the measuring you need. It is much simpler to align the metric functions by shifting than it is to rotate your graphic. After shifting, you have a location and rotation independent measure of your figure in polar form. Any distortions should be readily recognizable in direct comparisons. Jan 14, 2020 at 0:53

A Hough transform can certainly work. However, with position, scale, and orientation, you would have to use a 5-D Hough transform (2 DOF for position, 1 for scale, 1 for orientation), which might be computationally quite demanding.

As an alternative, you might want to look at SIFT or one of its extensions like SURF. It sounds like they can be adapted to do the job. And OpenCV has them.

• "computationally quite demanding" - I suspected that, thank you for the confirmation Jan 10, 2020 at 8:30