Skip to content

Commit

Permalink
Update various unit tests and doctests.
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Jan 4, 2024
1 parent 84c8670 commit b5ab267
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 194 deletions.
87 changes: 29 additions & 58 deletions colour_checker_detection/detection/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,47 +117,19 @@ def as_8_bit_BGR_image(image: ArrayLike) -> NDArrayInt:
Examples
--------
>>> from colour.algebra import random_triplet_generator
>>> prng = np.random.RandomState(4)
>>> image = list(random_triplet_generator(8, random_state=prng))
>>> image = np.reshape(image, [4, 2, 3])
>>> print(image)
[[[ 0.96702984 0.25298236 0.0089861 ]
[ 0.54723225 0.43479153 0.38657128]]
<BLANKLINE>
[[ 0.97268436 0.77938292 0.04416006]
[ 0.71481599 0.19768507 0.95665297]]
<BLANKLINE>
[[ 0.69772882 0.86299324 0.43614665]
[ 0.2160895 0.98340068 0.94897731]]
<BLANKLINE>
[[ 0.97627445 0.16384224 0.78630599]
[ 0.00623026 0.59733394 0.8662893 ]]]
>>> image = as_8_bit_BGR_image(image)
>>> print(image)
[[[ 23 137 251]
[167 176 195]]
<BLANKLINE>
[[ 59 228 251]
[250 122 219]]
<BLANKLINE>
[[176 238 217]
[249 253 128]]
<BLANKLINE>
[[229 112 252]
[239 203 18]]]
>>> image = np.linspace(0, 1, 24).reshape([4, 2, 3]) * 255
>>> as_8_bit_BGR_image(image)
array([[[ 23, 137, 251],
[167, 176, 195]],
array([[[196, 207, 0],
[139, 12, 120]],
<BLANKLINE>
[[ 59, 228, 251],
[250, 122, 219]],
[[193, 99, 252],
[184, 107, 25]],
<BLANKLINE>
[[176, 238, 217],
[249, 253, 128]],
[[139, 72, 2],
[ 68, 9, 203]],
<BLANKLINE>
[[229, 112, 252],
[239, 203, 18]]], dtype=uint8)
[[236, 182, 126],
[133, 83, 32]]], dtype=uint8)
"""

image = np.asarray(image)[..., :3]
Expand Down Expand Up @@ -203,22 +175,19 @@ def adjust_image(
Examples
--------
>>> from colour.algebra import random_triplet_generator
>>> prng = np.random.RandomState(4)
>>> image = list(random_triplet_generator(8, random_state=prng))
>>> image = np.reshape(image, [2, 4, 3])
>>> adjust_image(image, 5) # doctest: +ELLIPSIS
array([[[ 0.9925325..., 0.2419374..., -0.0139522...],
[ 0.6174497..., 0.3460756..., 0.3189758...],
[ 0.7447774..., 0.678666 ..., 0.1652180...],
[ 0.9476452..., 0.6550805..., 0.2609945...],
[ 0.6991505..., 0.1623470..., 1.0120867...]],
>>> image = np.arange(24).reshape([2, 4, 3])
>>> adjust_image(image, 5) # doctest: +SKIP
array([[[ -0.18225056, 0.8177495 , 1.8177495 ],
[ 1.8322501 , 2.83225 , 3.83225 ],
[ 4.5 , 5.5 , 6.5 ],
[ 7.1677475 , 8.167748 , 9.167748 ],
[ 9.182249 , 10.182249 , 11.182249 ]],
<BLANKLINE>
[[ 0.7269885..., 0.8556784..., 0.4049920...],
[ 0.2666565..., 1.0401633..., 0.8238320...],
[ 0.6419699..., 0.5442698..., 0.9082211...],
[ 0.7894426..., 0.1944301..., 0.7906868...],
[-0.0526997..., 0.6236685..., 0.8711483...]]], dtype=float32)
[[ 11.817749 , 12.81775 , 13.817749 ],
[ 13.83225 , 14.832251 , 15.832251 ],
[ 16.5 , 17.5 , 18.5 ],
[ 19.16775 , 20.167747 , 21.167747 ],
[ 21.182247 , 22.18225 , 23.182251 ]]], dtype=float32)
"""

image = as_float_array(image, FLOAT_DTYPE_DEFAULT)
Expand Down Expand Up @@ -307,6 +276,7 @@ def contour_centroid(contour: ArrayLike) -> Tuple[float, float]:
"""

moments = cv2.moments(contour) # pyright: ignore

centroid = (
moments["m10"] / moments["m00"],
moments["m01"] / moments["m00"],
Expand Down Expand Up @@ -334,14 +304,15 @@ def scale_contour(contour: ArrayLike, factor: float) -> NDArrayFloat:
Examples
--------
>>> contour = np.array([[0, 0], [1, 0], [1, 1], [0, 1]])
>>> scale_contour(contour, 2)
array([[ 0., 0.],
[ 2., 0.],
[ 2., 2.],
[ 0., 2.]])
>>> scale_contour(contour, 2) # doctest: +ELLIPSIS
array([[...-0.5, ...-0.5],
[... 1.5, ...-0.5],
[... 1.5, ... 1.5],
[...-0.5, ... 1.5]])
"""

centroid = as_int_array(contour_centroid(contour))
centroid = as_float_array(contour_centroid(contour))

scaled_contour = (as_float_array(contour) - centroid) * factor + centroid

return scaled_contour
Expand Down
8 changes: 4 additions & 4 deletions colour_checker_detection/detection/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ def colour_checkers_coordinates_segmentation(
... )
>>> image = read_image(path)
>>> colour_checkers_coordinates_segmentation(image) # doctest: +ELLIPSIS
(array([[ 365, 685],
[ 382, 222],
[1078, 247],
[1061, 710]]...)
(array([[ 365, 684],
[ 382, 221],
[1077, 247],
[1060, 710]]...)
"""

image = as_float_array(image, FLOAT_DTYPE_DEFAULT)[..., :3]
Expand Down
4 changes: 2 additions & 2 deletions colour_checker_detection/detection/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,12 +183,12 @@ def test_scale_contour(self):
contour = np.array([[0, 0], [1, 0], [1, 1], [0, 1]])
np.testing.assert_array_equal(
scale_contour(contour, 2),
np.array([[0, 0], [2, 0], [2, 2], [0, 2]]),
np.array([[-0.5, -0.5], [1.5, -0.5], [1.5, 1.5], [-0.5, 1.5]]),
)

np.testing.assert_array_equal(
scale_contour(contour, 0.5),
np.array([[0.0, 0.0], [0.5, 0.0], [0.5, 0.5], [0.0, 0.5]]),
np.array([[0.25, 0.25], [0.75, 0.25], [0.75, 0.75], [0.25, 0.75]]),
)


Expand Down
Loading

0 comments on commit b5ab267

Please sign in to comment.