0
# Color Processing
1
2
Color space conversion and manipulation functions for transforming images between different color representations. Includes standard color spaces (RGB, HSV, LAB) and specialized spaces for histological imaging and scientific applications.
3
4
## Capabilities
5
6
### Basic Color Conversion
7
8
Convert between fundamental color representations commonly used in image processing and computer vision.
9
10
```python { .api }
11
def rgb2gray(rgb):
12
"""
13
Convert RGB image to grayscale.
14
15
Parameters:
16
rgb : array_like
17
Input RGB image
18
19
Returns:
20
ndarray
21
Grayscale image
22
"""
23
24
def gray2rgb(gray, alpha=None):
25
"""
26
Convert grayscale image to RGB.
27
28
Parameters:
29
gray : array_like
30
Input grayscale image
31
alpha : array_like, optional
32
Alpha channel values
33
34
Returns:
35
ndarray
36
RGB image
37
"""
38
39
def rgba2rgb(rgba, background=(1, 1, 1)):
40
"""
41
Convert RGBA image to RGB by blending with background.
42
43
Parameters:
44
rgba : array_like
45
Input RGBA image
46
background : tuple of float, optional
47
Background color for alpha blending (default white)
48
49
Returns:
50
ndarray
51
RGB image
52
"""
53
```
54
55
### RGB ↔ HSV Conversion
56
57
Convert between RGB and HSV (Hue, Saturation, Value) color spaces for color-based image analysis and manipulation.
58
59
```python { .api }
60
def rgb2hsv(rgb):
61
"""
62
Convert RGB image to HSV color space.
63
64
Parameters:
65
rgb : array_like
66
Input RGB image with values in [0, 1]
67
68
Returns:
69
ndarray
70
HSV image with H in [0, 2π], S,V in [0, 1]
71
"""
72
73
def hsv2rgb(hsv):
74
"""
75
Convert HSV image to RGB color space.
76
77
Parameters:
78
hsv : array_like
79
Input HSV image with H in [0, 2π], S,V in [0, 1]
80
81
Returns:
82
ndarray
83
RGB image with values in [0, 1]
84
"""
85
```
86
87
### RGB ↔ LAB Conversion
88
89
Convert between RGB and CIELAB color spaces for perceptually uniform color operations and color difference calculations.
90
91
```python { .api }
92
def rgb2lab(rgb, illuminant='D65', observer='2'):
93
"""
94
Convert RGB to CIELAB color space.
95
96
Parameters:
97
rgb : array_like
98
Input RGB image
99
illuminant : str, optional
100
Illuminant name (default 'D65')
101
observer : str, optional
102
Observer angle ('2' or '10', default '2')
103
104
Returns:
105
ndarray
106
LAB image
107
"""
108
109
def lab2rgb(lab, illuminant='D65', observer='2'):
110
"""
111
Convert CIELAB to RGB color space.
112
113
Parameters:
114
lab : array_like
115
Input LAB image
116
illuminant : str, optional
117
Illuminant name (default 'D65')
118
observer : str, optional
119
Observer angle ('2' or '10', default '2')
120
121
Returns:
122
ndarray
123
RGB image
124
"""
125
```
126
127
### RGB ↔ XYZ Conversion
128
129
Convert between RGB and CIE XYZ color spaces for device-independent color representation.
130
131
```python { .api }
132
def rgb2xyz(rgb):
133
"""
134
Convert RGB to CIE XYZ color space.
135
136
Parameters:
137
rgb : array_like
138
Input RGB image
139
140
Returns:
141
ndarray
142
XYZ image
143
"""
144
145
def xyz2rgb(xyz):
146
"""
147
Convert CIE XYZ to RGB color space.
148
149
Parameters:
150
xyz : array_like
151
Input XYZ image
152
153
Returns:
154
ndarray
155
RGB image
156
"""
157
158
def xyz2lab(xyz, illuminant='D65', observer='2'):
159
"""
160
Convert XYZ to LAB color space.
161
162
Parameters:
163
xyz : array_like
164
Input XYZ image
165
illuminant : str, optional
166
Illuminant name (default 'D65')
167
observer : str, optional
168
Observer angle ('2' or '10', default '2')
169
170
Returns:
171
ndarray
172
LAB image
173
"""
174
175
def lab2xyz(lab, illuminant='D65', observer='2'):
176
"""
177
Convert LAB to XYZ color space.
178
179
Parameters:
180
lab : array_like
181
Input LAB image
182
illuminant : str, optional
183
Illuminant name (default 'D65')
184
observer : str, optional
185
Observer angle ('2' or '10', default '2')
186
187
Returns:
188
ndarray
189
XYZ image
190
"""
191
```
192
193
### YUV and YIQ Color Spaces
194
195
Convert between RGB and broadcast/video color spaces including YUV, YIQ, YPbPr, YCbCr, and YDbDr.
196
197
```python { .api }
198
def rgb2yuv(rgb):
199
"""
200
Convert RGB to YUV color space.
201
202
Parameters:
203
rgb : array_like
204
Input RGB image
205
206
Returns:
207
ndarray
208
YUV image
209
"""
210
211
def yuv2rgb(yuv):
212
"""
213
Convert YUV to RGB color space.
214
215
Parameters:
216
yuv : array_like
217
Input YUV image
218
219
Returns:
220
ndarray
221
RGB image
222
"""
223
224
def rgb2yiq(rgb):
225
"""
226
Convert RGB to YIQ color space.
227
228
Parameters:
229
rgb : array_like
230
Input RGB image
231
232
Returns:
233
ndarray
234
YIQ image
235
"""
236
237
def yiq2rgb(yiq):
238
"""
239
Convert YIQ to RGB color space.
240
241
Parameters:
242
yiq : array_like
243
Input YIQ image
244
245
Returns:
246
ndarray
247
RGB image
248
"""
249
250
def rgb2ycbcr(rgb):
251
"""
252
Convert RGB to YCbCr color space.
253
254
Parameters:
255
rgb : array_like
256
Input RGB image
257
258
Returns:
259
ndarray
260
YCbCr image
261
"""
262
263
def ycbcr2rgb(ycbcr):
264
"""
265
Convert YCbCr to RGB color space.
266
267
Parameters:
268
ycbcr : array_like
269
Input YCbCr image
270
271
Returns:
272
ndarray
273
RGB image
274
"""
275
```
276
277
### Histological Stain Separation
278
279
Separate and combine histological stains for medical image analysis, including support for multiple staining protocols.
280
281
```python { .api }
282
def rgb2hed(rgb):
283
"""
284
Convert RGB to HED (Hematoxylin-Eosin-DAB) color space.
285
286
Parameters:
287
rgb : array_like
288
Input RGB image
289
290
Returns:
291
ndarray
292
HED stain image
293
"""
294
295
def hed2rgb(hed):
296
"""
297
Convert HED to RGB color space.
298
299
Parameters:
300
hed : array_like
301
Input HED stain image
302
303
Returns:
304
ndarray
305
RGB image
306
"""
307
308
def separate_stains(rgb, conv_matrix):
309
"""
310
Separate stains in histological images using conversion matrix.
311
312
Parameters:
313
rgb : array_like
314
Input RGB image
315
conv_matrix : array_like
316
Stain separation matrix
317
318
Returns:
319
ndarray
320
Separated stains
321
"""
322
323
def combine_stains(stains, conv_matrix):
324
"""
325
Combine stains to reconstruct RGB image.
326
327
Parameters:
328
stains : array_like
329
Input stain image
330
conv_matrix : array_like
331
Stain combination matrix
332
333
Returns:
334
ndarray
335
Combined RGB image
336
"""
337
```
338
339
### Color Difference Metrics
340
341
Calculate perceptual color differences using various Delta E methods for color matching and quality assessment.
342
343
```python { .api }
344
def deltaE_cie76(lab1, lab2):
345
"""
346
Calculate CIE76 Delta E color difference.
347
348
Parameters:
349
lab1, lab2 : array_like
350
LAB color values to compare
351
352
Returns:
353
ndarray
354
Color difference values
355
"""
356
357
def deltaE_ciede94(lab1, lab2, kH=1, kC=1, kL=1, k1=0.045, k2=0.015):
358
"""
359
Calculate CIEDE94 Delta E color difference.
360
361
Parameters:
362
lab1, lab2 : array_like
363
LAB color values to compare
364
kH, kC, kL : float, optional
365
Weighting factors for hue, chroma, lightness
366
k1, k2 : float, optional
367
Constants for chroma and hue calculations
368
369
Returns:
370
ndarray
371
Color difference values
372
"""
373
374
def deltaE_ciede2000(lab1, lab2, kL=1, kC=1, kH=1):
375
"""
376
Calculate CIEDE2000 Delta E color difference.
377
378
Parameters:
379
lab1, lab2 : array_like
380
LAB color values to compare
381
kL, kC, kH : float, optional
382
Weighting factors for lightness, chroma, hue
383
384
Returns:
385
ndarray
386
Color difference values
387
"""
388
389
def deltaE_cmc(lab1, lab2, kL=2, kC=1):
390
"""
391
Calculate CMC Delta E color difference.
392
393
Parameters:
394
lab1, lab2 : array_like
395
LAB color values to compare
396
kL, kC : float, optional
397
Lightness and chroma weighting factors
398
399
Returns:
400
ndarray
401
Color difference values
402
"""
403
```
404
405
### Color Labeling and Visualization
406
407
Convert labeled images to color representations for visualization and analysis.
408
409
```python { .api }
410
def label2rgb(label, image=None, colors=None, alpha=0.3, bg_label=0, bg_color=(0, 0, 0), image_alpha=1, color_class=None, **kwargs):
411
"""
412
Convert labeled image to RGB color image for visualization.
413
414
Parameters:
415
label : array_like
416
Labeled input image
417
image : array_like, optional
418
Image used as overlay
419
colors : list or dict, optional
420
Colors for labels
421
alpha : float, optional
422
Alpha blending factor
423
bg_label : int, optional
424
Background label value
425
bg_color : tuple, optional
426
Background color
427
image_alpha : float, optional
428
Alpha for overlay image
429
color_class : dict, optional
430
Color class mapping
431
432
Returns:
433
ndarray
434
RGB representation of labeled image
435
"""
436
```
437
438
## Usage Examples
439
440
### Basic Color Space Conversion
441
442
```python
443
from skimage import data, color
444
import matplotlib.pyplot as plt
445
446
# Load a color image
447
image = data.astronaut()
448
449
# Convert to different color spaces
450
gray = color.rgb2gray(image)
451
hsv = color.rgb2hsv(image)
452
lab = color.rgb2lab(image)
453
454
# Display results
455
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
456
axes[0, 0].imshow(image)
457
axes[0, 0].set_title('Original RGB')
458
axes[0, 1].imshow(gray, cmap='gray')
459
axes[0, 1].set_title('Grayscale')
460
axes[1, 0].imshow(hsv)
461
axes[1, 0].set_title('HSV')
462
axes[1, 1].imshow(lab)
463
axes[1, 1].set_title('LAB')
464
plt.show()
465
```
466
467
### Color-based Segmentation
468
469
```python
470
from skimage import data, color, segmentation
471
import numpy as np
472
473
# Load image and convert to HSV
474
image = data.coffee()
475
hsv = color.rgb2hsv(image)
476
477
# Extract hue channel for segmentation
478
hue = hsv[:, :, 0]
479
480
# Create mask for specific hue range (e.g., brown colors)
481
lower_hue = 0.05
482
upper_hue = 0.15
483
mask = (hue >= lower_hue) & (hue <= upper_hue)
484
485
# Apply mask to original image
486
segmented = image.copy()
487
segmented[~mask] = 0
488
```
489
490
## Types
491
492
```python { .api }
493
from typing import Tuple, Optional, Union, Dict, List
494
from numpy.typing import NDArray
495
import numpy as np
496
497
# Color space representations
498
RGBImage = NDArray[np.floating]
499
GrayImage = NDArray[np.floating]
500
HSVImage = NDArray[np.floating]
501
LABImage = NDArray[np.floating]
502
XYZImage = NDArray[np.floating]
503
504
# Color values
505
RGB = Tuple[float, float, float]
506
HSV = Tuple[float, float, float]
507
LAB = Tuple[float, float, float]
508
509
# Stain separation
510
StainMatrix = NDArray[np.floating]
511
Illuminant = str
512
Observer = str
513
```
514
515
### Advanced Color Space Functions
516
517
Advanced color space conversion and utility functions for specialized applications.
518
519
```python { .api }
520
def convert_colorspace(arr, fromspace, tospace, *, channel_axis=-1):
521
"""
522
Convert an image array to a new color space.
523
524
Parameters:
525
arr : array_like
526
Input image array
527
fromspace : str
528
Source color space ('RGB', 'HSV', 'XYZ', 'LAB', etc.)
529
tospace : str
530
Target color space ('RGB', 'HSV', 'XYZ', 'LAB', etc.)
531
channel_axis : int, optional
532
Axis of the color channels, default is -1
533
534
Returns:
535
ndarray
536
Image in target color space
537
"""
538
539
def xyz_tristimulus_values(*, illuminant, observer, dtype=float):
540
"""
541
Get the CIE XYZ tristimulus values for an illuminant and observer.
542
543
Parameters:
544
illuminant : str
545
Illuminant type (e.g., 'D65', 'A', 'C')
546
observer : str
547
Observer angle ('2' or '10')
548
dtype : dtype, optional
549
Data type for the output
550
551
Returns:
552
ndarray
553
XYZ tristimulus values
554
"""
555
556
def gray2rgba(image, alpha=None, *, channel_axis=-1):
557
"""
558
Create an RGBA representation of a grayscale image.
559
560
Parameters:
561
image : array_like
562
Input grayscale image
563
alpha : array_like or float, optional
564
Alpha channel values
565
channel_axis : int, optional
566
Axis of the color channels, default is -1
567
568
Returns:
569
ndarray
570
RGBA image
571
"""
572
```
573
574
### Additional Color Conversions
575
576
Extended color space conversions including LUV and additional YUV variants.
577
578
```python { .api }
579
def rgb2luv(rgb):
580
"""
581
Convert RGB to CIE LUV color space.
582
583
Parameters:
584
rgb : array_like
585
Input RGB image
586
587
Returns:
588
ndarray
589
LUV image
590
"""
591
592
def luv2rgb(luv):
593
"""
594
Convert CIE LUV to RGB color space.
595
596
Parameters:
597
luv : array_like
598
Input LUV image
599
600
Returns:
601
ndarray
602
RGB image
603
"""
604
605
def luv2xyz(luv):
606
"""
607
Convert CIE LUV to XYZ color space.
608
609
Parameters:
610
luv : array_like
611
Input LUV image
612
613
Returns:
614
ndarray
615
XYZ image
616
"""
617
618
def xyz2luv(xyz):
619
"""
620
Convert XYZ to CIE LUV color space.
621
622
Parameters:
623
xyz : array_like
624
Input XYZ image
625
626
Returns:
627
ndarray
628
LUV image
629
"""
630
631
def lab2lch(lab):
632
"""
633
Convert CIE LAB to LCH color space.
634
635
Parameters:
636
lab : array_like
637
Input LAB image
638
639
Returns:
640
ndarray
641
LCH image (Lightness, Chroma, Hue)
642
"""
643
644
def lch2lab(lch):
645
"""
646
Convert LCH to CIE LAB color space.
647
648
Parameters:
649
lch : array_like
650
Input LCH image
651
652
Returns:
653
ndarray
654
LAB image
655
"""
656
657
def rgb2rgbcie(rgb):
658
"""
659
Convert RGB to RGB CIE color space.
660
661
Parameters:
662
rgb : array_like
663
Input RGB image
664
665
Returns:
666
ndarray
667
RGB CIE image
668
"""
669
670
def rgbcie2rgb(rgbcie):
671
"""
672
Convert RGB CIE to RGB color space.
673
674
Parameters:
675
rgbcie : array_like
676
Input RGB CIE image
677
678
Returns:
679
ndarray
680
RGB image
681
"""
682
683
def rgb2ydbdr(rgb):
684
"""
685
Convert RGB to YDbDr color space.
686
687
Parameters:
688
rgb : array_like
689
Input RGB image
690
691
Returns:
692
ndarray
693
YDbDr image
694
"""
695
696
def ydbdr2rgb(ydbdr):
697
"""
698
Convert YDbDr to RGB color space.
699
700
Parameters:
701
ydbdr : array_like
702
Input YDbDr image
703
704
Returns:
705
ndarray
706
RGB image
707
"""
708
709
def rgb2ypbpr(rgb):
710
"""
711
Convert RGB to YPbPr color space.
712
713
Parameters:
714
rgb : array_like
715
Input RGB image
716
717
Returns:
718
ndarray
719
YPbPr image
720
"""
721
722
def ypbpr2rgb(ypbpr):
723
"""
724
Convert YPbPr to RGB color space.
725
726
Parameters:
727
ypbpr : array_like
728
Input YPbPr image
729
730
Returns:
731
ndarray
732
RGB image
733
"""
734
```
735
736
### Histological Stain Deconvolution
737
738
Specialized functions and matrices for histological stain separation and analysis.
739
740
```python { .api }
741
# Stain deconvolution matrices - these are numpy arrays, not functions
742
rgb_from_hed: NDArray[np.floating] # Hematoxylin + Eosin + DAB matrix
743
hed_from_rgb: NDArray[np.floating] # Inverse of rgb_from_hed
744
745
rgb_from_hdx: NDArray[np.floating] # Hematoxylin + DAB matrix
746
hdx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_hdx
747
748
rgb_from_fgx: NDArray[np.floating] # Feulgen + Light Green matrix
749
fgx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_fgx
750
751
rgb_from_bex: NDArray[np.floating] # Giemsa: Methyl Blue + Eosin matrix
752
bex_from_rgb: NDArray[np.floating] # Inverse of rgb_from_bex
753
754
rgb_from_rbd: NDArray[np.floating] # FastRed + FastBlue + DAB matrix
755
rbd_from_rgb: NDArray[np.floating] # Inverse of rgb_from_rbd
756
757
rgb_from_gdx: NDArray[np.floating] # Methyl Green + DAB matrix
758
gdx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_gdx
759
760
rgb_from_hax: NDArray[np.floating] # Hematoxylin + AEC matrix
761
hax_from_rgb: NDArray[np.floating] # Inverse of rgb_from_hax
762
763
rgb_from_bro: NDArray[np.floating] # Blue Anilline + Red Azocarmine + Orange-G matrix
764
bro_from_rgb: NDArray[np.floating] # Inverse of rgb_from_bro
765
766
rgb_from_bpx: NDArray[np.floating] # Methyl Blue + Ponceau Fuchsin matrix
767
bpx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_bpx
768
769
rgb_from_ahx: NDArray[np.floating] # Alcian Blue + Hematoxylin matrix
770
ahx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_ahx
771
772
rgb_from_hpx: NDArray[np.floating] # Hematoxylin + PAS matrix
773
hpx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_hpx
774
```
775
776
### Color Utilities
777
778
```python { .api }
779
color_dict: Dict[str, Tuple[float, float, float]]
780
# Dictionary mapping color names to RGB tuples
781
# Contains standard color names like 'red', 'blue', 'green', etc.
782
```