Skip to content

Commit

Permalink
tests for image augmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Henley13 committed May 5, 2020
1 parent 94a02a2 commit 49bab67
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 82 deletions.
4 changes: 2 additions & 2 deletions bigfish/stack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
format_experimental_data, get_label_encoder,
remove_transcription_site_bis, filter_data,
balance_data, get_gene_encoder)
from .augmentation import augment
from .augmentation import augment_2d


_utils = ["check_array", "check_df", "check_recipe", "check_datamap",
Expand Down Expand Up @@ -68,7 +68,7 @@
"complete_coord_boundaries", "from_coord_to_frame",
"from_coord_to_surface"]

_augmentation = ["augment"]
_augmentation = ["augment_2d"]

_preparation = ["split_from_background", "build_image", "get_coordinates",
"get_distance_layers", "get_surface_layers", "build_batch",
Expand Down
166 changes: 87 additions & 79 deletions bigfish/stack/augmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,184 +5,192 @@
"""

import numpy as np
from .preprocess import check_parameter, check_array


def identity(image):
def augment_2d(image):
"""Augment an image applying a random operation.
Parameters
----------
image : np.ndarray
Image to augment with shape (y, x, channels).
Returns
-------
image_augmented : np.ndarray
Image augmented with shape (y, x, channels).
"""
# check input image
check_parameter(image=np.ndarray)
check_array(image, ndim=[2, 3])

# randomly choose an operator
operations = [_identity,
_flip_h, _flip_v,
_transpose, _transpose_inverse,
_rotation_90, _rotation_180, _rotation_270]
random_operation = np.random.choice(operations)

# augment the image
image_augmented = random_operation(image)

return image_augmented


def _identity(image):
"""Do not apply any operation to the image.
Parameters
----------
image : np.ndarray, np.float32
image : np.ndarray
Image with shape (x, y, channels).
Returns
-------
image : np.ndarray, np.float32
image : np.ndarray
Image with shape (x, y, channels).
"""
return image


def flip_h(image):
def _flip_h(image):
"""Flip an image horizontally.
Parameters
----------
image : np.ndarray, np.float32
Image to flip with shape (x, y, channels).
image : np.ndarray
Image to flip with shape (y, x, channels).
Returns
-------
image_flipped : np.ndarray, np.float32
Image flipped with shape (x, y, channels).
image_flipped : np.ndarray
Image flipped with shape (y, x, channels).
"""
image_flipped = np.flip(image, axis=0)

return image_flipped


def flip_v(image):
def _flip_v(image):
"""Flip an image vertically.
Parameters
----------
image : np.ndarray, np.float32
Image to flip with shape (x, y, channels).
image : np.ndarray
Image to flip with shape (y, x, channels).
Returns
-------
image_flipped : np.ndarray, np.float32
Image flipped with shape (x, y, channels).
image_flipped : np.ndarray
Image flipped with shape (y, x, channels).
"""
image_flipped = np.flip(image, axis=1)

return image_flipped


def transpose(image):
"""Transpose an image.
def _transpose(image):
"""Transpose an image (flip it along the top left - bottom right diagonal).
Parameters
----------
image : np.ndarray, np.float32
Image to transpose with shape (x, y, channels).
image : np.ndarray
Image to transpose with shape (y, x, channels).
Returns
-------
image_transposed : np.ndarray, np.float32
Image transposed with shape (x, y, channels).
image_transposed : np.ndarray
Image transposed with shape (y, x, channels).
"""
image_transposed = np.transpose(image, axes=(1, 0, 2))
if image.ndim == 3:
image_transposed = np.transpose(image, axes=(1, 0, 2))
else:
image_transposed = np.transpose(image)

return image_transposed


def rotation_90(image):
"""Rotate an image with 90 degrees.
def _rotation_90(image):
"""Rotate an image with 90 degrees (clockwise).
Parameters
----------
image : np.ndarray, np.float32
Image to rotate with shape (x, y, channels).
image : np.ndarray
Image to rotate with shape (y, x, channels).
Returns
-------
image_rotated : np.ndarray, np.float32
Image rotated with shape (x, y, channels).
image_rotated : np.ndarray
Image rotated with shape (y, x, channels).
"""
image_rotated = flip_h(image)
image_rotated = transpose(image_rotated)
image_rotated = _flip_h(image)
image_rotated = _transpose(image_rotated)

return image_rotated


def rotation_180(image):
"""Rotate an image with 90 degrees.
def _rotation_180(image):
"""Rotate an image with 180 degrees.
Parameters
----------
image : np.ndarray, np.float32
Image to rotate with shape (x, y, channels).
image : np.ndarray
Image to rotate with shape (y, x, channels).
Returns
-------
image_rotated : np.ndarray, np.float32
Image rotated with shape (x, y, channels).
image_rotated : np.ndarray
Image rotated with shape (y, x, channels).
"""
image_rotated = flip_v(image)
image_rotated = flip_h(image_rotated)
image_rotated = _flip_v(image)
image_rotated = _flip_h(image_rotated)

return image_rotated


def rotation_270(image):
"""Rotate an image with 90 degrees.
def _rotation_270(image):
"""Rotate an image with 270 degrees (clockwise).
Parameters
----------
image : np.ndarray, np.float32
Image to rotate with shape (x, y, channels).
image : np.ndarray
Image to rotate with shape (y, x, channels).
Returns
-------
image_rotated : np.ndarray, np.float32
Image rotated with shape (x, y, channels).
image_rotated : np.ndarray
Image rotated with shape (y, x, channels).
"""
image_rotated = flip_v(image)
image_rotated = transpose(image_rotated)
image_rotated = _flip_v(image)
image_rotated = _transpose(image_rotated)

return image_rotated


def transpose_inverse(image):
"""Transpose an image from the other diagonal.
def _transpose_inverse(image):
"""Flip an image along the bottom left - top right diagonal.
Parameters
----------
image : np.ndarray, np.float32
Image to transpose with shape (x, y, channels).
image : np.ndarray
Image to transpose with shape (y, x, channels).
Returns
-------
image_transposed : np.ndarray, np.float32
Image transposed with shape (x, y, channels).
image_transposed : np.ndarray
Image transposed with shape (y, x, channels).
"""
image_transposed = rotation_270(image)
image_transposed = transpose(image_transposed)
image_transposed = _transpose(image)
image_transposed = _rotation_180(image_transposed)

return image_transposed


def augment(image):
"""Augment an image applying a random operation.
Parameters
----------
image : np.ndarray, np.float32
Image to augment with shape (x, y, channels).
Returns
-------
image_augmented : np.ndarray, np.float32
Image augmented with shape (x, y, channels).
"""
# randomly choose an operator
operations = [identity,
flip_h, flip_v,
transpose, transpose_inverse,
rotation_90, rotation_180, rotation_270]
random_operation = np.random.choice(operations)

# augment the image
image_augmented = random_operation(image)

return image_augmented
2 changes: 1 addition & 1 deletion bigfish/stack/preparation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from scipy import ndimage as ndi

from .utils import get_margin_value
from .augmentation import augment
from .augmentation import augment_2d
from .preprocess import cast_img_float32
from .filter import mean_filter

Expand Down
Loading

0 comments on commit 49bab67

Please sign in to comment.