Skip to main content

visiometry

Subpixel-accurate dimensional measurement in images
circles · ellipses · lines · rectangles

PyPI Python License: Apache 2.0 Dependencies

visiometry measures geometric features in an image to a few thousandths of a pixel. Give it a rough position; it finds the real edge and returns the exact geometry, with the inliers, residuals and a quality flag. All tools share one simple pipeline so they behave the same way and are easy to follow.

Contents

Installation · Usage · How it works · Tools · Interactive explorer · Benchmark · Roadmap · Development

Installation

pip install visiometry            # core: numpy + scipy
pip install "visiometry[image]"   # + Pillow for PNG/JPEG/TIFF (vm.imread)
pip install "visiometry[gui]"     # + matplotlib for the interactive explorer
pip install "visiometry[image,gui]"

Python ≥ 3.10, any OS. From a local wheel or a clone:

pip install path/to/visiometry-0.1.0-py3-none-any.whl
pip install -e ".[dev,gui,image]"   # development install from the source tree

Usage

Measure a hole in your own image

import visiometry as vm

img = vm.imread("part.png")  # gray or colour, any size

res = vm.measure_circle(
    img,
    center=(520, 600),  # rough centre (x, y) in pixels
    r_min=43,  # where to look for the edge: from r_min ...
    r_max=63,  # ... to r_max
    polarity="light_to_dark",  # walking outwards: bright hole -> dark part
)

if res.ok:
    print(f"center = ({res.cx:.3f}, {res.cy:.3f}) px")
    print(f"radius = {res.radius:.3f} px, diameter = {res.diameter:.3f} px")
    print(f"rms = {res.rms:.3f} px, inliers {res.n_inliers}/{res.n_valid}")
else:
    print("measurement failed:", res.message)

res.to_dict()  # plain dict for CSV / JSON
res.edge_points  # every caliper: x, y, amplitude, valid, inlier, residual

A failed measurement does not raise: check ok and message.

All four tools

import visiometry as vm
from visiometry.synthetic import (
    make_circle_image,
    make_ellipse_image,
    make_line_image,
    make_rectangle_image,
)

img = make_circle_image((200, 200), center=(100.3, 99.7), radius=40, blur=1.2, noise=3)
print(vm.measure_circle(img, center=(101, 99), r_min=34, r_max=46))
# CircleResult(center=(100.3112, 99.7047), radius=39.9987, rms=0.0356, inliers=251/251, ok=True)

img = make_ellipse_image(
    (200, 200), center=(100.3, 99.7), axes=(60, 35), angle=20, blur=1.2, noise=3
)
print(vm.measure_ellipse(img, center=(101, 99), axes=(57, 37), angle=15))
# EllipseResult(center=(100.2968, 99.6991), a=59.9989, b=35.0001, angle=19.9874°, rms=0.0338, ...)

img = make_line_image((200, 200), point=(100.3, 99.7), angle=10, blur=1.2, noise=3)
print(vm.measure_line(img, start=(30, 87), end=(170, 112), search=8))
# LineResult(point=(99.9766, 99.6326), angle=10.0105°, rms=0.0284, inliers=71/71, ok=True)

img = make_rectangle_image(
    (200, 200), center=(100.3, 99.7), size=(110, 60), angle=15, blur=1.2, noise=3
)
box = vm.measure_rectangle(img, center=(101, 99), size=(106, 63), angle=13)
print(box)
# RectangleResult(center=(100.3085, 99.6960), size=(110.0264, 60.0066), angle=14.9818°, ...)
print(box.corners)  # top-left, top-right, bottom-right, bottom-left

More: examples/quickstart.py.

To try settings by mouse instead of code, run visiometry-gui [image] (see Interactive explorer).

How it works

Every tool follows the same steps:

  1. Calipers. Short 1D search profiles across the expected edge: rays across an annulus (circle), normals of the nominal ellipse, perpendicular segments along a line or along each side of a rectangle. Each profile is sampled at 1 px steps and averaged over caliper_width samples along the edge (on a curve: along a parallel curve, not a straight tangent, which would bias the size).
  2. Interpolation: cubic B-spline (interp="bspline"). Only small tiles around the calipers are prefiltered, so the image size does not matter.
  3. Edge per caliper. Gaussian-derivative filter (smooth); keep local maxima of the requested polarity stronger than rel_threshold × the median edge strength; pick the strongest / first / last; refine with a 3-point log-parabola (subpixel="gauss"), which is exact for the Gaussian-shaped peak of a blurred edge.
  4. RANSAC. Random minimal samples (3 points for a circle, 2 for a line, 5 for an ellipse), all scored at once with the MSAC cost. The inliers are re-fitted until stable; then the threshold is tightened to 3× the measured edge noise (MAD) and the inliers re-fitted again.
  5. Least-squares fit on the inliers: algebraic (Kasa) circle, orthogonal (TLS) line, direct ellipse fit (Fitzgibbon / Halír–Flusser). A rectangle is measured side by side like four lines, then the four sides are fitted together with right angles (closed form), and each side's inliers are chosen again with that orientation fixed, so that clutter on one side cannot tilt it.
  6. Curvature correction (circle, ellipse). On a blurred curved edge the gradient maximum lies inside the true contour by σ²κ/2 (κ = curvature, 1/R for a circle). σ is estimated from the width of the derivative peak and the bias is removed: by σ²/2R on the radius of a circle, and point by point along the ellipse, whose curvature varies. Straight edges have no such bias.

Curved tools re-place their calipers on the fitted shape (iterations=2) unless it moved less than recenter_tol (0.2 px).

Tools

Common arguments:

argument default meaning
search 8 each caliper searches ±search px across the nominal edge (ellipse, line, rectangle)
caliper_width / ray_width 5 samples averaged along the edge per caliper
smooth 1.0 σ of the Gaussian derivative [px]; 1.5–2 helps on noisy images
polarity any dark_to_light / light_to_dark walking outwards (line: toward its normal)
select strongest first / last edge on each caliper
rel_threshold 0.25 ignore edges weaker than this × the median edge strength
subpixel gauss parabola has up to ~0.02 px phase error
interp bspline bilinear is ~2× faster but up to ~0.03 px phase error
threshold 1.0 RANSAC inlier distance [px] (tightened automatically)
seed 0 makes RANSAC deterministic

Angles are in degrees, 0° = +x, growing toward +y (clockwise on screen). Coordinates use the image convention: x = column, y = row, and the center of the top-left pixel is (0, 0).

Circle

res = vm.measure_circle(img, center=(100, 100), r_min=34, r_max=46)
res.center, res.radius, res.diameter, res.rms, res.psf_sigma, res.radius_correction

Extra: n_rays (~1 per px of arc, 16–720), angle_range=(0, 360) to measure an arc, iterations=2, curvature_correction=True (False / known σ as a float).

Ellipse

res = vm.measure_ellipse(img, center=(100, 100), axes=(57, 37), angle=15)
res.center, res.a, res.b, res.angle, res.axes  # a >= b; angle of the a axis in [-90, 90)

Extra: n_calipers (~1 per px of perimeter, 16–720), iterations=2, curvature_correction=True.

Line

res = vm.measure_line(img, start=(20, 85), end=(180, 115), search=8)
res.point, res.direction, res.normal, res.angle
res.start, res.end  # nominal endpoints projected onto the fitted line
res.distance(x, y)  # signed distance to the line

The normal points to the right-hand side when walking start → end on screen; polarity refers to that direction (swap start and end to flip it). n_calipers defaults to one per 2 px of length.

Rectangle

res = vm.measure_rectangle(img, center=(100, 100), size=(106, 63), angle=13)
res.center, res.width, res.height, res.angle, res.corners
res.side  # side of each caliper: 0 top, 1 right, 2 bottom, 3 left

width runs along angle. Extra: calipers_per_side (1 per 2 px of side length), corner_margin (calipers closer than this to a corner are skipped; default search + 2).

Results and ok

Every result has rms, n_inliers, n_valid, n_calipers, inlier_ratio, edge_points (per caliper: x, y, amplitude, valid, inlier, residual), ok, message and to_dict().

ok is False when the fit failed or fewer than half of the calipers are inliers (for a rectangle: on any side). A failed fit returns NaN geometry and a message. Calipers that leave the image are dropped (valid=False), not padded.

Interactive explorer

visiometry-gui [image]           # or: python -m visiometry.gui [image]

visiometry explorer: calipers and fitted circle, edge profile of one caliper, residuals

Generate a synthetic circle, ellipse, line or rectangle with known parameters (blur, noise, clutter) or open an image. Set the search region with the mouse:

tool left click right click Shift + right click
circle center radius
ellipse center semi-axis a and its angle semi-axis b
line start end
rectangle center a corner (width, height) angle

You see every caliper, the inliers / outliers and the fit. In Inspect profile mode, click a caliper to see its intensity profile, derivative, threshold and the three samples of the subpixel fit. Every run shows the equivalent Python call and, for synthetic images, the error against the true shape.

Benchmark

visiometry against common OpenCV and scikit-image recipes on the PTB circle data set (public, with ground truth). Every method gets the same task: an initial guess up to ±3 px off (centre and radius) and a ±10 px radius search range. Only circles fully inside the image are counted. Failures = no result or an error > 2 px; the other columns are over the successful measurements. Units: pixels; time = median per circle, one thread.

Synthetic images, exact ground truth (10 images, 2752 × 2208, Gaussian blur σ = 3 px, 527 circles, R ≈ 100–180 px)

method failures centre RMS radius bias radius std ms / circle
visiometry 0 % 0.005 −0.004 0.003 40
scikit-image Canny + RANSAC 0 % 0.161 −0.215 0.087 13
scikit-image subpixel contour + RANSAC 8.5 % 0.264 +0.150 0.158 5
OpenCV HoughCircles (ALT) 0.9 % 0.603 +0.529 0.764 2
OpenCV Otsu contour + fitEllipse 2.7 % 0.941 +0.580 0.533 0.4

Real camera images (10 dPCR droplet images, 673 circles, R ≈ 130 px, low contrast, touching droplets with a dark rim)

method failures centre RMS radius bias radius std ms / circle
visiometry 1.6 % 0.462 +0.119 0.311 36
scikit-image Canny + RANSAC 39 % 0.514 +0.446 0.385 29
scikit-image subpixel contour + RANSAC 98 % 0.893 +0.219 0.533 24
OpenCV HoughCircles (ALT) 91 % 1.359 −0.260 0.747 2
OpenCV Otsu contour + fitEllipse 99 % 1.025 +1.668 0.331 2
  • Synthetic: visiometry's edge points are subpixel, hundreds of them are averaged and the blur's inward shift of a curved edge (σ²/2R) is removed. Canny edges lie on whole pixels; Hough votes on a coarse grid; a thresholded contour runs through pixel centres and depends on the threshold.
  • Real: the reference annotations are whole pixels (±0.5 px → ~0.29 px std), so nothing below ~0.3 px can be resolved here. The droplets have two edges (the inner and outer side of a dark rim); the annotation follows the outer one. visiometry is told so with polarity="dark_to_light". The other recipes have no such setting and mostly lock onto the inner edge (radius 8–9 px too small) or find nothing.

Data: PTB (Physikalisch-Technische Bundesanstalt), Synthetic and Real Image Dataset for Metrological Validation of AI-Based Circle Detection, Zenodo, doi:10.5281/zenodo.23757601, licensed CC BY-NC 4.0.

The comparison scripts need OpenCV and scikit-image, which visiometry itself does not depend on:

pip install opencv-python-headless scikit-image
python benchmarks/compare_ptb.py           # PTB data in evaluationdataset/
python examples/compare_testsample.py      # the plate images in testsample/

Roadmap

  • Point-to-point / point-to-line / line-to-line distances and angles
  • px→mm calibration and lens-distortion correction

Development

pip install -e ".[dev,gui]"
pytest
ruff check . && ruff format --check .
python benchmarks/accuracy.py && python benchmarks/speed.py

License

Apache License 2.0, see LICENSE.

Metadata

Release files for visiometry 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for visiometry 0.1.0
File Size Uploaded Provenance Actions
visiometry-0.1.0.tar.gz 62.8 kB Without provenance Details for visiometry-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for visiometry 0.1.0
File Interpreter ABI Platform Actions
visiometry-0.1.0-py3-none-any.whl Python 3 none any Details for visiometry-0.1.0-py3-none-any.whl

Total release size: 118.3 kB

Release files / visiometry-0.1.0.tar.gz

Download URL visiometry-0.1.0.tar.gz
Size 62.8 kB
Tags Source
SHA-256 checksum
How to use checksums
922a83a7864d41e8f7ca887d7b96b86f2154543c4ffbe094a62fdf04a83bf905
BLAKE2b-256 checksum
How to use checksums
731d86e04eec552e4168ebc7f984ad204d58a9afd77bc5152d49378f0712c266
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / visiometry-0.1.0-py3-none-any.whl

Download URL visiometry-0.1.0-py3-none-any.whl
Size 55.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4610658d2862545be31c0d006e440e349aa91358e481ba3ec0a17f8dd0da8865
BLAKE2b-256 checksum
How to use checksums
0051f73293536b50d359628367459de46630c31b25cb0044df64e2052ee44afc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for visiometry 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page