2

enter image description hereAn image has two ellipses ( for simple case we can consider both ellipses are same). One of ellipse is rotated and translated from the other. We have only image of these ellipses. How to estimate rotation angle ( angle between two ellipses) ?

Avi
  • 310
  • 2
  • 4
  • 16
  • Obtain major axis of both the ellipse -> Consider these axis to be lines and use the `arctan2` function or a similar function from `numpy` – Jeru Luke Jan 08 '17 at 07:32
  • if you do not know how to obtain axises see [Ellipse matching](http://stackoverflow.com/a/36054594/2521214) then `angle = acos(dot(a1,a2)/|a1|.|a2|)` where `a1,a2` are major axis vectors for each ellipse – Spektre Jan 08 '17 at 10:54
  • Thank you Jeru Luke and Spektre 5 – Avi Jan 08 '17 at 16:45

2 Answers2

1

I will present some initial pre-processing steps and then with the use of some OpenCV internal methods, we can get what you are asking for.

If the image is RGB or RGBA:
    convert it to GRAY [use cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)]
Threshold the image to get a binary image with 2 ellipses. [cv2.threshold()]
Find contours in the binary image [cv2.findContours()]
Call the cv2.fitline() on each contour to get the line equation.
Apply the formula to get the angle between 2 lines.

For more operations on contours visit http://docs.opencv.org/trunk/dd/d49/tutorial_py_contour_features.html

ZdaR
  • 22,343
  • 7
  • 66
  • 87
1

Off the top of my head, I'd do this: (considering the tags, I assume you're using opencv)

1-Use "findContours" command to get the boundaries' pixels of each ellipsis separately.

2-For each ellipsis, calculate the distance between each pairs of pixels (for all pixels in the boundary - in a dual loop) by the equation:(D=sqrt((y1-y2)^2 + (x1-x2)^2)) and find the pair that shows the most distance. This pair comprises of two ends of the major axis of the ellipsis.

3-Using two mentioned points, calculate the angle of the major axis with respect to x-axis of the image by the equation:

angle = arctan((y2-y1)/(x2-x1)) 

4-Find the angle for the other ellipsis and subtract two angles to find the angle between them.

MeiH
  • 1,763
  • 11
  • 17