albumentations.augmentations.functional
Functional implementations of image augmentation operations. This module contains low-level functions for various image augmentation techniques including color transformations, blur effects, tone curve adjustments, noise additions, and other visual modifications. These functions form the foundation for the transform classes and provide the core functionality for manipulating image data during the augmentation process.
Members
- classMacenkoNormalizer
- classSimpleNMF
- classStainNormalizer
- classVahadaneNormalizer
- functionadd_fog
- functionadd_gravel
- functionadd_noise
- functionadd_rain
- functionadd_shadow
- functionadd_snow_bleach
- functionadd_snow_texture
- functionadd_sun_flare_overlay
- functionadd_sun_flare_physics_based
- functionadjust_brightness_torchvision
- functionadjust_contrast_torchvision
- functionadjust_hue_torchvision
- functionadjust_saturation_torchvision
- functionapply_corner_illumination
- functionapply_gaussian_illumination
- functionapply_he_stain_augmentation
- functionapply_linear_illumination
- functionapply_plasma_brightness_contrast
- functionapply_plasma_shadow
- functionapply_salt_and_pepper
- functionauto_contrast
- functionbboxes_morphology
- functionchannel_shuffle
- functionchromatic_aberration
- functionclahe
- functionconvolve
- functioncreate_contrast_lut
- functioncreate_directional_gradient
- functiondilate
- functiondownscale
- functionequalize
- functionerode
- functionfancy_pca
- functiongamma_transform
- functiongenerate_constant_noise
- functiongenerate_noise
- functiongenerate_per_pixel_noise
- functiongenerate_plasma_pattern
- functiongenerate_random_values
- functiongenerate_shared_noise
- functiongenerate_snow_textures
- functionget_drop_mask
- functionget_fog_particle_radiuses
- functionget_histogram_bounds
- functionget_mask_array
- functionget_mud_params
- functionget_normalizer
- functionget_rain_params
- functionget_safe_brightness_contrast_params
- functionget_tissue_mask
- functiongrayscale_to_multichannel
- functionimage_compression
- functioninvert
- functioniso_noise
- functionlinear_transformation_rgb
- functionmorphology
- functionmove_tone_curve
- functionnoop
- functionnormalize_vectors
- functionorder_stains_combined
- functionpixel_dropout
- functionplanckian_jitter
- functionposterize
- functionprepare_drop_values
- functionrgb_to_optical_density
- functionsample_beta
- functionsample_gaussian
- functionsample_laplace
- functionsample_noise
- functionsample_uniform
- functionseparable_convolve
- functionsharpen_gaussian
- functionshift_hsv
- functionshot_noise
- functionslic
- functionsolarize
- functionspatter_mud
- functionspatter_rain
- functionsuperpixels
- functionto_gray
- functionto_gray_average
- functionto_gray_desaturation
- functionto_gray_from_lab
- functionto_gray_max
- functionto_gray_pca
- functionto_gray_weighted_average
- functionunsharp_mask
- functionvolume_channel_shuffle
- functionvolumes_channel_shuffle
MacenkoNormalizerclass
MacenkoNormalizer(
angular_percentile: float = 99
)
Macenko stain normalizer with optimized computations.
Parameters
Name | Type | Default | Description |
---|---|---|---|
angular_percentile | float | 99 | - |
SimpleNMFclass
SimpleNMF(
n_iter: int = 100
)
Parameters
Name | Type | Default | Description |
---|---|---|---|
n_iter | int | 100 | - |
StainNormalizerclass
StainNormalizer()
Base class for stain normalizers.
VahadaneNormalizerclass
VahadaneNormalizer()
add_fogfunction
add_fog(
img: np.ndarray,
fog_intensity: float,
alpha_coef: float,
fog_particle_positions: list[tuple[int, int]],
fog_particle_radiuses: list[int]
)
Add fog to an image. This function adds fog to an image by drawing fog particles on the image. The fog particles are drawn using the OpenCV function cv2.circle.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to add fog to. |
fog_intensity | float | - | The intensity of the fog effect, between 0 and 1. |
alpha_coef | float | - | The coefficient for the alpha blending. |
fog_particle_positions | list[tuple[int, int]] | - | The positions of the fog particles. |
fog_particle_radiuses | list[int] | - | The radiuses of the fog particles. |
Returns
- np.ndarray: The image with fog added.
add_gravelfunction
add_gravel(
img: np.ndarray,
gravels: list[Any]
)
Add gravel to an image. This function adds gravel to an image by drawing gravel particles on the image. The gravel particles are drawn using the OpenCV function cv2.circle.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to add gravel to. |
gravels | list[Any] | - | The gravel particles to draw on the image. |
Returns
- np.ndarray: The image with gravel added.
add_noisefunction
add_noise(
img: np.ndarray,
noise: np.ndarray
)
Add noise to an image. This function adds noise to an image by adding the noise to the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
noise | np.ndarray | - | Noise as a numpy array. |
Returns
- np.ndarray: The noise added to the image.
add_rainfunction
add_rain(
img: np.ndarray,
slant: float,
drop_length: int,
drop_width: int,
drop_color: tuple[int, int, int],
blur_value: int,
brightness_coefficient: float,
rain_drops: np.ndarray
)
Add rain to an image. This function adds rain to an image by drawing rain drops on the image. The rain drops are drawn using the OpenCV function cv2.polylines.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to add rain to. |
slant | float | - | The slant of the rain drops. |
drop_length | int | - | The length of the rain drops. |
drop_width | int | - | The width of the rain drops. |
drop_color | tuple[int, int, int] | - | The color of the rain drops. |
blur_value | int | - | The blur value of the rain drops. |
brightness_coefficient | float | - | The brightness coefficient of the rain drops. |
rain_drops | np.ndarray | - | The rain drops to draw on the image. |
Returns
- np.ndarray: The image with rain added.
add_shadowfunction
add_shadow(
img: np.ndarray,
vertices_list: list[np.ndarray],
intensities: np.ndarray
)
Add shadows to the image by reducing the intensity of the pixel values in specified regions.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. Multichannel images are supported. |
vertices_list | list[np.ndarray] | - | List of vertices for shadow polygons. |
intensities | np.ndarray | - | Array of shadow intensities. Range is [0, 1]. |
Returns
- np.ndarray: Image with shadows added.
References
- Automold--Road-Augmentation-Library: https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library
add_snow_bleachfunction
add_snow_bleach(
img: np.ndarray,
snow_point: float,
brightness_coeff: float
)
Adds a simple snow effect to the image by bleaching out pixels. This function simulates a basic snow effect by increasing the brightness of pixels that are above a certain threshold (snow_point). It operates in the HLS color space to modify the lightness channel.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. Can be either RGB uint8 or float32. |
snow_point | float | - | A float in the range [0, 1], scaled and adjusted to determine the threshold for pixel modification. Higher values result in less snow effect. |
brightness_coeff | float | - | Coefficient applied to increase the brightness of pixels below the snow_point threshold. Larger values lead to more pronounced snow effects. Should be greater than 1.0 for a visible effect. |
Returns
- np.ndarray: Image with simulated snow effect. The output has the same dtype as the input.
Notes
- This function converts the image to the HLS color space to modify the lightness channel. - The snow effect is created by selectively increasing the brightness of pixels. - This method tends to create a 'bleached' look, which may not be as realistic as more advanced snow simulation techniques. - The function automatically handles both uint8 and float32 input images.
References
- HLS Color Space: https://en.wikipedia.org/wiki/HSL_and_HSV
- Original implementation: https://github.com/UjjwalSaxena/Automold--Road-Augmentation-Library
add_snow_texturefunction
add_snow_texture(
img: np.ndarray,
snow_point: float,
brightness_coeff: float,
snow_texture: np.ndarray,
sparkle_mask: np.ndarray
)
Add a realistic snow effect to the input image. This function simulates snowfall by applying multiple visual effects to the image, including brightness adjustment, snow texture overlay, depth simulation, and color tinting. The result is a more natural-looking snow effect compared to simple pixel bleaching methods.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image in RGB format. |
snow_point | float | - | Coefficient that controls the amount and intensity of snow. Should be in the range [0, 1], where 0 means no snow and 1 means maximum snow effect. |
brightness_coeff | float | - | Coefficient for brightness adjustment to simulate the reflective nature of snow. Should be in the range [0, 1], where higher values result in a brighter image. |
snow_texture | np.ndarray | - | Snow texture. |
sparkle_mask | np.ndarray | - | Sparkle mask. |
Returns
- np.ndarray: Image with added snow effect. The output has the same dtype as the input.
Notes
This function works with both uint8 and float32 image types, automatically handling the conversion between them.
References
- Perlin Noise: https://en.wikipedia.org/wiki/Perlin_noise
- HSV Color Space: https://en.wikipedia.org/wiki/HSL_and_HSV
add_sun_flare_overlayfunction
add_sun_flare_overlay(
img: np.ndarray,
flare_center: tuple[float, float],
src_radius: int,
src_color: tuple[int, ...],
circles: list[Any]
)
Add a sun flare effect to an image using a simple overlay technique. This function creates a basic sun flare effect by overlaying multiple semi-transparent circles of varying sizes and intensities on the input image. The effect simulates a simple lens flare caused by bright light sources.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The input image. |
flare_center | tuple[float, float] | - | (x, y) coordinates of the flare center in pixel coordinates. |
src_radius | int | - | The radius of the main sun circle in pixels. |
src_color | tuple[int, ...] | - | The color of the sun, represented as a tuple of RGB values. |
circles | list[Any] | - | A list of tuples, each representing a circle that contributes to the flare effect. Each tuple contains: - alpha (float): The transparency of the circle (0.0 to 1.0). - center (tuple[int, int]): (x, y) coordinates of the circle center. - radius (int): The radius of the circle. - color (tuple[int, int, int]): RGB color of the circle. |
Returns
- np.ndarray: The output image with the sun flare effect added.
Notes
- This function uses a simple alpha blending technique to overlay flare elements. - The main sun is created as a gradient circle, fading from the center outwards. - Additional flare circles are added along an imaginary line from the sun's position. - This method is computationally efficient but may produce less realistic results compared to more advanced techniques.
References
- Alpha compositing: https://en.wikipedia.org/wiki/Alpha_compositing
- Lens flare: https://en.wikipedia.org/wiki/Lens_flare
add_sun_flare_physics_basedfunction
add_sun_flare_physics_based(
img: np.ndarray,
flare_center: tuple[int, int],
src_radius: int,
src_color: tuple[int, int, int],
circles: list[Any]
)
Add a more realistic sun flare effect to the image. This function creates a complex sun flare effect by simulating various optical phenomena that occur in real camera lenses when capturing bright light sources. The result is a more realistic and physically plausible lens flare effect.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. |
flare_center | tuple[int, int] | - | (x, y) coordinates of the sun's center in pixels. |
src_radius | int | - | Radius of the main sun circle in pixels. |
src_color | tuple[int, int, int] | - | Color of the sun in RGB format. |
circles | list[Any] | - | List of tuples, each representing a flare circle with parameters: (alpha, center, size, color) - alpha (float): Transparency of the circle (0.0 to 1.0). - center (tuple[int, int]): (x, y) coordinates of the circle center. - size (float): Size factor for the circle radius. - color (tuple[int, int, int]): RGB color of the circle. |
Returns
- np.ndarray: Image with added sun flare effect.
References
- Lens flare: https://en.wikipedia.org/wiki/Lens_flare
- Diffraction: https://en.wikipedia.org/wiki/Diffraction
- Chromatic aberration: https://en.wikipedia.org/wiki/Chromatic_aberration
- Screen blending: https://en.wikipedia.org/wiki/Blend_modes#Screen
adjust_brightness_torchvisionfunction
adjust_brightness_torchvision(
img: np.ndarray,
factor: np.ndarray
)
Adjust the brightness of an image. This function adjusts the brightness of an image by multiplying each pixel value by a factor. The brightness is adjusted by multiplying the image by the factor.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
factor | np.ndarray | - | The factor to adjust the brightness by. |
Returns
- np.ndarray: The adjusted image.
adjust_contrast_torchvisionfunction
adjust_contrast_torchvision(
img: np.ndarray,
factor: float
)
Adjust the contrast of an image. This function adjusts the contrast of an image by multiplying each pixel value by a factor. The contrast is adjusted by multiplying the image by the factor.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
factor | float | - | The factor to adjust the contrast by. |
Returns
- np.ndarray: The adjusted image.
adjust_hue_torchvisionfunction
adjust_hue_torchvision(
img: np.ndarray,
factor: float
)
Adjust the hue of an image. This function adjusts the hue of an image by adding a factor to the hue value.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. |
factor | float | - | The factor to adjust the hue by. |
Returns
- np.ndarray: The adjusted image.
adjust_saturation_torchvisionfunction
adjust_saturation_torchvision(
img: np.ndarray,
factor: float,
gamma: float = 0
)
Adjust the saturation of an image. This function adjusts the saturation of an image by multiplying each pixel value by a factor. The saturation is adjusted by multiplying the image by the factor.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
factor | float | - | The factor to adjust the saturation by. |
gamma | float | 0 | The gamma value to use for the adjustment. |
Returns
- np.ndarray: The adjusted image.
apply_corner_illuminationfunction
apply_corner_illumination(
img: np.ndarray,
intensity: float,
corner: Literal[0, 1, 2, 3]
)
Apply corner illumination to the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image |
intensity | float | - | Illumination intensity |
corner | One of:
| - | The corner to apply the illumination to. |
Returns
- np.ndarray: Image with corner illumination applied.
apply_gaussian_illuminationfunction
apply_gaussian_illumination(
img: np.ndarray,
intensity: float,
center: tuple[float, float],
sigma: float
)
Apply gaussian illumination to the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image |
intensity | float | - | Illumination intensity |
center | tuple[float, float] | - | The center of the illumination. |
sigma | float | - | The sigma of the illumination. |
apply_he_stain_augmentationfunction
apply_he_stain_augmentation(
img: np.ndarray,
stain_matrix: np.ndarray,
scale_factors: np.ndarray,
shift_values: np.ndarray,
augment_background: bool
)
Apply HE stain augmentation to an image. This function applies HE stain augmentation to an image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. |
stain_matrix | np.ndarray | - | Stain matrix. |
scale_factors | np.ndarray | - | Scale factors. |
shift_values | np.ndarray | - | Shift values. |
augment_background | bool | - | Whether to augment the background. |
Returns
- np.ndarray: Augmented image.
apply_linear_illuminationfunction
apply_linear_illumination(
img: np.ndarray,
intensity: float,
angle: float
)
Apply linear illumination to the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image |
intensity | float | - | Illumination intensity |
angle | float | - | Illumination angle in radians |
Returns
- np.ndarray: Image with linear illumination
apply_plasma_brightness_contrastfunction
apply_plasma_brightness_contrast(
img: np.ndarray,
brightness_factor: float,
contrast_factor: float,
plasma_pattern: np.ndarray
)
Apply plasma-based brightness and contrast adjustments. This function applies plasma-based brightness and contrast adjustments to an image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to apply the brightness and contrast adjustments to. |
brightness_factor | float | - | The brightness factor to apply. |
contrast_factor | float | - | The contrast factor to apply. |
plasma_pattern | np.ndarray | - | The plasma pattern to use for the brightness and contrast adjustments. |
Returns
- np.ndarray: The image with the brightness and contrast adjustments applied.
apply_plasma_shadowfunction
apply_plasma_shadow(
img: np.ndarray,
intensity: float,
plasma_pattern: np.ndarray
)
Apply plasma shadow to the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image |
intensity | float | - | Shadow intensity |
plasma_pattern | np.ndarray | - | Plasma pattern to use |
Returns
- np.ndarray: Image with plasma shadow
apply_salt_and_pepperfunction
apply_salt_and_pepper(
img: np.ndarray,
salt_mask: np.ndarray,
pepper_mask: np.ndarray
)
Apply salt and pepper noise to an image. This function applies salt and pepper noise to an image using pre-computed masks.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to apply salt and pepper noise to. |
salt_mask | np.ndarray | - | The salt mask to use for the salt and pepper noise. |
pepper_mask | np.ndarray | - | The pepper mask to use for the salt and pepper noise. |
Returns
- np.ndarray: The image with salt and pepper noise applied.
auto_contrastfunction
auto_contrast(
img: np.ndarray,
cutoff: float,
ignore: int | None,
method: Literal['cdf', 'pil']
)
Apply automatic contrast enhancement.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image |
cutoff | float | - | Cutoff percentage for histogram |
ignore | One of:
| - | Value to ignore in histogram |
method | One of:
| - | Method to use for contrast enhancement |
Returns
- np.ndarray: Image with enhanced contrast
bboxes_morphologyfunction
bboxes_morphology(
bboxes: np.ndarray,
kernel: np.ndarray,
operation: Literal['dilation', 'erosion'],
image_shape: tuple[int, int]
)
Apply morphology to bounding boxes. This function applies morphology to bounding boxes by first converting the bounding boxes to a mask and then applying the morphology to the mask.
Parameters
Name | Type | Default | Description |
---|---|---|---|
bboxes | np.ndarray | - | Bounding boxes as a numpy array. |
kernel | np.ndarray | - | Kernel as a numpy array. |
operation | One of:
| - | The operation to apply. |
image_shape | tuple[int, int] | - | The shape of the image. |
Returns
- np.ndarray: The morphology applied to the bounding boxes.
channel_shufflefunction
channel_shuffle(
img: np.ndarray,
channels_shuffled: list[int]
)
Shuffle the channels of an image. This function shuffles the channels of an image by using the cv2.mixChannels function. The channels are shuffled according to the channels_shuffled array.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to shuffle. |
channels_shuffled | list[int] | - | The array of channels to shuffle. |
Returns
- np.ndarray: The shuffled image.
chromatic_aberrationfunction
chromatic_aberration(
img: np.ndarray,
primary_distortion_red: float,
secondary_distortion_red: float,
primary_distortion_blue: float,
secondary_distortion_blue: float,
interpolation: int
)
Apply chromatic aberration to an image. This function applies chromatic aberration to an image by distorting the red and blue channels.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
primary_distortion_red | float | - | The primary distortion of the red channel. |
secondary_distortion_red | float | - | The secondary distortion of the red channel. |
primary_distortion_blue | float | - | The primary distortion of the blue channel. |
secondary_distortion_blue | float | - | The secondary distortion of the blue channel. |
interpolation | int | - | The interpolation method to use. |
Returns
- np.ndarray: The chromatic aberration applied to the image.
clahefunction
clahe(
img: np.ndarray,
clip_limit: float,
tile_grid_size: tuple[int, int]
)
Apply Contrast Limited Adaptive Histogram Equalization (CLAHE) to the input image. This function enhances the contrast of the input image using CLAHE. For color images, it converts the image to the LAB color space, applies CLAHE to the L channel, and then converts the image back to RGB.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. Can be grayscale (2D array) or RGB (3D array). |
clip_limit | float | - | Threshold for contrast limiting. Higher values give more contrast. |
tile_grid_size | tuple[int, int] | - | Size of grid for histogram equalization. Width and height of the grid. |
Returns
- np.ndarray: Image with CLAHE applied. The output has the same dtype as the input.
Example
>>> import numpy as np
>>> img = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> result = clahe(img, clip_limit=2.0, tile_grid_size=(8, 8))
>>> assert result.shape == img.shape
>>> assert result.dtype == img.dtype
Notes
- If the input image is float32, it's temporarily converted to uint8 for processing and then converted back to float32. - For color images, CLAHE is applied only to the luminance channel in the LAB color space.
convolvefunction
convolve(
img: np.ndarray,
kernel: np.ndarray
)
Convolve an image with a kernel. This function convolves an image with a kernel.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. |
kernel | np.ndarray | - | Kernel. |
Returns
- np.ndarray: Convolved image.
create_contrast_lutfunction
create_contrast_lut(
hist: np.ndarray,
min_intensity: int,
max_intensity: int,
max_value: int,
method: Literal['cdf', 'pil']
)
Create lookup table for contrast adjustment. This function creates a lookup table for contrast adjustment.
Parameters
Name | Type | Default | Description |
---|---|---|---|
hist | np.ndarray | - | Histogram of the image. |
min_intensity | int | - | Minimum intensity of the histogram. |
max_intensity | int | - | Maximum intensity of the histogram. |
max_value | int | - | Maximum value of the lookup table. |
method | One of:
| - | Method to use for contrast enhancement. |
Returns
- np.ndarray: Lookup table for contrast adjustment.
create_directional_gradientfunction
create_directional_gradient(
height: int,
width: int,
angle: float
)
Create a directional gradient in [0, 1] range. This function creates a directional gradient in the [0, 1] range.
Parameters
Name | Type | Default | Description |
---|---|---|---|
height | int | - | The height of the image. |
width | int | - | The width of the image. |
angle | float | - | The angle of the gradient. |
Returns
- np.ndarray: The directional gradient.
dilatefunction
dilate(
img: np.ndarray,
kernel: np.ndarray
)
Apply dilation to an image. This function applies dilation to an image using the cv2.dilate function.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
kernel | np.ndarray | - | Kernel as a numpy array. |
Returns
- np.ndarray: The dilated image.
downscalefunction
downscale(
img: np.ndarray,
scale: float,
down_interpolation: int,
up_interpolation: int
)
Downscale and upscale an image. This function downscales and upscales an image using the specified interpolation methods. The downscaling and upscaling are performed using the cv2.resize function.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
scale | float | - | The scale factor for the downscaling and upscaling. |
down_interpolation | int | - | The interpolation method for the downscaling. |
up_interpolation | int | - | The interpolation method for the upscaling. |
Returns
- np.ndarray: The downscaled and upscaled image.
equalizefunction
equalize(
img: np.ndarray,
mask: np.ndarray | None = None,
mode: Literal['cv', 'pil'] = cv,
by_channels: bool = True
)
Apply histogram equalization to the input image. This function enhances the contrast of the input image by equalizing its histogram. It supports both grayscale and color images, and can operate on individual channels or on the luminance channel of the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. Can be grayscale (2D array) or RGB (3D array). |
mask | One of:
| None | Optional mask to apply the equalization selectively. If provided, must have the same shape as the input image. Default: None. |
mode | One of:
| cv | The backend to use for equalization. Can be either "cv" for OpenCV or "pil" for Pillow-style equalization. Default: "cv". |
by_channels | bool | True | If True, applies equalization to each channel independently. If False, converts the image to YCrCb color space and equalizes only the luminance channel. Only applicable to color images. Default: True. |
Returns
- np.ndarray: Equalized image. The output has the same dtype as the input.
Example
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>> equalized = A.equalize(image, mode="cv", by_channels=True)
>>> assert equalized.shape == image.shape
>>> assert equalized.dtype == image.dtype
Notes
- If the input image is not uint8, it will be temporarily converted to uint8 for processing and then converted back to its original dtype. - For color images, when by_channels=False, the image is converted to YCrCb color space, equalized on the Y channel, and then converted back to RGB. - The function preserves the original number of channels in the image.
erodefunction
erode(
img: np.ndarray,
kernel: np.ndarray
)
Apply erosion to an image. This function applies erosion to an image using the cv2.erode function.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
kernel | np.ndarray | - | Kernel as a numpy array. |
Returns
- np.ndarray: The eroded image.
fancy_pcafunction
fancy_pca(
img: np.ndarray,
alpha_vector: np.ndarray
)
Perform 'Fancy PCA' augmentation on an image with any number of channels.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image |
alpha_vector | np.ndarray | - | Vector of scale factors for each principal component. Should have the same length as the number of channels in the image. |
Returns
- np.ndarray: Augmented image of the same shape, type, and range as the input.
Notes
- This function generalizes the Fancy PCA augmentation to work with any number of channels. - It preserves the original range of the image ([0, 255] for uint8, [0, 1] for float32). - For single-channel images, the augmentation is applied as a simple scaling of pixel intensity variation. - For multi-channel images, PCA is performed on the entire image, treating each pixel as a point in N-dimensional space (where N is the number of channels). - The augmentation preserves the correlation between channels while adding controlled noise. - Computation time may increase significantly for images with a large number of channels.
References
- ImageNet classification with deep convolutional neural networks: Krizhevsky, A., Sutskever, I., & Hinton, G. E. (2012): In Advances in neural information processing systems (pp. 1097-1105).
gamma_transformfunction
gamma_transform(
img: np.ndarray,
gamma: float
)
Apply gamma transformation to an image. This function applies gamma transformation to an image by raising each pixel value to the power of gamma. The result is a non-linear transformation that can enhance or reduce the contrast of the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to apply gamma transformation to. |
gamma | float | - | The gamma value to apply. |
Returns
- np.ndarray: The gamma transformed image.
generate_constant_noisefunction
generate_constant_noise(
noise_type: Literal['uniform', 'gaussian', 'laplace', 'beta'],
shape: tuple[int, ...],
params: dict[str, Any],
max_value: float,
random_generator: np.random.Generator
)
Generate constant noise. This function generates constant noise by sampling from the noise distribution.
Parameters
Name | Type | Default | Description |
---|---|---|---|
noise_type | One of:
| - | The type of noise to generate. |
shape | tuple[int, ...] | - | The shape of the noise to generate. |
params | dict[str, Any] | - | The parameters of the noise to generate. |
max_value | float | - | The maximum value of the noise to generate. |
random_generator | np.random.Generator | - | The random number generator to use. |
Returns
- np.ndarray: The constant noise generated.
generate_noisefunction
generate_noise(
noise_type: Literal['uniform', 'gaussian', 'laplace', 'beta'],
spatial_mode: Literal['constant', 'per_pixel', 'shared'],
shape: tuple[int, ...],
params: dict[str, Any] | None,
max_value: float,
approximation: float,
random_generator: np.random.Generator
)
Generate noise with optional approximation for speed. This function generates noise with optional approximation for speed.
Parameters
Name | Type | Default | Description |
---|---|---|---|
noise_type | One of:
| - | The type of noise to generate. |
spatial_mode | One of:
| - | The spatial mode to use. |
shape | tuple[int, ...] | - | The shape of the noise to generate. |
params | One of:
| - | The parameters of the noise to generate. |
max_value | float | - | The maximum value of the noise to generate. |
approximation | float | - | The approximation to use for the noise to generate. |
random_generator | np.random.Generator | - | The random number generator to use. |
Returns
- np.ndarray: The noise generated.
generate_per_pixel_noisefunction
generate_per_pixel_noise(
noise_type: Literal['uniform', 'gaussian', 'laplace', 'beta'],
shape: tuple[int, ...],
params: dict[str, Any],
max_value: float,
random_generator: np.random.Generator
)
Generate per-pixel noise. This function generates per-pixel noise by sampling from the noise distribution.
Parameters
Name | Type | Default | Description |
---|---|---|---|
noise_type | One of:
| - | The type of noise to generate. |
shape | tuple[int, ...] | - | The shape of the noise to generate. |
params | dict[str, Any] | - | The parameters of the noise to generate. |
max_value | float | - | The maximum value of the noise to generate. |
random_generator | np.random.Generator | - | The random number generator to use. |
Returns
- np.ndarray: The per-pixel noise generated.
generate_plasma_patternfunction
generate_plasma_pattern(
target_shape: tuple[int, int],
roughness: float,
random_generator: np.random.Generator
)
Generate a plasma pattern. This function generates a plasma pattern using the diamond-square algorithm.
Parameters
Name | Type | Default | Description |
---|---|---|---|
target_shape | tuple[int, int] | - | The shape of the plasma pattern to generate. |
roughness | float | - | The roughness of the plasma pattern. |
random_generator | np.random.Generator | - | The random number generator to use. |
Returns
- np.ndarray: The plasma pattern generated.
generate_random_valuesfunction
generate_random_values(
channels: int,
dtype: np.dtype,
random_generator: np.random.Generator
)
Generate random values.
Parameters
Name | Type | Default | Description |
---|---|---|---|
channels | int | - | Number of channels |
dtype | np.dtype | - | Data type of the output array |
random_generator | np.random.Generator | - | Random number generator |
Returns
- np.ndarray: Random values
generate_shared_noisefunction
generate_shared_noise(
noise_type: Literal['uniform', 'gaussian', 'laplace', 'beta'],
shape: tuple[int, ...],
params: dict[str, Any],
max_value: float,
random_generator: np.random.Generator
)
Generate shared noise.
Parameters
Name | Type | Default | Description |
---|---|---|---|
noise_type | One of:
| - | Type of noise to generate |
shape | tuple[int, ...] | - | Shape of the output array |
params | dict[str, Any] | - | Distribution parameters |
max_value | float | - | Maximum value for the noise |
random_generator | np.random.Generator | - | Random number generator |
Returns
- np.ndarray: Generated noise
generate_snow_texturesfunction
generate_snow_textures(
img_shape: tuple[int, int],
random_generator: np.random.Generator
)
Generate snow texture and sparkle mask.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img_shape | tuple[int, int] | - | Image shape. |
random_generator | np.random.Generator | - | Random generator to use. |
Returns
- tuple[np.ndarray, np.ndarray]: Tuple of (snow_texture, sparkle_mask) arrays.
get_drop_maskfunction
get_drop_mask(
shape: tuple[int, ...],
per_channel: bool,
dropout_prob: float,
random_generator: np.random.Generator
)
Generate dropout mask. This function generates a dropout mask.
Parameters
Name | Type | Default | Description |
---|---|---|---|
shape | tuple[int, ...] | - | Shape of the output mask |
per_channel | bool | - | Whether to apply dropout per channel |
dropout_prob | float | - | Dropout probability |
random_generator | np.random.Generator | - | Random number generator |
Returns
- np.ndarray: Dropout mask
get_fog_particle_radiusesfunction
get_fog_particle_radiuses(
img_shape: tuple[int, int],
num_particles: int,
fog_intensity: float,
random_generator: np.random.Generator
)
Generate radiuses for fog particles.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img_shape | tuple[int, int] | - | Image shape. |
num_particles | int | - | Number of fog particles. |
fog_intensity | float | - | Intensity of the fog effect, between 0 and 1. |
random_generator | np.random.Generator | - | Random generator to use. |
Returns
- list[int]: List of radiuses for each fog particle.
get_histogram_boundsfunction
get_histogram_bounds(
hist: np.ndarray,
cutoff: float
)
Get the low and high bounds of the histogram. This function gets the low and high bounds of the histogram.
Parameters
Name | Type | Default | Description |
---|---|---|---|
hist | np.ndarray | - | Histogram of the image. |
cutoff | float | - | Cutoff percentage for histogram. |
Returns
- tuple[int, int]: Low and high bounds of the histogram.
get_mask_arrayfunction
get_mask_array(
data: dict[str, Any]
)
Get mask array from input data if it exists.
Parameters
Name | Type | Default | Description |
---|---|---|---|
data | dict[str, Any] | - | - |
get_mud_paramsfunction
get_mud_params(
liquid_layer: np.ndarray,
color: np.ndarray,
cutout_threshold: float,
sigma: float,
intensity: float,
random_generator: np.random.Generator
)
Generate parameters for mud effect. This function generates parameters for a mud effect.
Parameters
Name | Type | Default | Description |
---|---|---|---|
liquid_layer | np.ndarray | - | Liquid layer of the image. |
color | np.ndarray | - | Color of the mud. |
cutout_threshold | float | - | Cutout threshold for the mud. |
sigma | float | - | Sigma for the Gaussian blur. |
intensity | float | - | Intensity of the mud. |
random_generator | np.random.Generator | - | Random number generator. |
Returns
- dict[str, Any]: Parameters for the mud effect.
get_normalizerfunction
get_normalizer(
method: Literal['vahadane', 'macenko']
)
Get stain normalizer based on method. This function gets a stain normalizer based on a method.
Parameters
Name | Type | Default | Description |
---|---|---|---|
method | One of:
| - | Method to use for stain normalization. |
Returns
- StainNormalizer: Stain normalizer.
get_rain_paramsfunction
get_rain_params(
liquid_layer: np.ndarray,
color: np.ndarray,
intensity: float
)
Generate parameters for rain effect. This function generates parameters for a rain effect.
Parameters
Name | Type | Default | Description |
---|---|---|---|
liquid_layer | np.ndarray | - | Liquid layer of the image. |
color | np.ndarray | - | Color of the rain. |
intensity | float | - | Intensity of the rain. |
Returns
- dict[str, Any]: Parameters for the rain effect.
get_safe_brightness_contrast_paramsfunction
get_safe_brightness_contrast_params(
alpha: float,
beta: float,
max_value: float
)
Get safe brightness and contrast parameters.
Parameters
Name | Type | Default | Description |
---|---|---|---|
alpha | float | - | Contrast factor |
beta | float | - | Brightness factor |
max_value | float | - | Maximum pixel value |
Returns
- tuple[float, float]: Safe alpha and beta values
get_tissue_maskfunction
get_tissue_mask(
img: np.ndarray,
threshold: float = 0.85
)
Get tissue mask from image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image |
threshold | float | 0.85 | Threshold for tissue detection. Default: 0.85 |
Returns
- np.ndarray: Binary mask where True indicates tissue regions
grayscale_to_multichannelfunction
grayscale_to_multichannel(
grayscale_image: np.ndarray,
num_output_channels: int = 3
)
Convert a grayscale image to a multi-channel image. This function takes a 2D grayscale image or a 3D image with a single channel and converts it to a multi-channel image by repeating the grayscale data across the specified number of channels.
Parameters
Name | Type | Default | Description |
---|---|---|---|
grayscale_image | np.ndarray | - | Input grayscale image. Can be 2D (height, width) or 3D (height, width, 1). |
num_output_channels | int | 3 | Number of channels in the output image. Defaults to 3. |
Returns
- np.ndarray: Multi-channel image with shape (height, width, num_channels)
image_compressionfunction
image_compression(
img: np.ndarray,
quality: int,
image_type: Literal['.jpg', '.webp']
)
Compress the image using JPEG or WebP compression.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image |
quality | int | - | Quality of compression in range [1, 100] |
image_type | One of:
| - | Type of compression to use |
Returns
- np.ndarray: Compressed image
invertfunction
invert(
img: np.ndarray
)
Invert the colors of an image. This function inverts the colors of an image by subtracting each pixel value from the maximum possible value. The result is a negative of the original image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to invert. |
Returns
- np.ndarray: The inverted image.
iso_noisefunction
iso_noise(
image: np.ndarray,
color_shift: float,
intensity: float,
random_generator: np.random.Generator
)
Apply poisson noise to an image to simulate camera sensor noise.
Parameters
Name | Type | Default | Description |
---|---|---|---|
image | np.ndarray | - | Input image. Currently, only RGB images are supported. |
color_shift | float | - | The amount of color shift to apply. |
intensity | float | - | Multiplication factor for noise values. Values of ~0.5 produce a noticeable, yet acceptable level of noise. |
random_generator | np.random.Generator | - | If specified, this will be random generator used for noise generation. |
Returns
- np.ndarray: The noised image.
linear_transformation_rgbfunction
linear_transformation_rgb(
img: np.ndarray,
transformation_matrix: np.ndarray
)
Apply a linear transformation to the RGB channels of an image. This function applies a linear transformation matrix to the RGB channels of an image. The transformation matrix is a 3x3 matrix that maps the RGB values to new values.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. Can be grayscale (2D array) or RGB (3D array). |
transformation_matrix | np.ndarray | - | 3x3 transformation matrix. |
Returns
- np.ndarray: Image with the linear transformation applied. The output has the same dtype as the input.
morphologyfunction
morphology(
img: np.ndarray,
kernel: np.ndarray,
operation: Literal['dilation', 'erosion']
)
Apply morphology to an image. This function applies morphology to an image using the cv2.morphologyEx function.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
kernel | np.ndarray | - | Kernel as a numpy array. |
operation | One of:
| - | The operation to apply. |
Returns
- np.ndarray: The morphology applied to the image.
move_tone_curvefunction
move_tone_curve(
img: np.ndarray,
low_y: float | np.ndarray,
high_y: float | np.ndarray
)
Rescales the relationship between bright and dark areas of the image by manipulating its tone curve.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Any number of channels |
low_y | One of:
| - | per-channel or single y-position of a Bezier control point used to adjust the tone curve, must be in range [0, 1] |
high_y | One of:
| - | per-channel or single y-position of a Bezier control point used to adjust image tone curve, must be in range [0, 1] |
Returns
- np.ndarray: Image with adjusted tone curve
noopfunction
noop(
input_obj: Any
)
No-op function. This function is a no-op and returns the input object unchanged. It is used to satisfy the type checker requirements for the `noop` function.
Parameters
Name | Type | Default | Description |
---|---|---|---|
input_obj | Any | - | The input object to return unchanged. |
Returns
- Any: The input object unchanged.
normalize_vectorsfunction
normalize_vectors(
vectors: np.ndarray
)
Normalize vectors. This function normalizes vectors.
Parameters
Name | Type | Default | Description |
---|---|---|---|
vectors | np.ndarray | - | Vectors to normalize. |
Returns
- np.ndarray: Normalized vectors.
order_stains_combinedfunction
order_stains_combined(
stain_colors: np.ndarray
)
Order stains using a combination of methods. This combines both angular information and spectral characteristics for more robust identification.
Parameters
Name | Type | Default | Description |
---|---|---|---|
stain_colors | np.ndarray | - | Stain colors. |
Returns
- tuple[int, int]: Hematoxylin and eosin indices.
pixel_dropoutfunction
pixel_dropout(
image: np.ndarray,
drop_mask: np.ndarray,
drop_values: np.ndarray
)
Apply pixel dropout to the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
image | np.ndarray | - | Input image |
drop_mask | np.ndarray | - | Boolean mask indicating which pixels to drop |
drop_values | np.ndarray | - | Values to replace dropped pixels with |
Returns
- np.ndarray: Image with dropped pixels
planckian_jitterfunction
planckian_jitter(
img: np.ndarray,
temperature: int,
mode: Literal['blackbody', 'cied']
)
Apply Planckian jitter to an image. This function applies Planckian jitter to an image by linearly interpolating between the two closest temperatures in the PLANCKIAN_COEFFS dictionary.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
temperature | int | - | The temperature to apply. |
mode | One of:
| - | The mode to use. |
Returns
- np.ndarray: The Planckian jitter applied to the image.
posterizefunction
posterize(
img: np.ndarray,
bits: Literal[1, 2, 3, 4, 5, 6, 7] | list[Literal[1, 2, 3, 4, 5, 6, 7]]
)
Reduce the number of bits for each color channel by keeping only the highest N bits.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. Can be single or multi-channel. |
bits | One of:
| - | Number of high bits to keep.. Can be either: - A single value to apply the same bit reduction to all channels - A list of values to apply different bit reduction per channel. Length of list must match number of channels in image. |
Returns
- np.ndarray: Image with reduced bit depth. Has same shape and dtype as input.
Notes
- The transform keeps the N highest bits and sets all other bits to 0 - For example, if bits=3: - Original value: 11010110 (214) - Keep 3 bits: 11000000 (192) - The number of unique colors per channel will be 2^bits - Higher bits values = more colors = more subtle effect - Lower bits values = fewer colors = more dramatic posterization
prepare_drop_valuesfunction
prepare_drop_values(
array: np.ndarray,
value: float | Sequence[float] | np.ndarray | None,
random_generator: np.random.Generator
)
Prepare values to fill dropped pixels.
Parameters
Name | Type | Default | Description |
---|---|---|---|
array | np.ndarray | - | Input array to determine shape and dtype |
value | One of:
| - | User-specified drop values or None for random |
random_generator | np.random.Generator | - | Random number generator |
Returns
- np.ndarray: Array of values matching input shape
rgb_to_optical_densityfunction
rgb_to_optical_density(
img: np.ndarray,
eps: float = 1e-06
)
Convert RGB image to optical density. This function converts an RGB image to optical density.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. |
eps | float | 1e-06 | Epsilon value. |
Returns
- np.ndarray: Optical density image.
sample_betafunction
sample_beta(
size: tuple[int, ...],
params: dict[str, Any],
random_generator: np.random.Generator
)
Sample from Beta distribution. This function samples from a Beta distribution.
Parameters
Name | Type | Default | Description |
---|---|---|---|
size | tuple[int, ...] | - | The size of the noise to generate. |
params | dict[str, Any] | - | The parameters of the noise to generate. |
random_generator | np.random.Generator | - | The random number generator to use. |
Returns
- np.ndarray: The Beta noise sampled.
sample_gaussianfunction
sample_gaussian(
size: tuple[int, ...],
params: dict[str, Any],
random_generator: np.random.Generator
)
Sample from Gaussian distribution. This function samples from a Gaussian distribution.
Parameters
Name | Type | Default | Description |
---|---|---|---|
size | tuple[int, ...] | - | The size of the noise to generate. |
params | dict[str, Any] | - | The parameters of the noise to generate. |
random_generator | np.random.Generator | - | The random number generator to use. |
Returns
- np.ndarray: The Gaussian noise sampled.
sample_laplacefunction
sample_laplace(
size: tuple[int, ...],
params: dict[str, Any],
random_generator: np.random.Generator
)
Sample from Laplace distribution. This function samples from a Laplace distribution.
Parameters
Name | Type | Default | Description |
---|---|---|---|
size | tuple[int, ...] | - | The size of the noise to generate. |
params | dict[str, Any] | - | The parameters of the noise to generate. |
random_generator | np.random.Generator | - | The random number generator to use. |
Returns
- np.ndarray: The Laplace noise sampled.
sample_noisefunction
sample_noise(
noise_type: Literal['uniform', 'gaussian', 'laplace', 'beta'],
size: tuple[int, ...],
params: dict[str, Any],
max_value: float,
random_generator: np.random.Generator
)
Sample from specific noise distribution. This function samples from a specific noise distribution.
Parameters
Name | Type | Default | Description |
---|---|---|---|
noise_type | One of:
| - | The type of noise to generate. |
size | tuple[int, ...] | - | The size of the noise to generate. |
params | dict[str, Any] | - | The parameters of the noise to generate. |
max_value | float | - | The maximum value of the noise to generate. |
random_generator | np.random.Generator | - | The random number generator to use. |
Returns
- np.ndarray: The noise sampled.
sample_uniformfunction
sample_uniform(
size: tuple[int, ...],
params: dict[str, Any],
random_generator: np.random.Generator
)
Sample from uniform distribution.
Parameters
Name | Type | Default | Description |
---|---|---|---|
size | tuple[int, ...] | - | Size of the output array |
params | dict[str, Any] | - | Distribution parameters |
random_generator | np.random.Generator | - | Random number generator |
Returns
- np.ndarray | float: Sampled values
separable_convolvefunction
separable_convolve(
img: np.ndarray,
kernel: np.ndarray
)
Convolve an image with a separable kernel. This function convolves an image with a separable kernel.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image. |
kernel | np.ndarray | - | Kernel. |
Returns
- np.ndarray: Convolved image.
sharpen_gaussianfunction
sharpen_gaussian(
img: np.ndarray,
alpha: float,
kernel_size: int,
sigma: float
)
Sharpen image using Gaussian blur. This function sharpens an image using a Gaussian blur.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to sharpen. |
alpha | float | - | The alpha value to use for the sharpening. |
kernel_size | int | - | The kernel size to use for the Gaussian blur. |
sigma | float | - | The sigma value to use for the Gaussian blur. |
Returns
- np.ndarray: The sharpened image.
shift_hsvfunction
shift_hsv(
img: np.ndarray,
hue_shift: float,
sat_shift: float,
val_shift: float
)
Shift the hue, saturation, and value of an image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to shift. |
hue_shift | float | - | The amount to shift the hue. |
sat_shift | float | - | The amount to shift the saturation. |
val_shift | float | - | The amount to shift the value. |
Returns
- np.ndarray: The shifted image.
shot_noisefunction
shot_noise(
img: np.ndarray,
scale: float,
random_generator: np.random.Generator
)
Apply shot noise to the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image |
scale | float | - | Scale factor for the noise |
random_generator | np.random.Generator | - | Random number generator |
Returns
- np.ndarray: Image with shot noise
slicfunction
slic(
image: np.ndarray,
n_segments: int,
compactness: float = 10.0,
max_iterations: int = 10
)
Simple Linear Iterative Clustering (SLIC) superpixel segmentation using OpenCV and NumPy.
Parameters
Name | Type | Default | Description |
---|---|---|---|
image | np.ndarray | - | Input image (2D or 3D numpy array). |
n_segments | int | - | Approximate number of superpixels to generate. |
compactness | float | 10.0 | Balance between color proximity and space proximity. |
max_iterations | int | 10 | Maximum number of iterations for k-means. |
Returns
- np.ndarray: Segmentation mask where each superpixel has a unique label.
solarizefunction
solarize(
img: np.ndarray,
threshold: float
)
Invert all pixel values above a threshold.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | The image to solarize. Can be uint8 or float32. |
threshold | float | - | Normalized threshold value in range [0, 1]. For uint8 images: pixels above threshold * 255 are inverted For float32 images: pixels above threshold are inverted |
Returns
- np.ndarray: Solarized image.
Notes
The threshold is normalized to [0, 1] range for both uint8 and float32 images. For uint8 images, the threshold is internally scaled by 255.
spatter_mudfunction
spatter_mud(
img: np.ndarray,
non_mud: np.ndarray,
mud: np.ndarray
)
Apply spatter mud to an image. This function applies spatter mud to an image by adding the mud to the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
non_mud | np.ndarray | - | Non-mud image as a numpy array. |
mud | np.ndarray | - | Mud image as a numpy array. |
Returns
- np.ndarray: The spatter mud applied to the image.
spatter_rainfunction
spatter_rain(
img: np.ndarray,
rain: np.ndarray
)
Apply spatter rain to an image. This function applies spatter rain to an image by adding the rain to the image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
rain | np.ndarray | - | Rain image as a numpy array. |
Returns
- np.ndarray: The spatter rain applied to the image.
superpixelsfunction
superpixels(
image: np.ndarray,
n_segments: int,
replace_samples: Sequence[bool],
max_size: int | None,
interpolation: int
)
Apply superpixels to an image. This function applies superpixels to an image using the SLIC algorithm. The superpixels are applied by replacing the pixels in the image with the mean intensity of the superpixel.
Parameters
Name | Type | Default | Description |
---|---|---|---|
image | np.ndarray | - | Input image as a numpy array. |
n_segments | int | - | The number of segments to use for the superpixels. |
replace_samples | Sequence[bool] | - | The samples to replace. |
max_size | One of:
| - | The maximum size of the superpixels. |
interpolation | int | - | The interpolation method to use. |
Returns
- np.ndarray: The superpixels applied to the image.
to_grayfunction
to_gray(
img: np.ndarray,
num_output_channels: int,
method: Literal['weighted_average', 'from_lab', 'desaturation', 'average', 'max', 'pca']
)
Convert an image to grayscale using a specified method. This function converts an image to grayscale using a specified method. The method can be one of the following: - "weighted_average": Use the weighted average method. - "from_lab": Use the L channel from the LAB color space. - "desaturation": Use the desaturation method. - "average": Use the average method. - "max": Use the maximum channel value method. - "pca": Use the Principal Component Analysis method.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
num_output_channels | int | - | The number of channels in the output image. |
method | One of:
| - | The method to use for grayscale conversion. |
Returns
- np.ndarray: Grayscale image as a 2D numpy array.
to_gray_averagefunction
to_gray_average(
img: np.ndarray
)
Convert an image to grayscale using the average method. This function computes the arithmetic mean across all channels for each pixel, resulting in a grayscale representation of the image. Key aspects of this method: 1. It treats all channels equally, regardless of their perceptual importance. 2. Works with any number of channels, making it versatile for various image types. 3. Simple and fast to compute, but may not accurately represent perceived brightness. 4. For RGB images, the formula is: Gray = (R + G + B) / 3 Note: This method may produce different results compared to weighted methods (like RGB weighted average) which account for human perception of color brightness. It may also produce unexpected results for images with alpha channels or non-color data in additional channels.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. Can be any number of channels. |
Returns
- np.ndarray: Grayscale image as a 2D numpy array. The output data type
to_gray_desaturationfunction
to_gray_desaturation(
img: np.ndarray
)
Convert an image to grayscale using the desaturation method.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. |
Returns
- np.ndarray: Grayscale image as a 2D numpy array.
to_gray_from_labfunction
to_gray_from_lab(
img: np.ndarray
)
Convert an RGB image to grayscale using the L channel from the LAB color space. This function converts the RGB image to the LAB color space and extracts the L channel. The LAB color space is designed to approximate human vision, where L represents lightness. Key aspects of this method: 1. The L channel represents the lightness of each pixel, ranging from 0 (black) to 100 (white). 2. It's more perceptually uniform than RGB, meaning equal changes in L values correspond to roughly equal changes in perceived lightness. 3. The L channel is independent of the color information (A and B channels), making it suitable for grayscale conversion. This method can be particularly useful when you want a grayscale image that closely matches human perception of lightness, potentially preserving more perceived contrast than simple RGB-based methods.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input RGB image as a numpy array. |
Returns
- np.ndarray: Grayscale image as a 2D numpy array, representing the L (lightness) channel.
to_gray_maxfunction
to_gray_max(
img: np.ndarray
)
Convert an image to grayscale using the maximum channel value method. This function takes the maximum value across all channels for each pixel, resulting in a grayscale image that preserves the brightest parts of the original image. Key aspects of this method: 1. Works with any number of channels, making it versatile for various image types. 2. For 3-channel (e.g., RGB) images, this method is equivalent to extracting the V (Value) channel from the HSV color space. 3. Preserves the brightest parts of the image but may lose some color contrast information. 4. Simple and fast to compute. Note: - This method tends to produce brighter grayscale images compared to other conversion methods, as it always selects the highest intensity value from the channels. - For RGB images, it may not accurately represent perceived brightness as it doesn't account for human color perception.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array. Can be any number of channels. |
Returns
- np.ndarray: Grayscale image as a 2D numpy array. The output data type
Notes
- This method tends to produce brighter grayscale images compared to other conversion methods, as it always selects the highest intensity value from the channels. - For RGB images, it may not accurately represent perceived brightness as it doesn't account for human color perception.
to_gray_pcafunction
to_gray_pca(
img: np.ndarray
)
Convert an image to grayscale using Principal Component Analysis (PCA). This function applies PCA to reduce a multi-channel image to a single channel, effectively creating a grayscale representation that captures the maximum variance in the color data.
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input image as a numpy array with shape (height, width, channels). |
Returns
- np.ndarray: Grayscale image as a 2D numpy array with shape (height, width).
Notes
This method can potentially preserve more information from the original image compared to standard weighted average methods, as it accounts for the correlations between color channels.
to_gray_weighted_averagefunction
to_gray_weighted_average(
img: np.ndarray
)
Convert an RGB image to grayscale using the weighted average method. This function uses OpenCV's cvtColor function with COLOR_RGB2GRAY conversion, which applies the following formula: Y = 0.299*R + 0.587*G + 0.114*B
Parameters
Name | Type | Default | Description |
---|---|---|---|
img | np.ndarray | - | Input RGB image as a numpy array. |
Returns
- np.ndarray: Grayscale image as a 2D numpy array.
unsharp_maskfunction
unsharp_mask(
image: np.ndarray,
ksize: int,
sigma: float,
alpha: float,
threshold: int
)
Apply an unsharp mask to an image. This function applies an unsharp mask to an image using the Gaussian blur function. The unsharp mask is applied by subtracting the blurred image from the original image and then adding the result to the original image.
Parameters
Name | Type | Default | Description |
---|---|---|---|
image | np.ndarray | - | Input image as a numpy array. |
ksize | int | - | The kernel size to use for the Gaussian blur. |
sigma | float | - | The sigma value to use for the Gaussian blur. |
alpha | float | - | The alpha value to use for the unsharp mask. |
threshold | int | - | The threshold value to use for the unsharp mask. |
Returns
- np.ndarray: The unsharp mask applied to the image.
volume_channel_shufflefunction
volume_channel_shuffle(
volume: np.ndarray,
channels_shuffled: Sequence[int]
)
Shuffle channels of a single volume (D, H, W, C) or (D, H, W).
Parameters
Name | Type | Default | Description |
---|---|---|---|
volume | np.ndarray | - | Input volume. |
channels_shuffled | Sequence[int] | - | New channel order. |
Returns
- np.ndarray: Volume with channels shuffled.
volumes_channel_shufflefunction
volumes_channel_shuffle(
volumes: np.ndarray,
channels_shuffled: Sequence[int]
)
Shuffle channels of a batch of volumes (B, D, H, W, C) or (B, D, H, W).
Parameters
Name | Type | Default | Description |
---|---|---|---|
volumes | np.ndarray | - | Input batch of volumes. |
channels_shuffled | Sequence[int] | - | New channel order. |
Returns
- np.ndarray: Batch of volumes with channels shuffled.