-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
segmentation.py
634 lines (566 loc) · 21.5 KB
/
segmentation.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
"""
Colour Checker Detection - Segmentation
=======================================
Defines the objects for colour checker detection using segmentation:
- :attr:`colour_checker_detection.SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC`
- :attr:`colour_checker_detection.SETTINGS_SEGMENTATION_COLORCHECKER_SG`
- :attr:`colour_checker_detection.SETTINGS_SEGMENTATION_COLORCHECKER_NANO`
- :func:`colour_checker_detection.segmenter_default`
- :func:`colour_checker_detection.detect_colour_checkers_segmentation`
References
----------
- :cite:`Abecassis2011` : Abecassis, F. (2011). OpenCV - Rotation
(Deskewing). Retrieved October 27, 2018, from http://felix.abecassis.me/\
2011/10/opencv-rotation-deskewing/
"""
from __future__ import annotations
from dataclasses import dataclass
import cv2
import numpy as np
from colour.hints import (
Any,
ArrayLike,
Callable,
Dict,
NDArrayFloat,
NDArrayInt,
Tuple,
Union,
cast,
)
from colour.io import convert_bit_depth, read_image
from colour.models import eotf_inverse_sRGB, eotf_sRGB
from colour.plotting import CONSTANTS_COLOUR_STYLE, plot_image
from colour.utilities import (
MixinDataclassIterable,
Structure,
is_string,
)
from colour.utilities.documentation import (
DocstringDict,
is_documentation_building,
)
from colour_checker_detection.detection.common import (
DTYPE_FLOAT_DEFAULT,
SETTINGS_DETECTION_COLORCHECKER_CLASSIC,
SETTINGS_DETECTION_COLORCHECKER_SG,
DataDetectionColourChecker,
as_int32_array,
contour_centroid,
detect_contours,
is_square,
quadrilateralise_contours,
reformat_image,
remove_stacked_contours,
sample_colour_checker,
scale_contour,
)
__author__ = "Colour Developers"
__copyright__ = "Copyright 2018 Colour Developers"
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
__maintainer__ = "Colour Developers"
__email__ = "colour-developers@colour-science.org"
__status__ = "Production"
__all__ = [
"SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC",
"SETTINGS_SEGMENTATION_COLORCHECKER_SG",
"SETTINGS_SEGMENTATION_COLORCHECKER_NANO",
"DataSegmentationColourCheckers",
"segmenter_default",
"detect_colour_checkers_segmentation",
]
SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC: Dict = (
SETTINGS_DETECTION_COLORCHECKER_CLASSIC.copy()
)
SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC.update(
{
"aspect_ratio_minimum": 1.5 * 0.9,
"aspect_ratio_maximum": 1.5 * 1.1,
"swatches_count_minimum": int(24 * 0.75),
"swatches_count_maximum": int(24 * 1.25),
"swatch_minimum_area_factor": 200,
"swatch_contour_scale": 1 + 1 / 3,
}
)
if is_documentation_building(): # pragma: no cover
SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC = DocstringDict(
SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC
)
SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC.__doc__ = """
Settings for the segmentation of the *X-Rite* *ColorChecker Classic* and
*X-Rite* *ColorChecker Passport*.
"""
SETTINGS_SEGMENTATION_COLORCHECKER_SG: Dict = SETTINGS_DETECTION_COLORCHECKER_SG.copy()
SETTINGS_SEGMENTATION_COLORCHECKER_SG.update(
{
"aspect_ratio_minimum": 1.4 * 0.9,
"aspect_ratio_maximum": 1.4 * 1.1,
"swatches_count_minimum": int(140 * 0.50),
"swatches_count_maximum": int(140 * 1.5),
"swatch_contour_scale": 1 + 1 / 3,
"swatch_minimum_area_factor": 200,
}
)
if is_documentation_building(): # pragma: no cover
SETTINGS_SEGMENTATION_COLORCHECKER_SG = DocstringDict(
SETTINGS_SEGMENTATION_COLORCHECKER_SG
)
SETTINGS_SEGMENTATION_COLORCHECKER_SG.__doc__ = """
Settings for the segmentation of the *X-Rite* *ColorChecker SG**.
"""
SETTINGS_SEGMENTATION_COLORCHECKER_NANO: Dict = (
SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC.copy()
)
SETTINGS_SEGMENTATION_COLORCHECKER_NANO.update(
{
"aspect_ratio_minimum": 1.4 * 0.75,
"aspect_ratio_maximum": 1.4 * 1.5,
"swatch_contour_scale": 1 + 1 / 2,
}
)
if is_documentation_building(): # pragma: no cover
SETTINGS_SEGMENTATION_COLORCHECKER_NANO = DocstringDict(
SETTINGS_SEGMENTATION_COLORCHECKER_NANO
)
SETTINGS_SEGMENTATION_COLORCHECKER_NANO.__doc__ = """
Settings for the segmentation of the *X-Rite* *ColorChecker Nano**.
"""
@dataclass
class DataSegmentationColourCheckers(MixinDataclassIterable):
"""
Colour checkers detection data used for plotting, debugging and further
analysis.
Parameters
----------
rectangles
Colour checker bounding boxes, i.e., the clusters that have the
relevant count of swatches.
clusters
Detected swatches clusters.
swatches
Detected swatches.
segmented_image
Segmented image.
"""
rectangles: NDArrayInt
clusters: NDArrayInt
swatches: NDArrayInt
segmented_image: NDArrayFloat
def segmenter_default(
image: ArrayLike,
cctf_encoding: Callable = eotf_inverse_sRGB,
apply_cctf_encoding: bool = True,
additional_data: bool = False,
**kwargs: Any,
) -> DataSegmentationColourCheckers | NDArrayInt:
"""
Detect the colour checker rectangles in given image :math:`image` using
segmentation.
The process is a follows:
- Input image :math:`image` is converted to a grayscale image
:math:`image_g` and normalised to range [0, 1].
- Image :math:`image_g` is denoised using multiple bilateral filtering
passes into image :math:`image_d.`
- Image :math:`image_d` is thresholded into image :math:`image_t`.
- Image :math:`image_t` is eroded and dilated to cleanup remaining noise
into image :math:`image_k`.
- Contours are detected on image :math:`image_k`
- Contours are filtered to only keep squares/swatches above and below
defined surface area.
- Squares/swatches are clustered to isolate region-of-interest that are
potentially colour checkers: Contours are scaled by a third so that
colour checkers swatches are joined, creating a large rectangular
cluster. Rectangles are fitted to the clusters.
- Clusters with an aspect ratio different to the expected one are
rejected, a side-effect is that the complementary pane of the
*X-Rite* *ColorChecker Passport* is omitted.
- Clusters with a number of swatches close to the expected one are
kept.
Parameters
----------
image
Image to detect the colour checker rectangles from.
cctf_encoding
Encoding colour component transfer function / opto-electronic
transfer function used when converting the image from float to 8-bit.
apply_cctf_encoding
Apply the encoding colour component transfer function / opto-electronic
transfer function.
additional_data
Whether to output additional data.
Other Parameters
----------------
adaptive_threshold_kwargs
Keyword arguments for :func:`cv2.adaptiveThreshold` definition.
aspect_ratio
Colour checker aspect ratio, e.g. 1.5.
aspect_ratio_minimum
Minimum colour checker aspect ratio for detection: projective geometry
might reduce the colour checker aspect ratio.
aspect_ratio_maximum
Maximum colour checker aspect ratio for detection: projective geometry
might increase the colour checker aspect ratio.
bilateral_filter_iterations
Number of iterations to use for bilateral filtering.
bilateral_filter_kwargs
Keyword arguments for :func:`cv2.bilateralFilter` definition.
convolution_iterations
Number of iterations to use for the erosion / dilation process.
convolution_kernel
Convolution kernel to use for the erosion / dilation process.
interpolation_method
Interpolation method used when resizing the images, `cv2.INTER_CUBIC`
and `cv2.INTER_LINEAR` methods are recommended.
reference_values
Reference values for the colour checker of interest.
swatch_contour_scale
As the image is filtered, the swatches area will tend to shrink, the
generated contours can thus be scaled.
swatch_minimum_area_factor
Swatch minimum area factor :math:`f` with the minimum area :math:`m_a`
expressed as follows: :math:`m_a = image_w * image_h / s_c / f` where
:math:`image_w`, :math:`image_h` and :math:`s_c` are respectively the
image width, height and the swatches count.
swatches
Colour checker swatches total count.
swatches_achromatic_slice
A `slice` instance defining achromatic swatches used to detect if the
colour checker is upside down.
swatches_chromatic_slice
A `slice` instance defining chromatic swatches used to detect if the
colour checker is upside down.
swatches_count_maximum
Maximum swatches count to be considered for the detection.
swatches_count_minimum
Minimum swatches count to be considered for the detection.
swatches_horizontal
Colour checker swatches horizontal columns count.
swatches_vertical
Colour checker swatches vertical row count.
transform
Transform to apply to the colour checker image post-detection.
working_width
Width the input image is resized to for detection.
working_height
Height the input image is resized to for detection.
Returns
-------
:class:`colour_checker_detection.DataSegmentationColourCheckers` or \
:class:`np.ndarray`
Colour checker rectangles and additional data or colour checker
rectangles only.
Notes
-----
- Multiple colour checkers can be detected if present in ``image``.
Examples
--------
>>> import os
>>> from colour import read_image
>>> from colour_checker_detection import ROOT_RESOURCES_TESTS
>>> path = os.path.join(
... ROOT_RESOURCES_TESTS,
... "colour_checker_detection",
... "detection",
... "IMG_1967.png",
... )
>>> image = read_image(path)
>>> segmenter_default(image) # doctest: +ELLIPSIS
array([[[ 358, 691],
[ 373, 219],
[1086, 242],
[1071, 713]]]...)
"""
settings = Structure(**SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC)
settings.update(**kwargs)
if apply_cctf_encoding:
image = cctf_encoding(image)
image = reformat_image(image, settings.working_width, settings.interpolation_method)
width, height = image.shape[1], image.shape[0]
minimum_area = (
width * height / settings.swatches / settings.swatch_minimum_area_factor
)
maximum_area = width * height / settings.swatches
contours, image_k = detect_contours(image, True, **settings) # pyright: ignore
# Filtering squares/swatches contours.
squares = []
for swatch_contour in quadrilateralise_contours(contours):
if minimum_area < cv2.contourArea(swatch_contour) < maximum_area and is_square(
swatch_contour
):
squares.append(
as_int32_array(cv2.boxPoints(cv2.minAreaRect(swatch_contour)))
)
# Removing stacked squares.
squares = as_int32_array(remove_stacked_contours(squares))
# Clustering swatches.
swatches = [
scale_contour(square, settings.swatch_contour_scale) for square in squares
]
image_c = np.zeros(image.shape, dtype=np.uint8)
cv2.drawContours(
image_c,
as_int32_array(swatches), # pyright: ignore
-1,
[255] * 3,
-1,
)
image_c = cv2.cvtColor(image_c, cv2.COLOR_RGB2GRAY)
contours, _hierarchy = cv2.findContours(
image_c, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
)
clusters = as_int32_array(
[cv2.boxPoints(cv2.minAreaRect(contour)) for contour in contours]
)
# Filtering clusters using their aspect ratio.
filtered_clusters = []
for cluster in clusters[:]:
rectangle = cv2.minAreaRect(cluster)
width = max(rectangle[1][0], rectangle[1][1])
height = min(rectangle[1][0], rectangle[1][1])
ratio = width / height
if settings.aspect_ratio_minimum < ratio < settings.aspect_ratio_maximum:
filtered_clusters.append(as_int32_array(cluster))
clusters = as_int32_array(filtered_clusters)
# Filtering swatches within cluster.
counts = []
for cluster in clusters:
count = 0
for swatch in swatches:
if cv2.pointPolygonTest(cluster, contour_centroid(swatch), False) == 1:
count += 1
counts.append(count)
indexes = np.where(
np.logical_and(
as_int32_array(counts) >= settings.swatches_count_minimum,
as_int32_array(counts) <= settings.swatches_count_maximum,
)
)[0]
rectangles = clusters[indexes]
if additional_data:
return DataSegmentationColourCheckers(
rectangles,
clusters,
squares,
image_k, # pyright: ignore
)
else:
return rectangles
def detect_colour_checkers_segmentation(
image: str | ArrayLike,
samples: int = 32,
cctf_decoding: Callable = eotf_sRGB,
apply_cctf_decoding: bool = False,
segmenter: Callable = segmenter_default,
segmenter_kwargs: dict | None = None,
show: bool = False,
additional_data: bool = False,
**kwargs: Any,
) -> Tuple[DataDetectionColourChecker | NDArrayFloat, ...]:
"""
Detect the colour checkers swatches in given image using segmentation.
Parameters
----------
image
Image (or image path to read the image from) to detect the colour
checkers swatches from.
samples
Sample count to use to average (mean) the swatches colours. The effective
sample count is :math:`samples^2`.
cctf_decoding
Decoding colour component transfer function / opto-electronic
transfer function used when converting the image from 8-bit to float.
apply_cctf_decoding
Apply the decoding colour component transfer function / opto-electronic
transfer function.
segmenter
Callable responsible to segment the image and extract the colour
checker rectangles.
segmenter_kwargs
Keyword arguments to pass to the ``segmenter``.
show
Whether to show various debug images.
additional_data
Whether to output additional data.
Other Parameters
----------------
adaptive_threshold_kwargs
Keyword arguments for :func:`cv2.adaptiveThreshold` definition.
aspect_ratio
Colour checker aspect ratio, e.g. 1.5.
aspect_ratio_minimum
Minimum colour checker aspect ratio for detection: projective geometry
might reduce the colour checker aspect ratio.
aspect_ratio_maximum
Maximum colour checker aspect ratio for detection: projective geometry
might increase the colour checker aspect ratio.
bilateral_filter_iterations
Number of iterations to use for bilateral filtering.
bilateral_filter_kwargs
Keyword arguments for :func:`cv2.bilateralFilter` definition.
convolution_iterations
Number of iterations to use for the erosion / dilation process.
convolution_kernel
Convolution kernel to use for the erosion / dilation process.
interpolation_method
Interpolation method used when resizing the images, `cv2.INTER_CUBIC`
and `cv2.INTER_LINEAR` methods are recommended.
reference_values
Reference values for the colour checker of interest.
swatch_contour_scale
As the image is filtered, the swatches area will tend to shrink, the
generated contours can thus be scaled.
swatch_minimum_area_factor
Swatch minimum area factor :math:`f` with the minimum area :math:`m_a`
expressed as follows: :math:`m_a = image_w * image_h / s_c / f` where
:math:`image_w`, :math:`image_h` and :math:`s_c` are respectively the
image width, height and the swatches count.
swatches
Colour checker swatches total count.
swatches_achromatic_slice
A `slice` instance defining achromatic swatches used to detect if the
colour checker is upside down.
swatches_chromatic_slice
A `slice` instance defining chromatic swatches used to detect if the
colour checker is upside down.
swatches_count_maximum
Maximum swatches count to be considered for the detection.
swatches_count_minimum
Minimum swatches count to be considered for the detection.
swatches_horizontal
Colour checker swatches horizontal columns count.
swatches_vertical
Colour checker swatches vertical row count.
transform
Transform to apply to the colour checker image post-detection.
working_width
Width the input image is resized to for detection.
working_height
Height the input image is resized to for detection.
Returns
-------
:class`tuple`
Tuple of :class:`DataDetectionColourChecker` class
instances or colour checkers swatches.
Examples
--------
>>> import os
>>> from colour import read_image
>>> from colour_checker_detection import ROOT_RESOURCES_TESTS
>>> path = os.path.join(
... ROOT_RESOURCES_TESTS,
... "colour_checker_detection",
... "detection",
... "IMG_1967.png",
... )
>>> image = read_image(path)
>>> detect_colour_checkers_segmentation(image) # doctest: +SKIP
(array([[ 0.360005 , 0.22310828, 0.11760835],
[ 0.6258309 , 0.39448667, 0.24166533],
[ 0.33198 , 0.31600377, 0.28866866],
[ 0.3046006 , 0.273321 , 0.10486555],
[ 0.41751358, 0.31914026, 0.30789137],
[ 0.34866226, 0.43934596, 0.29126382],
[ 0.67983997, 0.35236534, 0.06997226],
[ 0.27118555, 0.25352538, 0.33078724],
[ 0.62091863, 0.27034152, 0.18652563],
[ 0.3071613 , 0.17978874, 0.19181632],
[ 0.48547146, 0.4585586 , 0.03294956],
[ 0.6507678 , 0.40023172, 0.01607676],
[ 0.19286253, 0.18585181, 0.27459183],
[ 0.28054565, 0.38513032, 0.1224441 ],
[ 0.5545431 , 0.21436104, 0.12549178],
[ 0.72068894, 0.51493925, 0.00548734],
[ 0.5772921 , 0.2577179 , 0.2685553 ],
[ 0.17289193, 0.3163792 , 0.2950853 ],
[ 0.7394083 , 0.60953134, 0.4383072 ],
[ 0.6281671 , 0.51759964, 0.37215686],
[ 0.51360977, 0.42048824, 0.2985709 ],
[ 0.36953217, 0.30218402, 0.20827036],
[ 0.26286703, 0.21493268, 0.14277342],
[ 0.16102524, 0.13381621, 0.08047409]]...),)
"""
if segmenter_kwargs is None:
segmenter_kwargs = {}
settings = Structure(**SETTINGS_SEGMENTATION_COLORCHECKER_CLASSIC)
settings.update(**kwargs)
swatches_h = settings.swatches_horizontal
swatches_v = settings.swatches_vertical
working_width = settings.working_width
working_height = int(working_width / settings.aspect_ratio)
if is_string(image):
image = read_image(cast(str, image))
else:
image = convert_bit_depth(
image,
DTYPE_FLOAT_DEFAULT.__name__, # pyright: ignore
)
if apply_cctf_decoding:
image = cctf_decoding(image)
image = cast(Union[NDArrayInt, NDArrayFloat], image)
image = reformat_image(image, settings.working_width, settings.interpolation_method)
rectangle = as_int32_array(
[
[working_width, 0],
[working_width, working_height],
[0, working_height],
[0, 0],
]
)
segmentation_colour_checkers_data = segmenter(
image, additional_data=True, **{**segmenter_kwargs, **settings}
)
colour_checkers_data = []
for quadrilateral in segmentation_colour_checkers_data.rectangles:
colour_checkers_data.append(
sample_colour_checker(image, quadrilateral, rectangle, samples, **settings)
)
if show:
colour_checker = np.copy(colour_checkers_data[-1].colour_checker)
for swatch_mask in colour_checkers_data[-1].swatch_masks:
colour_checker[
swatch_mask[0] : swatch_mask[1],
swatch_mask[2] : swatch_mask[3],
...,
] = 0
plot_image(
CONSTANTS_COLOUR_STYLE.colour.colourspace.cctf_encoding(colour_checker),
)
plot_image(
CONSTANTS_COLOUR_STYLE.colour.colourspace.cctf_encoding(
np.reshape(
colour_checkers_data[-1].swatch_colours,
[swatches_v, swatches_h, 3],
)
),
)
if show:
plot_image(
segmentation_colour_checkers_data.segmented_image,
text_kwargs={"text": "Segmented Image", "color": "black"},
)
image_c = np.copy(image)
cv2.drawContours(
image_c,
segmentation_colour_checkers_data.swatches,
-1,
(1, 0, 1),
3,
)
cv2.drawContours(
image_c,
segmentation_colour_checkers_data.clusters,
-1,
(0, 1, 1),
3,
)
plot_image(
CONSTANTS_COLOUR_STYLE.colour.colourspace.cctf_encoding(image_c),
text_kwargs={"text": "Swatches & Clusters", "color": "white"},
)
if additional_data:
return tuple(colour_checkers_data)
else:
return tuple(
colour_checker_data.swatch_colours
for colour_checker_data in colour_checkers_data
)