Equalize
Targets:
image
volume
Image Types:uint8, float32
Equalize the image histogram.
This transform applies histogram equalization to the input image. Histogram equalization is a method in image processing of contrast adjustment using the image's histogram.
Arguments
modecv | pil
cv
Use OpenCV or Pillow equalization method. Default: 'cv'
by_channelsbool
true
If True, use equalization by channels separately,
else convert image to YCbCr representation and use equalization by Y channel.
Default: True
maskndarray | Callable[..., Any] | None
If given, only the pixels selected by the mask are included in the analysis. Can be:
- A 1-channel or 3-channel numpy array of the same size as the input image.
- A callable (function) that generates a mask. The function should accept 'image' as its first argument, and can accept additional arguments specified in mask_params. Default: None
mask_paramsSequence
[]
Additional parameters to pass to the mask function. These parameters will be taken from the data dict passed to call. Default: ()
pfloat
0.5
Probability of applying the transform. Default: 0.5.
Examples
>>> import numpy as np
>>> import albumentations as A
>>> image = np.random.randint(0, 256, (100, 100, 3), dtype=np.uint8)
>>>
>>> # Using a static mask
>>> mask = np.random.randint(0, 2, (100, 100), dtype=np.uint8)
>>> transform = A.Equalize(mask=mask, p=1.0)
>>> result = transform(image=image)
>>>
>>> # Using a dynamic mask function
>>> def mask_func(image, bboxes):
... mask = np.ones_like(image[:, :, 0], dtype=np.uint8)
... for bbox in bboxes:
... x1, y1, x2, y2 = map(int, bbox)
... mask[y1:y2, x1:x2] = 0 # Exclude areas inside bounding boxes
... return mask
>>>
>>> transform = A.Equalize(mask=mask_func, mask_params=['bboxes'], p=1.0)
>>> bboxes = [(10, 10, 50, 50), (60, 60, 90, 90)] # Example bounding boxes
>>> result = transform(image=image, bboxes=bboxes)Notes
- When mode='cv', OpenCV's equalizeHist() function is used.
- When mode='pil', Pillow's equalize() function is used.
- The 'by_channels' parameter determines whether equalization is applied to each color channel independently (True) or to the luminance channel only (False).
- If a mask is provided as a numpy array, it should have the same height and width as the input image.
- If a mask is provided as a function, it allows for dynamic mask generation based on the input image and additional parameters. This is useful for scenarios where the mask depends on the image content or external data (e.g., bounding boxes, segmentation masks).
References
- OpenCV equalizeHisthttps://docs.opencv.org/3.4/d6/dc7/group__imgproc__hist.html#ga7e54091f0c937d49bf84152a16f76d6e
- Pillow ImageOps.equalizehttps://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.equalize
- Histogram Equalizationhttps://en.wikipedia.org/wiki/Histogram_equalization