Comprehensive image processing and computer vision library for Python with algorithms for filtering, morphology, segmentation, and feature detection
—
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.
Convert between fundamental color representations commonly used in image processing and computer vision.
def rgb2gray(rgb):
"""
Convert RGB image to grayscale.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
Grayscale image
"""
def gray2rgb(gray, alpha=None):
"""
Convert grayscale image to RGB.
Parameters:
gray : array_like
Input grayscale image
alpha : array_like, optional
Alpha channel values
Returns:
ndarray
RGB image
"""
def rgba2rgb(rgba, background=(1, 1, 1)):
"""
Convert RGBA image to RGB by blending with background.
Parameters:
rgba : array_like
Input RGBA image
background : tuple of float, optional
Background color for alpha blending (default white)
Returns:
ndarray
RGB image
"""Convert between RGB and HSV (Hue, Saturation, Value) color spaces for color-based image analysis and manipulation.
def rgb2hsv(rgb):
"""
Convert RGB image to HSV color space.
Parameters:
rgb : array_like
Input RGB image with values in [0, 1]
Returns:
ndarray
HSV image with H in [0, 2π], S,V in [0, 1]
"""
def hsv2rgb(hsv):
"""
Convert HSV image to RGB color space.
Parameters:
hsv : array_like
Input HSV image with H in [0, 2π], S,V in [0, 1]
Returns:
ndarray
RGB image with values in [0, 1]
"""Convert between RGB and CIELAB color spaces for perceptually uniform color operations and color difference calculations.
def rgb2lab(rgb, illuminant='D65', observer='2'):
"""
Convert RGB to CIELAB color space.
Parameters:
rgb : array_like
Input RGB image
illuminant : str, optional
Illuminant name (default 'D65')
observer : str, optional
Observer angle ('2' or '10', default '2')
Returns:
ndarray
LAB image
"""
def lab2rgb(lab, illuminant='D65', observer='2'):
"""
Convert CIELAB to RGB color space.
Parameters:
lab : array_like
Input LAB image
illuminant : str, optional
Illuminant name (default 'D65')
observer : str, optional
Observer angle ('2' or '10', default '2')
Returns:
ndarray
RGB image
"""Convert between RGB and CIE XYZ color spaces for device-independent color representation.
def rgb2xyz(rgb):
"""
Convert RGB to CIE XYZ color space.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
XYZ image
"""
def xyz2rgb(xyz):
"""
Convert CIE XYZ to RGB color space.
Parameters:
xyz : array_like
Input XYZ image
Returns:
ndarray
RGB image
"""
def xyz2lab(xyz, illuminant='D65', observer='2'):
"""
Convert XYZ to LAB color space.
Parameters:
xyz : array_like
Input XYZ image
illuminant : str, optional
Illuminant name (default 'D65')
observer : str, optional
Observer angle ('2' or '10', default '2')
Returns:
ndarray
LAB image
"""
def lab2xyz(lab, illuminant='D65', observer='2'):
"""
Convert LAB to XYZ color space.
Parameters:
lab : array_like
Input LAB image
illuminant : str, optional
Illuminant name (default 'D65')
observer : str, optional
Observer angle ('2' or '10', default '2')
Returns:
ndarray
XYZ image
"""Convert between RGB and broadcast/video color spaces including YUV, YIQ, YPbPr, YCbCr, and YDbDr.
def rgb2yuv(rgb):
"""
Convert RGB to YUV color space.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
YUV image
"""
def yuv2rgb(yuv):
"""
Convert YUV to RGB color space.
Parameters:
yuv : array_like
Input YUV image
Returns:
ndarray
RGB image
"""
def rgb2yiq(rgb):
"""
Convert RGB to YIQ color space.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
YIQ image
"""
def yiq2rgb(yiq):
"""
Convert YIQ to RGB color space.
Parameters:
yiq : array_like
Input YIQ image
Returns:
ndarray
RGB image
"""
def rgb2ycbcr(rgb):
"""
Convert RGB to YCbCr color space.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
YCbCr image
"""
def ycbcr2rgb(ycbcr):
"""
Convert YCbCr to RGB color space.
Parameters:
ycbcr : array_like
Input YCbCr image
Returns:
ndarray
RGB image
"""Separate and combine histological stains for medical image analysis, including support for multiple staining protocols.
def rgb2hed(rgb):
"""
Convert RGB to HED (Hematoxylin-Eosin-DAB) color space.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
HED stain image
"""
def hed2rgb(hed):
"""
Convert HED to RGB color space.
Parameters:
hed : array_like
Input HED stain image
Returns:
ndarray
RGB image
"""
def separate_stains(rgb, conv_matrix):
"""
Separate stains in histological images using conversion matrix.
Parameters:
rgb : array_like
Input RGB image
conv_matrix : array_like
Stain separation matrix
Returns:
ndarray
Separated stains
"""
def combine_stains(stains, conv_matrix):
"""
Combine stains to reconstruct RGB image.
Parameters:
stains : array_like
Input stain image
conv_matrix : array_like
Stain combination matrix
Returns:
ndarray
Combined RGB image
"""Calculate perceptual color differences using various Delta E methods for color matching and quality assessment.
def deltaE_cie76(lab1, lab2):
"""
Calculate CIE76 Delta E color difference.
Parameters:
lab1, lab2 : array_like
LAB color values to compare
Returns:
ndarray
Color difference values
"""
def deltaE_ciede94(lab1, lab2, kH=1, kC=1, kL=1, k1=0.045, k2=0.015):
"""
Calculate CIEDE94 Delta E color difference.
Parameters:
lab1, lab2 : array_like
LAB color values to compare
kH, kC, kL : float, optional
Weighting factors for hue, chroma, lightness
k1, k2 : float, optional
Constants for chroma and hue calculations
Returns:
ndarray
Color difference values
"""
def deltaE_ciede2000(lab1, lab2, kL=1, kC=1, kH=1):
"""
Calculate CIEDE2000 Delta E color difference.
Parameters:
lab1, lab2 : array_like
LAB color values to compare
kL, kC, kH : float, optional
Weighting factors for lightness, chroma, hue
Returns:
ndarray
Color difference values
"""
def deltaE_cmc(lab1, lab2, kL=2, kC=1):
"""
Calculate CMC Delta E color difference.
Parameters:
lab1, lab2 : array_like
LAB color values to compare
kL, kC : float, optional
Lightness and chroma weighting factors
Returns:
ndarray
Color difference values
"""Convert labeled images to color representations for visualization and analysis.
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):
"""
Convert labeled image to RGB color image for visualization.
Parameters:
label : array_like
Labeled input image
image : array_like, optional
Image used as overlay
colors : list or dict, optional
Colors for labels
alpha : float, optional
Alpha blending factor
bg_label : int, optional
Background label value
bg_color : tuple, optional
Background color
image_alpha : float, optional
Alpha for overlay image
color_class : dict, optional
Color class mapping
Returns:
ndarray
RGB representation of labeled image
"""from skimage import data, color
import matplotlib.pyplot as plt
# Load a color image
image = data.astronaut()
# Convert to different color spaces
gray = color.rgb2gray(image)
hsv = color.rgb2hsv(image)
lab = color.rgb2lab(image)
# Display results
fig, axes = plt.subplots(2, 2, figsize=(10, 8))
axes[0, 0].imshow(image)
axes[0, 0].set_title('Original RGB')
axes[0, 1].imshow(gray, cmap='gray')
axes[0, 1].set_title('Grayscale')
axes[1, 0].imshow(hsv)
axes[1, 0].set_title('HSV')
axes[1, 1].imshow(lab)
axes[1, 1].set_title('LAB')
plt.show()from skimage import data, color, segmentation
import numpy as np
# Load image and convert to HSV
image = data.coffee()
hsv = color.rgb2hsv(image)
# Extract hue channel for segmentation
hue = hsv[:, :, 0]
# Create mask for specific hue range (e.g., brown colors)
lower_hue = 0.05
upper_hue = 0.15
mask = (hue >= lower_hue) & (hue <= upper_hue)
# Apply mask to original image
segmented = image.copy()
segmented[~mask] = 0from typing import Tuple, Optional, Union, Dict, List
from numpy.typing import NDArray
import numpy as np
# Color space representations
RGBImage = NDArray[np.floating]
GrayImage = NDArray[np.floating]
HSVImage = NDArray[np.floating]
LABImage = NDArray[np.floating]
XYZImage = NDArray[np.floating]
# Color values
RGB = Tuple[float, float, float]
HSV = Tuple[float, float, float]
LAB = Tuple[float, float, float]
# Stain separation
StainMatrix = NDArray[np.floating]
Illuminant = str
Observer = strAdvanced color space conversion and utility functions for specialized applications.
def convert_colorspace(arr, fromspace, tospace, *, channel_axis=-1):
"""
Convert an image array to a new color space.
Parameters:
arr : array_like
Input image array
fromspace : str
Source color space ('RGB', 'HSV', 'XYZ', 'LAB', etc.)
tospace : str
Target color space ('RGB', 'HSV', 'XYZ', 'LAB', etc.)
channel_axis : int, optional
Axis of the color channels, default is -1
Returns:
ndarray
Image in target color space
"""
def xyz_tristimulus_values(*, illuminant, observer, dtype=float):
"""
Get the CIE XYZ tristimulus values for an illuminant and observer.
Parameters:
illuminant : str
Illuminant type (e.g., 'D65', 'A', 'C')
observer : str
Observer angle ('2' or '10')
dtype : dtype, optional
Data type for the output
Returns:
ndarray
XYZ tristimulus values
"""
def gray2rgba(image, alpha=None, *, channel_axis=-1):
"""
Create an RGBA representation of a grayscale image.
Parameters:
image : array_like
Input grayscale image
alpha : array_like or float, optional
Alpha channel values
channel_axis : int, optional
Axis of the color channels, default is -1
Returns:
ndarray
RGBA image
"""Extended color space conversions including LUV and additional YUV variants.
def rgb2luv(rgb):
"""
Convert RGB to CIE LUV color space.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
LUV image
"""
def luv2rgb(luv):
"""
Convert CIE LUV to RGB color space.
Parameters:
luv : array_like
Input LUV image
Returns:
ndarray
RGB image
"""
def luv2xyz(luv):
"""
Convert CIE LUV to XYZ color space.
Parameters:
luv : array_like
Input LUV image
Returns:
ndarray
XYZ image
"""
def xyz2luv(xyz):
"""
Convert XYZ to CIE LUV color space.
Parameters:
xyz : array_like
Input XYZ image
Returns:
ndarray
LUV image
"""
def lab2lch(lab):
"""
Convert CIE LAB to LCH color space.
Parameters:
lab : array_like
Input LAB image
Returns:
ndarray
LCH image (Lightness, Chroma, Hue)
"""
def lch2lab(lch):
"""
Convert LCH to CIE LAB color space.
Parameters:
lch : array_like
Input LCH image
Returns:
ndarray
LAB image
"""
def rgb2rgbcie(rgb):
"""
Convert RGB to RGB CIE color space.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
RGB CIE image
"""
def rgbcie2rgb(rgbcie):
"""
Convert RGB CIE to RGB color space.
Parameters:
rgbcie : array_like
Input RGB CIE image
Returns:
ndarray
RGB image
"""
def rgb2ydbdr(rgb):
"""
Convert RGB to YDbDr color space.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
YDbDr image
"""
def ydbdr2rgb(ydbdr):
"""
Convert YDbDr to RGB color space.
Parameters:
ydbdr : array_like
Input YDbDr image
Returns:
ndarray
RGB image
"""
def rgb2ypbpr(rgb):
"""
Convert RGB to YPbPr color space.
Parameters:
rgb : array_like
Input RGB image
Returns:
ndarray
YPbPr image
"""
def ypbpr2rgb(ypbpr):
"""
Convert YPbPr to RGB color space.
Parameters:
ypbpr : array_like
Input YPbPr image
Returns:
ndarray
RGB image
"""Specialized functions and matrices for histological stain separation and analysis.
# Stain deconvolution matrices - these are numpy arrays, not functions
rgb_from_hed: NDArray[np.floating] # Hematoxylin + Eosin + DAB matrix
hed_from_rgb: NDArray[np.floating] # Inverse of rgb_from_hed
rgb_from_hdx: NDArray[np.floating] # Hematoxylin + DAB matrix
hdx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_hdx
rgb_from_fgx: NDArray[np.floating] # Feulgen + Light Green matrix
fgx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_fgx
rgb_from_bex: NDArray[np.floating] # Giemsa: Methyl Blue + Eosin matrix
bex_from_rgb: NDArray[np.floating] # Inverse of rgb_from_bex
rgb_from_rbd: NDArray[np.floating] # FastRed + FastBlue + DAB matrix
rbd_from_rgb: NDArray[np.floating] # Inverse of rgb_from_rbd
rgb_from_gdx: NDArray[np.floating] # Methyl Green + DAB matrix
gdx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_gdx
rgb_from_hax: NDArray[np.floating] # Hematoxylin + AEC matrix
hax_from_rgb: NDArray[np.floating] # Inverse of rgb_from_hax
rgb_from_bro: NDArray[np.floating] # Blue Anilline + Red Azocarmine + Orange-G matrix
bro_from_rgb: NDArray[np.floating] # Inverse of rgb_from_bro
rgb_from_bpx: NDArray[np.floating] # Methyl Blue + Ponceau Fuchsin matrix
bpx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_bpx
rgb_from_ahx: NDArray[np.floating] # Alcian Blue + Hematoxylin matrix
ahx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_ahx
rgb_from_hpx: NDArray[np.floating] # Hematoxylin + PAS matrix
hpx_from_rgb: NDArray[np.floating] # Inverse of rgb_from_hpxcolor_dict: Dict[str, Tuple[float, float, float]]
# Dictionary mapping color names to RGB tuples
# Contains standard color names like 'red', 'blue', 'green', etc.Install with Tessl CLI
npx tessl i tessl/pypi-scikit-image