{"id":17272523,"url":"https://github.com/hbldh/b2ac","last_synced_at":"2025-08-04T15:33:29.242Z","repository":{"id":145583869,"uuid":"42679263","full_name":"hbldh/b2ac","owner":"hbldh","description":"Python and C implementations of an ellipse fitting algorithm in double and fixed point precision. ","archived":false,"fork":false,"pushed_at":"2024-10-29T07:45:46.000Z","size":97,"stargazers_count":22,"open_issues_count":3,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-14T08:41:36.093Z","etag":null,"topics":["ellipse-fitting","fixed-point","numpy","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hbldh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-09-17T20:08:07.000Z","updated_at":"2025-03-31T06:12:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"41dacbf5-2e0d-4174-b060-c81e0fe78dd5","html_url":"https://github.com/hbldh/b2ac","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/hbldh/b2ac","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbldh%2Fb2ac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbldh%2Fb2ac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbldh%2Fb2ac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbldh%2Fb2ac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hbldh","download_url":"https://codeload.github.com/hbldh/b2ac/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hbldh%2Fb2ac/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267362906,"owners_count":24075218,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ellipse-fitting","fixed-point","numpy","python"],"created_at":"2024-10-15T08:48:49.959Z","updated_at":"2025-07-27T13:13:13.575Z","avatar_url":"https://github.com/hbldh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# B2AC - Ellipse fitting in Python and C\n\n|  Branch       | Build status     |\n| :------------ | ---------------: |\n| `master`      | [![Build Status](https://travis-ci.org/hbldh/b2ac.svg?branch=master)](https://travis-ci.org/hbldh/b2ac) |\n| `develop`     | [![Build Status](https://travis-ci.org/hbldh/b2ac.svg?branch=develop)](https://travis-ci.org/hbldh/b2ac) |\n\nPython and C implementations of an ellipse fitting algorithm in double and fixed point precision. \n\nThe implementations here were never meant for production, but were written as an examination of how much\nspeedup could be attained from implementing ellipse fitting methods in fixed point and at what precision cost.\n\nThe ellipse fitting method in \\[2\\] (an improved version of the one in \\[1\\]) was implemented.\n\n## Installation\n\nInstall simply by calling:\n\n    pip install git+https://www.github.com/hbldh/b2ac\n    \nNumpy needs to be installed prior to `b2ac`, since it compiles the C extension using Numpy headers.\n\n## Testing\n\nTest with nosetests:\n\n    nosetests tests\n\n## Usage\n\nAn ellipse can be fitted using Python methods:\n\n```python\nimport numpy as np\nimport b2ac.preprocess\nimport b2ac.fit\nimport b2ac.conversion\n\npoints = np.array([[3, 0], [0, 5], [3, 10], [6, 5]])\n\n# Optional removal of mean value from points to obtain better\n# fitting performance, especially in integer precision. \npoints, x_mean, y_mean = b2ac.preprocess.remove_mean_values(points)\n\n# Fit using NumPy methods in double precision.\nconic_numpy = b2ac.fit.fit_improved_B2AC_numpy(points)\n# Fit using own written methods in double precision.\nconic_double = b2ac.fit.fit_improved_B2AC_double(points)\n# Fit using own written methods in 64-bit integer precision.\nconic_int = b2ac.fit.fit_improved_B2AC_int(points)\n\n# Convert from conic coefficient form to general ellipse form.\ngeneral_form_numpy = b2ac.conversion.conic_to_general_1(conic_numpy)\ngeneral_form_numpy[0][0] += x_mean\ngeneral_form_numpy[0][1] += y_mean\n\ngeneral_form_double = b2ac.conversion.conic_to_general_1(conic_double)\ngeneral_form_double[0][0] += x_mean\ngeneral_form_double[0][1] += y_mean\n\ngeneral_form_int = b2ac.conversion.conic_to_general_int(conic_int)\ngeneral_form_int[0][0] += x_mean\ngeneral_form_int[0][1] += y_mean\n\n```\n\n\u003e The mathematical notation described below follows the one used in \\[2\\].\n\n### Solution strategy\n\nFirst, the scatter matrix **S** is calculated according to regular specifications,\nand the **M** matrix as well. The inverse of **S\u003csub\u003e3\u003c/sub\u003e** is calculated through \ndefinition of [3x3 matrix inverse](http://mathworld.wolfram.com/MatrixInverse.html).\n\nA QR algorithm with largest value shift, using Givens rotations for both \ntridiagonalization and actual QR steps, (See \\[3\\]) is then used\nto find eigenvalues of the matrix **M**. With these eigenvalues, we\napply an inverse iteration method for calculating the \ncorresponding eigenvectors.\n\nThe algorithms returns the conic coefficients defining one fitted ellipse.\nThese can then be transformed to general form: center point, \naxes lengths and rotation angle.\n\n#### Special considerations for integer version\n\n\u003e The integer version uses 64 bit integers!\n\nThe calculations of **S** and **M** has special handling. The fact that\nthis method uses a lot of squared values can easily lead to overflow, especially if\n32 bit integers are used. \n\nThe major thing to remember here is that when calculating inverses,\nthe division of the determinant is postponed, thus using the determinant as\na scale factor during calculations. This scale factor can then be removed \nfirst when the sought eigenvector has been found. \n\n#### Limitations for fixed point implementation\n\nFor integer versions, it is of great importance to first remove the \nmean values from the points, thus making all values much smaller. This will\ndrastically improve the fitting results.\n\nAnother important aspect is the number of points used for estimating an\nellipse. Using `\u003e100` points, the matrix values in the estimation also grows\nand the possibility of overflow is increased. Subsampling of points is recommended.\n\n\n## References\n\n1.  [Fitzgibbon, A., Pilu, M., \u0026 Fisher, R.B.,\n    \"Direct least square fitting of ellipses,\" Pattern Analysis and Machine Intelligence,\n    IEEE Transactions on , vol.21, no.5, pp.476-480, May 1999]\n    (http://research.microsoft.com/pubs/67845/ellipse-pami.pdf)\n    \n2.  [Halir, R., \u0026 Flusser, J., \"Numerically stable direct least squares fitting of ellipses.\" \n     Proc. 6th International Conference in Central Europe on Computer Graphics and Visualization. \n     WSCG. Vol. 98. 1998](http://autotrace.sourceforge.net/WSCG98.pdf)\n\n3.  Golub, G.H. \u0026 Van Loan, C.F., 2012. Matrix Computations (4th Ed.). \n    Johns Hopkins University Press, Baltimore, MD, USA.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhbldh%2Fb2ac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhbldh%2Fb2ac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhbldh%2Fb2ac/lists"}