Skip to content

Commit

Permalink
Merge branch 'feature/v0.1.4' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
KelSolaar committed Apr 12, 2022
2 parents af087ec + 5bba7ff commit 823d4e9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Continuous Integration - Quality & Unit Tests
on: [push, pull_request]

jobs:
continuous-integration-package:
continuous-integration-quality-unit-tests:
name: ${{ matrix.os }} - Python ${{ matrix.python-version }}
strategy:
matrix:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Continuous Integration - Static Type Checking
on: [push, pull_request]

jobs:
continuous-integration-package:
continuous-integration-static-type-checking:
name: ${{ matrix.os }} - Python ${{ matrix.python-version }}
strategy:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.31.0
rev: v2.31.1
hooks:
- id: pyupgrade
args: [--py38-plus]
Expand Down
125 changes: 64 additions & 61 deletions colour_checker_detection/detection/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
as_float_array,
as_int_array,
as_int,
orient,
usage_warning,
)
from colour.utilities.documentation import (
Expand All @@ -64,17 +65,17 @@
"SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC",
"SETTINGS_SEGMENTATION_COLORCHECKER_SG",
"FLOAT_DTYPE_DEFAULT",
"ColourCheckersDetectionData",
"ColourCheckerSwatchesData",
"swatch_masks",
"as_8_bit_BGR_image",
"adjust_image",
"is_square",
"contour_centroid",
"scale_contour",
"crop_and_level_image_with_rectangle",
"DataColourCheckersCoordinatesSegmentation",
"colour_checkers_coordinates_segmentation",
"extract_colour_checkers_segmentation",
"DataDetectColourCheckersSegmentation",
"detect_colour_checkers_segmentation",
]

Expand Down Expand Up @@ -149,52 +150,6 @@
"""Dtype used for the computations."""


@dataclass
class ColourCheckersDetectionData(MixinDataclassIterable):
"""
Colour checkers detection data used for plotting, debugging and further
analysis.
Parameters
----------
colour_checkers
Colour checker bounding boxes, i.e., the. clusters that have the
relevant count of swatches.
clusters
Detected swatches clusters.
swatches
Detected swatches.
segmented_image
Thresholded/Segmented image.
"""

colour_checkers: Tuple[NDArray, ...]
clusters: Tuple[NDArray, ...]
swatches: Tuple[NDArray, ...]
segmented_image: NDArray


@dataclass
class ColourCheckerSwatchesData(MixinDataclassIterable):
"""
Colour checker swatches data used for plotting, debugging and further
analysis.
Parameters
----------
swatch_colours
Colour checker swatches colours.
colour_checker_image
Cropped and levelled Colour checker image.
swatch_masks
Colour checker swatches masks.
"""

swatch_colours: Tuple[NDArray, ...]
colour_checker_image: NDArray
swatch_masks: Tuple[NDArray, ...]


