Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clahe operation #216

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ _static/
_templates/
__pycache__/
.ipynb_checkpoints/
.vscode/
50 changes: 50 additions & 0 deletions Augmentor/Operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import os
import random
import warnings
# Added CLAHE algorithm from CV2
import cv2

# Python 2-3 compatibility - not currently needed.
# try:
Expand Down Expand Up @@ -2127,3 +2129,51 @@ def do(image1, image2, y1, y2):
augmented_images.append(do(image, image, y1, y2))

return augmented_images


class Clahe(Operation):
"""
This class : Clahe is used to perform Contrast Limited Adaptive Histogram Equalization
as it is implemented in the OpenCV2 library 4.1.1
"""

def __init__(self, probability, clip_limit_min=0.0, clip_limit_max=40.0, tile_grid=(8, 8)):
"""
Initializes parameters
:param probability: Controls the probability that the operation is
performed when it is inovked in the pipeline
:param clip_limit_min: Sets the contrast limit min value
:param clip_limit_max: Sets the contrast limit max value
:param tile_grid: it controls the size of the blocks that are to be histogram equalized. Default
value as described in docs.opencv.org is 8x8
"""
Operation.__init__(self, probability)
self.clip_limit_min = clip_limit_min
self.clip_limit_max = clip_limit_max
self.tile_grid = tile_grid

def __str__(self):
return "Clahe contrast_limit_min: {}, contrast_limit_max {}, tile: {}".format(self.clip_limit_min,
self.clip_limit_max,
self.tile_grid)

def perform_operation(self, images):
"""
Perform OpenCV Contrast Limited Adaptive Histogram Equalization
:param images: The image(s) to equalize
:return: The transformed image(s) as a list of object(s) of type PIL.Image.
"""
def do(image):
if not image.mode == 'L':
img_grayscale = ImageOps.grayscale(image)
else:
img_grayscale = image

clip_limit_threshold = np.random.uniform(self.clip_limit_min, self.clip_limit_max)
img_cv2 = np.asarray(img_grayscale)
clahe_cv2 = cv2.createCLAHE(clipLimit=clip_limit_threshold, tileGridSize=self.tile_grid)
img_clahe = clahe_cv2.apply(img_cv2)
return img_clahe

augmented_images = [Image.fromarray(do(img)) for img in images]
return augmented_images
26 changes: 26 additions & 0 deletions Augmentor/Pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,32 @@ def histogram_equalisation(self, probability=1.0):
else:
self.add_operation(HistogramEqualisation(probability=probability))

def clahe(self, probability, clip_limit_min, clip_limit_max, tile_grid):
"""
Apply Contrast Limited Adaptive Histogram Equalization
In order to avoid noise amplification a contrast limited is required. If clip_limit_min and clip_limit_max
have different values, different CLAHE will be obtained randomly

If the user desires to use CLAHE as a preprocess by using the same clip_limit value, then set:
clip_limit_min = clip_limit_max

:param probability: The probability that the function will execute
when the image is passed through the pipeline.
:param clip_limit_min: min value of clip_limit.
:param clip_limit_max: max value of clip_limit
:param tile_grid: the number of pixels of the block to be equalized

More information consult at docs.opencv.org

"""
if not 0 < probability <= 1:
raise ValueError(Pipeline._probability_error_text)
else:
self.add_operation(Clahe(probability=probability,
clip_limit_min=clip_limit_min,
clip_limit_max=clip_limit_max,
tile_grid=tile_grid))

def scale(self, probability, scale_factor):
"""
Scale (enlarge) an image, while maintaining its aspect ratio. This
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Pillow
future
futures
tqdm
numpy
numpy
opencv-python