ImageTransformer¶
ImageTransformer is a special type of action for image transformation.
This type allows you to consistently describe the necessary changes for the image, so that when performing a step, you can get an already modified image at the output.
The conversion can be single or multiple.
ImageTransformer does not implement transforms itself — it combines torchvision.transforms and OpenCV. Medical-specific helpers include circle_crop, ellipse_crop, and min_area_rect_crop for fundus-camera images.
from hyppopipe.data.image import Image
from hyppopipe.pipeline.image.transform import ImageTransformer
from hyppopipe.pipeline import Pipeline, Step
from torchvision.transforms import v2 as transforms
image_data_processing = Pipeline(
{
"GetResizedImage": Step(
ImageTransformer().resize(224),
inputs=["__input__"],
),
"GetSharpenImage": Step(
ImageTransformer().sharpen(2.0),
inputs=["GetResizedImage"],
),
"GetBluredImage": Step(
ImageTransformer().gaussian_blur(5, sigma=(0.1, 2.0)),
inputs=["GetResizedImage"],
),
# Or we can use fluent transformations:
"FluentTransformations": Step(
ImageTransformer().resize(224).gaussian_blur(5, sigma=(0.1, 2.0)).rotate(90),
inputs=["__input__"],
),
# Or composed transformations:
"ComposedTransformations": Step(
ImageTransformer.from_compose(
transforms.Compose([
transforms.Resize(224),
transforms.GaussianBlur(5, sigma=(0.1, 2.0)),
transforms.RandomRotation((90, 90)),
])
),
inputs=["__input__"],
)
},
shift_result=False,
)
original_image = Image.from_path("images/image74prime.tif")
pipeline_result = image_data_processing.predict(original_image)
Documentation¶
Composable torchvision and OpenCV transforms for :class:~hyppopipe.data.image.Image.
CircleCropConfig
dataclass
¶
Bases: ContourSelectConfig
:class:ContourSelectConfig plus circle-specific filters for :meth:ImageTransformer.circle_crop.
Attributes:
| Name | Type | Description |
|---|---|---|
min_radius_ratio |
float
|
Minimum enclosing-circle radius vs |
max_radius_ratio |
float
|
Maximum radius vs |
min_circularity |
float
|
|
ContourSelectConfig
dataclass
¶
Contour detection and filtering for OpenCV contour-based steps.
Used by :meth:ImageTransformer.ellipse_crop, :meth:ImageTransformer.min_area_rect_crop,
:meth:ImageTransformer.convex_hull_mask, and :meth:ImageTransformer.contour_mask.
Attributes:
| Name | Type | Description |
|---|---|---|
threshold |
int
|
Grayscale binarization level (higher → fewer faint regions). |
min_contour_area_ratio |
float
|
Ignore contours smaller than this fraction of the image. |
max_contour_area_ratio |
float
|
Ignore contours larger than this (often the image frame). |
reject_frame_contour |
bool
|
Skip contours whose bounding box touches all four edges. |
frame_margin_px |
int
|
Edge tolerance when detecting a frame-filling contour. |
FloodFillConfig
dataclass
¶
Tuning for :meth:ImageTransformer.flood_fill.
ImageTransformer
¶
Fluent builder that applies transforms in the order they were added.
Torchvision steps run on CHW tensors; OpenCV steps receive BGR HWC uint8
arrays (standard OpenCV layout) and are converted automatically.
OpenCV steps skip themselves on invalid input or failed callables (with a warning) and pass the tensor through to the next step.
__call__(image)
¶
Apply all configured transforms in registration order.
__init__()
¶
Start with an empty transform list.
add(step)
¶
Append a custom CHW tensor → CHW tensor step.
center_crop(size)
¶
Append torchvision.transforms.v2.CenterCrop.
circle_crop(config=None, **kwargs)
¶
Append OpenCV crop around a filtered contour's enclosing circle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
CircleCropConfig | None
|
Full tuning object; overrides keyword arguments when set. |
None
|
**kwargs
|
Any
|
Fields for :class: |
{}
|
Example::
ImageTransformer().circle_crop(
threshold=40,
max_contour_area_ratio=0.75,
min_circularity=0.5,
)
clear()
¶
Remove all transforms from the recipe.
contour_mask(config=None, *, background=None, **kwargs)
¶
Zero (or fill) pixels outside the selected contour polygon.
convex_hull_mask(config=None, *, background=None, **kwargs)
¶
Zero (or fill) pixels outside the convex hull of the selected contour.
ellipse_crop(config=None, **kwargs)
¶
Crop axis-aligned square around :func:cv2.fitEllipse of the selected contour.
flood_fill(config=None, **kwargs)
¶
Region growing from seed_xy (image center when omitted).
from_compose(compose)
classmethod
¶
Create a transformer from a torchvision.transforms.v2.Compose.
Each child transform becomes its own sequential step (order preserved).
from_transforms(transformations)
classmethod
¶
Create a transformer from torchvision v2 transforms (one step each).
gaussian_blur(kernel_size, sigma)
¶
Append torchvision.transforms.v2.GaussianBlur.
min_area_rect_crop(config=None, **kwargs)
¶
Perspective-crop the oriented minimum-area rectangle of the selected contour.
morphology(config=None, **kwargs)
¶
Apply :func:cv2.morphologyEx on grayscale (optionally binarized first).
normalize(mean, std)
¶
Append torchvision.transforms.v2.Normalize.
opencv(fn, *, step_name='opencv')
¶
Append an OpenCV callable (BGR HWC uint8 in/out).
remove_small_components(config=None, *, background=None, **kwargs)
¶
Drop connected components smaller than min_area after thresholding.
resize(size, *args, **kwargs)
¶
Append torchvision.transforms.v2.Resize.
rotate(degrees)
¶
Append torchvision.transforms.v2.RandomRotation.
sharpen(factor=2.0)
¶
Append torchvision.transforms.v2.RandomAdjustSharpness with p=1.
torchvision(transform)
¶
Append a torchvision.transforms.v2 transform (runs on CHW tensor).
MaskBackgroundConfig
dataclass
¶
MorphologyConfig
dataclass
¶
Tuning for :meth:ImageTransformer.morphology.
RemoveSmallComponentsConfig
dataclass
¶
Tuning for :meth:ImageTransformer.remove_small_components.