← Back to all transforms
TemplateTransform
Description
Apply blending of input image with specified templates. This transform overlays one or more template images onto the input image using alpha blending. It allows for creating complex composite images or simulating various visual effects. Args: templates (numpy array | list[np.ndarray]): Images to use as templates for the transform. If a single numpy array is provided, it will be used as the only template. If a list of numpy arrays is provided, one will be randomly chosen for each application. img_weight (tuple[float, float] | float): Weight of the original image in the blend. If a single float, that value will always be used. If a tuple (min, max), the weight will be randomly sampled from the range [min, max) for each application. To use a fixed weight, use (weight, weight). Default: (0.5, 0.5). template_transform (A.Compose | None): A composition of Albumentations transforms to apply to the template before blending. This should be an instance of A.Compose containing one or more Albumentations transforms. Default: None. name (str | None): Name of the transform instance. Used for serialization purposes. Default: None. p (float): Probability of applying the transform. Default: 0.5. Targets: image Image types: uint8, float32 Number of channels: Any Note: - The template(s) must have the same number of channels as the input image or be single-channel. - If a single-channel template is used with a multi-channel image, the template will be replicated across all channels. - The template(s) will be resized to match the input image size if they differ. - To make this transform serializable, provide a name when initializing it. Mathematical Formulation: Given: - I: Input image - T: Template image - w_i: Weight of input image (sampled from img_weight) The blended image B is computed as: B = w_i * I + (1 - w_i) * T Examples: >>> import numpy as np >>> import albumentations as A >>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) >>> template = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) # Apply template transform with a single template >>> transform = A.TemplateTransform(templates=template, name="my_template_transform", p=1.0) >>> blended_image = transform(image=image)['image'] # Apply template transform with multiple templates and custom weights >>> templates = [np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8) for _ in range(3)] >>> transform = A.TemplateTransform( ... templates=templates, ... img_weight=(0.3, 0.7), ... name="multi_template_transform", ... p=1.0 ... ) >>> blended_image = transform(image=image)['image'] # Apply template transform with additional transforms on the template >>> template_transform = A.Compose([A.RandomBrightnessContrast(p=1)]) >>> transform = A.TemplateTransform( ... templates=template, ... img_weight=0.6, ... template_transform=template_transform, ... name="transformed_template", ... p=1.0 ... ) >>> blended_image = transform(image=image)['image'] References: - Alpha compositing: https://en.wikipedia.org/wiki/Alpha_compositing - Image blending: https://en.wikipedia.org/wiki/Image_blending
Parameters
- templates: ndarray | Sequence[ndarray] (default: null)
- img_weight: int | tuple[int, int] | float | tuple[float, float] (default: (0.5, 0.5))
- template_transform: Compose | BasicTransform | None (default: null)
- name: str | None (default: null)
- p: float (default: 0.5)
Targets
- Image
Try it out
ⓘ
Original Image:
Reference Image:
Result:
Transform result will appear here