Metal
Metal

Reputation: 143

How to fit an ellipse contour with 4 points?

I have 4 coordinate points. Using these 4 points I want to fit an ellipse, but seems like the requirement for cv2.fitellipse() is a minimum of 5 points. Is there any way to get around this and draw a countour with just 4 points?

How does fitellipse draw a contour when cv2.CHAIN_APPROX_SIMPLE is used, which gives only 4 coordinates point.

Upvotes: 1

Views: 4249

Answers (2)

gfkri
gfkri

Reputation: 2179

Four points aren't enough to fit an ellipse without ambiguity (don't forget that a general ellipse can have arbitrary rotation). You need at least five to get an exact solution or more to fit in a least square manner. For a more detailed explanation I found this.

You can draw the countour itself (without fitting anything) with drawContours (or fit e.g. a circle).

So to answer your second question (assuming that I understand it right): It doesn't if there are less than 5 points available, but FindContours in combination with CHAIN_APPROX_SIMPLE returns eventually more depending on the particular detected contour.

See here. In this C++ example ellipses are only fitted if at least 5 points are available.

Upvotes: 2

PSchn
PSchn

Reputation: 728

If you take a look at the equation for ellipses:

enter image description here

and the homogenous representation:

enter image description here.

The conic-matrix looks like this:

enter image description here

you see that it has 6 parameters - one projective degree of freedom, so 5 degree of freedom (if f not equal 0). Every point on C gives a condition to the parameters a-f. So five points are needed to describe a conic:

enter image description here

To solve this you need to compute the kernel of the matrix (for five points):

enter image description here

if you have more then five you need the least square solution: enter image description here

and x is the eigenvector from the smallest eigenvalue of enter image description here

Upvotes: 2

Related Questions