Skip to content

Commit

Permalink
move format functions from segmentation to stack
Browse files Browse the repository at this point in the history
  • Loading branch information
Henley13 committed Jan 21, 2022
1 parent e9f6fcb commit dc1b0e0
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 143 deletions.
9 changes: 2 additions & 7 deletions bigfish/segmentation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@
from .utils import compute_mean_convexity_ratio
from .utils import compute_surface_ratio
from .utils import count_instances
from .utils import resize_image
from .utils import get_marge_padding
from .utils import compute_image_standardization


_cell = [
"unet_distance_edge_double",
Expand Down Expand Up @@ -72,9 +70,6 @@
"compute_mean_diameter",
"compute_mean_convexity_ratio",
"compute_surface_ratio",
"count_instances",
"resize_image",
"get_marge_padding",
"compute_image_standardization"]
"count_instances"]

__all__ = _cell + _nuc + _postprocess + _utils
21 changes: 9 additions & 12 deletions bigfish/segmentation/cell_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
import bigfish.stack as stack

from .utils import thresholding
from .utils import resize_image
from .utils import get_marge_padding
from .utils import compute_image_standardization
from .postprocess import label_instances
from .postprocess import clean_segmentation

Expand Down Expand Up @@ -102,21 +99,21 @@ def apply_unet_distance_double(model, nuc, cell, nuc_label, target_size=None,
new_height = int(np.round(height * ratio))
new_width = int(np.round(width * ratio))
new_shape = (new_height, new_width)
nuc_to_process = resize_image(nuc, new_shape, "bilinear")
cell_to_process = resize_image(cell, new_shape, "bilinear")
nuc_to_process = stack.resize_image(nuc, new_shape, "bilinear")
cell_to_process = stack.resize_image(cell, new_shape, "bilinear")
nuc_label_to_process = nuc_label.copy()

# get padding marge to make it multiple of 16
marge_padding = get_marge_padding(new_height, new_width, x=16)
marge_padding = stack.get_marge_padding(new_height, new_width, x=16)
top, bottom = marge_padding[0]
left, right = marge_padding[1]
nuc_to_process = pad(nuc_to_process, marge_padding, mode='symmetric')
cell_to_process = pad(cell_to_process, marge_padding, mode='symmetric')

# standardize and cast cell image
nuc_to_process = compute_image_standardization(nuc_to_process)
nuc_to_process = stack.compute_image_standardization(nuc_to_process)
nuc_to_process = nuc_to_process.astype(np.float32)
cell_to_process = compute_image_standardization(cell_to_process)
cell_to_process = stack.compute_image_standardization(cell_to_process)
cell_to_process = cell_to_process.astype(np.float32)

# augment images
Expand Down Expand Up @@ -155,14 +152,14 @@ def apply_unet_distance_double(model, nuc, cell, nuc_label, target_size=None,
# from the image augmentation
if target_size is not None:
if i in [0, 1, 2, 6]:
prediction_cell = resize_image(
prediction_cell = stack.resize_image(
prediction_cell, (height, width), "bilinear")
prediction_distance = resize_image(
prediction_distance = stack.resize_image(
prediction_distance, (height, width), "bilinear")
else:
prediction_cell = resize_image(
prediction_cell = stack.resize_image(
prediction_cell, (width, height), "bilinear")
prediction_distance = resize_image(
prediction_distance = stack.resize_image(
prediction_distance, (width, height), "bilinear")

# store predictions
Expand Down
14 changes: 6 additions & 8 deletions bigfish/segmentation/nuc_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import numpy as np

import bigfish.stack as stack
from .utils import resize_image
from .utils import get_marge_padding
from .utils import compute_image_standardization

from .postprocess import label_instances
from .postprocess import clean_segmentation

Expand Down Expand Up @@ -90,16 +88,16 @@ def apply_unet_3_classes(model, image, target_size=None,
new_height = int(np.round(height * ratio))
new_width = int(np.round(width * ratio))
new_shape = (new_height, new_width)
image_to_process = resize_image(image, new_shape, "bilinear")
image_to_process = stack.resize_image(image, new_shape, "bilinear")

# get padding marge to make it multiple of 16
marge_padding = get_marge_padding(new_height, new_width, x=16)
marge_padding = stack.get_marge_padding(new_height, new_width, x=16)
top, bottom = marge_padding[0]
left, right = marge_padding[1]
image_to_process = pad(image_to_process, marge_padding, mode='symmetric')

# standardize and cast image
image_to_process = compute_image_standardization(image_to_process)
image_to_process = stack.compute_image_standardization(image_to_process)
image_to_process = image_to_process.astype(np.float32)

# augment images
Expand Down Expand Up @@ -131,10 +129,10 @@ def apply_unet_3_classes(model, image, target_size=None,
# from the image augmentation
if target_size is not None:
if i in [0, 1, 2, 6]:
prediction = resize_image(
prediction = stack.resize_image(
prediction, (height, width), "bilinear")
else:
prediction = resize_image(
prediction = stack.resize_image(
prediction, (width, height), "bilinear")

# store predictions
Expand Down
115 changes: 0 additions & 115 deletions bigfish/segmentation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
import numpy as np

from skimage.measure import regionprops
from skimage.transform import resize


# TODO make functions compatible with different type of integers
# TODO move format functions in bigfish.stack.preprocess

# ### Thresholding method ###

Expand Down Expand Up @@ -180,116 +178,3 @@ def count_instances(image_label):
nb_instances = len(indices)

return nb_instances


# ### Format and crop images ###

def resize_image(image, output_shape, method="bilinear"):
"""Resize an image with bilinear interpolation or nearest neighbor method.
Parameters
----------
image : np.ndarray
Image to resize.
output_shape : Tuple[int]
Shape of the resized image.
method : str
Interpolation method to use.
Returns
-------
image_resized : np.ndarray
Resized image.
"""
# check parameters
stack.check_parameter(output_shape=tuple, method=str)
stack.check_array(image,
ndim=[2, 3],
dtype=[np.uint8, np.uint16, np.float32])

# resize image
if method == "bilinear":
image_resized = resize(image, output_shape,
mode="reflect", preserve_range=True,
order=1, anti_aliasing=True)
elif method == "nearest":
image_resized = resize(image, output_shape,
mode="reflect", preserve_range=True,
order=0, anti_aliasing=False)
else:
raise ValueError("Method {0} is not available. Choose between "
"'bilinear' or 'nearest' instead.".format(method))

# cast output dtype
image_resized = image_resized.astype(image.dtype)

return image_resized


def get_marge_padding(height, width, x):
"""Pad image to make its shape a multiple of `x`.
Parameters
----------
height : int
Original height of the image.
width : int
Original width of the image.
x : int
Padded image have a `height` and `width` multiple of `x`.
Returns
-------
marge_padding : List[List]
List of lists with the format
[[`marge_height_t`, `marge_height_b`], [`marge_width_l`,
`marge_width_r`]].
"""
# check parameters
stack.check_parameter(height=int, width=int, x=int)

# pad height and width to make it multiple of x
marge_sup_height = x - (height % x)
marge_sup_height_l = int(marge_sup_height / 2)
marge_sup_height_r = marge_sup_height - marge_sup_height_l
marge_sup_width = x - (width % x)
marge_sup_width_l = int(marge_sup_width / 2)
marge_sup_width_r = marge_sup_width - marge_sup_width_l
marge_padding = [[marge_sup_height_l, marge_sup_height_r],
[marge_sup_width_l, marge_sup_width_r]]

return marge_padding


def compute_image_standardization(image):
"""Normalize image by computing its z score.
Parameters
----------
image : np.ndarray
Image to normalize with shape (y, x).
Returns
-------
normalized_image : np.ndarray
Normalized image with shape (y, x).
"""
# check parameters
stack.check_array(image, ndim=2, dtype=[np.uint8, np.uint16, np.float32])

# check image is in 2D
if len(image.shape) != 2:
raise ValueError("'image' should be a 2-d array. Not {0}-d array"
.format(len(image.shape)))

# compute mean and standard deviation
m = np.mean(image)
adjusted_stddev = max(np.std(image), 1.0 / np.sqrt(image.size))

# normalize image
normalized_image = (image - m) / adjusted_stddev

return normalized_image
8 changes: 7 additions & 1 deletion bigfish/stack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
from .preprocess import cast_img_uint16
from .preprocess import cast_img_float32
from .preprocess import cast_img_float64
from .preprocess import resize_image
from .preprocess import get_marge_padding
from .preprocess import compute_image_standardization

from .filter import mean_filter
from .filter import median_filter
Expand Down Expand Up @@ -96,7 +99,10 @@
"cast_img_uint8",
"cast_img_uint16",
"cast_img_float32",
"cast_img_float64"]
"cast_img_float64",
"resize_image",
"get_marge_padding",
"compute_image_standardization"]

_filter = [
"log_filter",
Expand Down
Loading

0 comments on commit dc1b0e0

Please sign in to comment.