Skip to content

fitEllipse returns a very far ellipse #26078

@erasta

Description

@erasta

System Information

OpenCV python version: 4.10.0
Operating System / Platform: Windows 11
Python version: 3.11.9

Detailed description

When running cv2.fitEllipse on a the points below i got an ellipse that is very far and not related to them.
Subset of the points did not result with same problem.

Steps to reproduce

import cv2
import numpy as np
import matplotlib.pyplot as plt
points = [
    [1434, 308], [1434, 309], [1433, 310], [1427, 310], [1427, 312], [1426, 313], [1422, 313], [1422, 314],
    [1421, 315], [1415, 315], [1415, 316], [1414, 317], [1408, 317], [1408, 319], [1407, 320], [1403, 320], 
    [1403, 321], [1402, 322], [1396, 322], [1396, 323], [1395, 324], [1389, 324], [1389, 326], [1388, 327], 
    [1382, 327], [1382, 328], [1381, 329], [1376, 329], [1376, 330], [1375, 331], [1369, 331], [1369, 333], 
    [1368, 334], [1362, 334], [1362, 335], [1361, 336], [1359, 336], [1359, 1016], [1365, 1016], [1366, 1017], 
    [1366, 1019], [1430, 1019], [1430, 1017], [1431, 1016], [1440, 1016], [1440, 308]
]
ellipse = cv2.fitEllipse(np.array(points))
points = np.array(points)  
center, axes, angle = ellipse
ellipse_points = cv2.ellipse2Poly((int(center[0]), int(center[1])), (int(axes[0] // 2), int(axes[1] // 2)), int(angle), 0, 360, 1)
plt.figure(figsize=(8, 8))
plt.scatter(points[:, 0], points[:, 1], c='blue', label='Points')
ellipse_polygon = plt.Polygon(ellipse_points, fill=None, edgecolor='red', label='Fitted Ellipse')
plt.gca().add_patch(ellipse_polygon)
plt.gca().set_aspect('equal', adjustable='box')
plt.legend()
plt.show()

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions