File: enclosingcircle_unit_test.py

package info (click to toggle)
python-sigima 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 25,608 kB
  • sloc: python: 35,251; makefile: 3
file content (75 lines) | stat: -rw-r--r-- 2,447 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.

"""
Enclosing circle test

Testing enclsoing circle function on various test images.
"""

# pylint: disable=invalid-name  # Allows short reference names like x, y, ...
# pylint: disable=duplicate-code

import pytest

import sigima.proc.image
import sigima.tools.image
from sigima.config import _
from sigima.tests import guiutils
from sigima.tests.data import RingParam, create_ring_image, get_laser_spot_data
from sigima.tests.env import execenv
from sigima.tests.helpers import check_scalar_result


def __enclosingcircle_test(data):
    """Enclosing circle test function"""
    # pylint: disable=import-outside-toplevel
    from sigima import viz

    items = []
    items += [viz.create_image(data, interpolation="nearest")]

    # Computing centroid coordinates
    row, col = sigima.tools.image.get_centroid_auto(data)
    label = _("Centroid") + " (%d, %d)"
    execenv.print(label % (row, col))
    cursor = viz.create_cursor("x", (col, row), label=label)
    items.append(cursor)

    x, y, radius = sigima.tools.image.get_enclosing_circle(data)
    circle = viz.create_circle(x, y, radius)
    items.append(circle)
    execenv.print(x, y, radius)
    execenv.print("")

    viz.view_image_items(items)


@pytest.mark.gui
def test_enclosing_circle_interactive():
    """Interactive test for enclosing circle computation."""
    with guiutils.lazy_qt_app_context(force=True):
        for data in get_laser_spot_data():
            __enclosingcircle_test(data)


@pytest.mark.validation
def test_image_enclosing_circle():
    """Test enclosing circle on a ring image."""
    p = RingParam.create(image_size=200, xc=100, yc=100, radius=30, thickness=5)
    # Create a ring image, so that the outer circle radius is radius + thickness:
    obj = create_ring_image(p)
    execenv.print("Testing enclosing circle on a ring image...")
    ex, ey, er = sigima.tools.image.get_enclosing_circle(obj.data)
    geometry = sigima.proc.image.enclosing_circle(obj)
    x, y, r = geometry.coords[0]
    execenv.print(geometry)
    assert ex == x and ey == y and er == r, (
        f"Enclosing circle test failed: expected ({ex}, {ey}, {er}), "
        f"got ({x}, {y}, {r})"
    )
    check_scalar_result("Enclosing circle", er, p.radius + p.thickness, rtol=0.002)


if __name__ == "__main__":
    test_enclosing_circle_interactive()
    test_image_enclosing_circle()