def swatch_masks(
width: Integer,
height: Integer,
Expand Down Expand Up @@ -568,10 +523,6 @@ def crop_and_level_image_with_rectangle(
centroid = contour_centroid(cv2.boxPoints(rectangle))
angle = rectangle[-1]

if angle < -45:
angle += 90
width_r, height_r = height_r, width_r

width_r, height_r = as_int_array([width_r, height_r])

M_r = cv2.getRotationMatrix2D(centroid, angle, 1)
Expand All @@ -581,12 +532,40 @@ def crop_and_level_image_with_rectangle(
image_r, (width_r, height_r), (centroid[0], centroid[1])
)

if image_c.shape[0] > image_c.shape[1]:
image_c = orient(image_c, "90 CW")

return image_c


@dataclass
class DataColourCheckersCoordinatesSegmentation(MixinDataclassIterable):
"""
Colour checkers detection data used for plotting, debugging and further
analysis.
Parameters
----------
colour_checkers
Colour checker bounding boxes, i.e., the. clusters that have the
relevant count of swatches.
clusters
Detected swatches clusters.
swatches
Detected swatches.
segmented_image
Thresholded/Segmented image.
"""

colour_checkers: Tuple[NDArray, ...]
clusters: Tuple[NDArray, ...]
swatches: Tuple[NDArray, ...]
segmented_image: NDArray


def colour_checkers_coordinates_segmentation(
image: ArrayLike, additional_data: Boolean = False, **kwargs: Any
) -> Union[ColourCheckersDetectionData, Tuple[NDArray, ...]]:
) -> Union[DataColourCheckersCoordinatesSegmentation, Tuple[NDArray, ...]]:
"""
Detect the colour checkers coordinates in given image :math:`image` using
segmentation.
Expand Down Expand Up @@ -669,10 +648,10 @@ def colour_checkers_coordinates_segmentation(
Returns
-------
:class:`colour_checker_detection.detection.segmentation.\
ColourCheckersDetectionData` or :class:`tuple`
DataColourCheckersCoordinatesSegmentation` or :class:`tuple`
Tuple of colour checkers coordinates or
:class:`ColourCheckersDetectionData` class instance with additional
data.
:class:`DataColourCheckersCoordinatesSegmentation` class
instance with additional data.
Notes
-----
Expand Down Expand Up @@ -803,7 +782,7 @@ def colour_checkers_coordinates_segmentation(
colour_checkers = tuple(clusters[i] for i in indexes)

if additional_data:
return ColourCheckersDetectionData(
return DataColourCheckersCoordinatesSegmentation(
tuple(colour_checkers), tuple(clusters), tuple(swatches), image_c
)
else:
Expand Down Expand Up @@ -963,12 +942,36 @@ def extract_colour_checkers_segmentation(
return tuple(colour_checkers)


@dataclass
class DataDetectColourCheckersSegmentation(MixinDataclassIterable):
"""
Colour checker swatches data used for plotting, debugging and further
analysis.
Parameters
----------
swatch_colours
Colour checker swatches colours.
colour_checker_image
Cropped and levelled Colour checker image.
swatch_masks
Colour checker swatches masks.
"""

swatch_colours: Tuple[NDArray, ...]
colour_checker_image: NDArray
swatch_masks: Tuple[NDArray, ...]


def detect_colour_checkers_segmentation(
image: ArrayLike,
samples: Integer = 16,
additional_data: Boolean = False,
**kwargs: Any,
) -> Union[Tuple[ColourCheckerSwatchesData, ...], Tuple[NDArray, ...]]:
) -> Union[
Tuple[DataDetectColourCheckersSegmentation, ...],
Tuple[NDArray, ...],
]:
"""
Detect the colour checkers swatches in given image using segmentation.
Expand Down Expand Up @@ -1032,8 +1035,8 @@ def detect_colour_checkers_segmentation(
Returns
-------
:class`tuple`
Tuple of :class:`ColourCheckerSwatchesData` class instances or
colour checkers swatches.
Tuple of :class:`DataDetectColourCheckersSegmentation` class
instances or colour checkers swatches.
Examples
--------
Expand Down Expand Up @@ -1126,7 +1129,7 @@ def detect_colour_checkers_segmentation(

if additional_data:
return tuple(
ColourCheckerSwatchesData(
DataDetectColourCheckersSegmentation(
tuple(colour_checkers_colours[i]), *colour_checkers_data[i]
)
for i, colour_checker_colours in enumerate(colour_checkers_colours)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# !/usr/bin/env python
"""
Defines the unit tests for the
Define the unit tests for the
:mod:`colour_checker_detection.detection.segmentation` module.
"""

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,5 @@ convention = "numpy"
add-ignore = "D104,D200,D202,D205,D301,D400"

[build-system]
requires = ["poetry_core>=1.0.0"]
requires = [ "poetry_core>=1.0.0" ]
build-backend = "poetry.core.masonry.api"

0 comments on commit 823d4e9

Please sign in to comment.