-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #422 from kwcckw/dev
Updated benchmarks and added Stains.
- Loading branch information
Showing
7 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import random | ||
|
||
import cv2 | ||
import numpy as np | ||
|
||
from augraphy.base.augmentation import Augmentation | ||
from augraphy.utilities.overlaybuilder import OverlayBuilder | ||
from augraphy.utilities.texturegenerator import TextureGenerator | ||
|
||
|
||
class Stains(Augmentation): | ||
"""Creates a stains texture and the stains texture is blended into the input image by using OverlayBuilder. | ||
:param stains_type: Types of stains. Use "random" for random stains effect. | ||
Select from "rough_stains", "fine_stains", "severe_stains", "light_stains". | ||
:type stains_type: tuple, optional | ||
:param stains_blend_method: The blending method to blend stains texture into the input image. | ||
:type stains_blend_method: string, optional | ||
:param stains_blend_alpha: The blending alpha value for blending method with the usage of alpha. | ||
:type stains_blend_alpha: float, optional | ||
:param p: The probability this Augmentation will be applied. | ||
:type p: float, optional | ||
""" | ||
|
||
def __init__( | ||
self, | ||
stains_type="random", | ||
stains_blend_method="darken", | ||
stains_blend_alpha=0.5, | ||
p=1, | ||
): | ||
"""Constructor method""" | ||
super().__init__(p=p) | ||
self.stains_type = stains_type | ||
self.stains_blend_method = stains_blend_method | ||
self.stains_blend_alpha = stains_blend_alpha | ||
|
||
# Constructs a string representation of this Augmentation. | ||
def __repr__(self): | ||
return f"stains(stains_type={self.stains_type}, stains_blend_method={self.stains_blend_method}, stains_blend_alpha={self.stains_blend_alpha}, p={self.p})" | ||
|
||
# Applies the Augmentation to input data. | ||
def __call__(self, image, layer=None, mask=None, keypoints=None, bounding_boxes=None, force=False): | ||
if force or self.should_run(): | ||
image = image.copy() | ||
|
||
# convert and make sure image is color image | ||
has_alpha = 0 | ||
if len(image.shape) > 2: | ||
is_gray = 0 | ||
if image.shape[2] == 4: | ||
has_alpha = 1 | ||
image, image_alpha = image[:, :, :3], image[:, :, 3] | ||
else: | ||
is_gray = 1 | ||
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR) | ||
|
||
ysize, xsize = image.shape[:2] | ||
|
||
stains = ["rough_stains", "fine_stains", "severe_stains", "light_stains"] | ||
if self.stains_type not in stains: | ||
stains_type = random.choice(stains) | ||
else: | ||
stains_type = self.stains_type | ||
|
||
# generator to create stains | ||
stains_generator = TextureGenerator() | ||
|
||
# generate stains | ||
image_stains = stains_generator( | ||
texture_type=stains_type, | ||
texture_width=xsize, | ||
texture_height=ysize, | ||
quilt_texture=0, | ||
) | ||
|
||
# blend points image into input again | ||
ob = OverlayBuilder( | ||
self.stains_blend_method, | ||
image_stains, | ||
image, | ||
1, | ||
(1, 1), | ||
"center", | ||
0, | ||
self.stains_blend_alpha, | ||
) | ||
image_output = ob.build_overlay() | ||
|
||
# return image follows the input image color channel | ||
if is_gray: | ||
image_output = cv2.cvtColor(image_output, cv2.COLOR_BGR2GRAY) | ||
if has_alpha: | ||
image_output = np.dstack((image_output, image_alpha)) | ||
|
||
# check for additional output of mask, keypoints and bounding boxes | ||
outputs_extra = [] | ||
if mask is not None or keypoints is not None or bounding_boxes is not None: | ||
outputs_extra = [mask, keypoints, bounding_boxes] | ||
|
||
# returns additional mask, keypoints and bounding boxes if there is additional input | ||
if outputs_extra: | ||
# returns in the format of [image, mask, keypoints, bounding_boxes] | ||
return [image_output] + outputs_extra | ||
else: | ||
return image_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
****** | ||
Stains | ||
****** | ||
|
||
.. autoclass:: augraphy.augmentations.stains.Stains | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
-------- | ||
Overview | ||
-------- | ||
The Stains augmentation creates a stains texture and the stains texture is blended into the input image by using OverlayBuilder. | ||
|
||
|
||
Initially, a clean image with single line of text is created. | ||
|
||
Code example: | ||
|
||
:: | ||
|
||
# import libraries | ||
import cv2 | ||
import numpy as np | ||
from augraphy import * | ||
|
||
|
||
# create a clean image with single line of text | ||
image = np.full((500, 1500,3), 250, dtype="uint8") | ||
cv2.putText( | ||
image, | ||
"Lorem ipsum dolor sit amet, consectetur adipiscing elit", | ||
(80, 250), | ||
cv2.FONT_HERSHEY_SIMPLEX, | ||
1.5, | ||
0, | ||
3, | ||
) | ||
|
||
cv2.imshow("Input image", image) | ||
|
||
Clean image: | ||
|
||
.. figure:: augmentations/input.png | ||
|
||
--------- | ||
Example 1 | ||
--------- | ||
In this example, a Stains augmentation instance is initialized and the stains type is set to severe stains (stains_type="severe_stains"). The method to blend stains texture into image is set to darken (stains_blend_method="darken"). | ||
|
||
|
||
Code example: | ||
|
||
:: | ||
|
||
stains = Stains(stains_type="severe_stains", | ||
stains_blend_method="darken", | ||
) | ||
|
||
img_stains = stains(image) | ||
|
||
cv2.imshow("stains", img_stains) | ||
|
||
Augmented image: | ||
|
||
.. figure:: augmentations/stains/stains.png |