Reputation: 143
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
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
Reputation: 728
If you take a look at the equation for ellipses:
and the homogenous representation:
The conic-matrix looks like this:
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:
To solve this you need to compute the kernel of the matrix (for five points):
if you have more then five you need the least square solution:
and x is the eigenvector from the smallest eigenvalue of
Upvotes: 2