# This file is part of Sympathy for Data.
# Copyright (c) 2017, Combine Control Systems AB
#
# SYMPATHY FOR DATA COMMERCIAL LICENSE
# You should have received a link to the License with Sympathy for Data.
import numpy as np
import skimage
from sympathy.api import node
from sympathy.api.nodeconfig import Ports
from skimage import transform
from sylib.imageprocessing.image import ImagePort
from sylib.imageprocessing.algorithm_selector import ImageFiltering_abstract
from sylib.imageprocessing.generic_filtering import GenericImageFiltering
from packaging import version
skimage_version = version.Version(skimage.__version__)
# Test for deprecation of criterion arguments
check_19 = skimage_version >= version.Version('0.19.0')
def resize_args(im, params):
if not check_19:
return transform.rescale(
im, (params['scale y'].value, params['scale x'].value),
mode='constant',
multichannel=True,
anti_aliasing=True,
order=params['interpolation degree'].value)
else:
return transform.rescale(
im, (params['scale y'].value, params['scale x'].value),
mode='constant',
channel_axis=2,
anti_aliasing=True,
order=params['interpolation degree'].value)
def alg_resize(im, params):
req_h = params['height'].value
req_w = params['width'].value
h, w = req_h, req_w
if params['aspect'].value:
aspect = im.shape[1] / float(im.shape[0])
size = min(req_w / aspect, req_h)
w = int(size * aspect)
h = int(size)
shape = (h, w) + im.shape[2:]
result_im = transform.resize(
im, shape, order=params['interpolation degree'].value,
mode='constant', anti_aliasing=True)
if params['padding'].value:
pad_h = req_h - h
pad_w = req_w - w
padded_im = np.zeros((req_h, req_w) + im.shape[2:])
x0 = int(pad_w/2)
x1 = x0 + result_im.shape[1]
y0 = int(pad_h/2)
y1 = y0 + result_im.shape[0]
padded_im[y0:y1, x0:x1] = result_im
return padded_im
return result_im
def alg_padding_twosides(im, params):
if len(im.shape) < 3:
im = im.reshape(im.shape+(1,))
add_alpha = params['add alpha'].value
left = params['left'].value
right = params['right'].value
top = params['top'].value
bottom = params['bottom'].value
k = params['k'].value
new_height = im.shape[0] + abs(top) + abs(bottom)
new_width = im.shape[1] + abs(left) + abs(right)
result = np.full((new_height, new_width, im.shape[2]+add_alpha), k)
y_start = max(0, top)
y_end = y_start + im.shape[0]
x_start = max(0, left)
x_end = x_start + im.shape[1]
if add_alpha:
result[y_start:y_end, x_start:x_end, :-1] = im
result[y_start:y_end, x_start:x_end, -1] = np.ones(im.shape[:2])
else:
result[y_start:y_end, x_start:x_end] = im
return result
def alg_crop_image(im, params):
x, y = params['x'].value, params['y'].value
w, h = params['width'].value, params['height'].value
shape = im.shape
x, y = min(x, shape[1]), min(y, shape[0])
w, h = min(w, shape[1]-x), min(h, shape[0]-y)
return im[y:y+h, x:x+w]
def alg_resize_insert_image(im, params):
"""
Pads the input image to a larger size with a specified alignment position.
The padded area is filled with a constant padding value.
"""
if len(im.shape) < 3:
im = im.reshape(im.shape+(1,))
targert_h = params['height'].value
target_w = params['width'].value
alignment = params['alignment'].value
k = params['k'].value
im_h, im_w = im.shape[0], im.shape[1]
if im_h > targert_h or im_w > target_w:
raise ValueError("Input image is larger than target dimensions.")
pad_h = targert_h - im_h
pad_w = target_w - im_w
alignment_map = {
'center': (pad_h // 2, pad_w // 2),
'center left': (pad_h // 2, 0),
'center right': (pad_h // 2, pad_w),
'top center': (0, pad_w // 2),
'top left': (0, 0),
'top right': (0, pad_w),
'bottom left': (pad_h, 0),
'bottom right': (pad_h, pad_w),
'bottom center': (pad_h, pad_w // 2),
}
pad_ht, pad_side = alignment_map.get(alignment)
result = np.full((targert_h, target_w, im.shape[2]), k)
result[pad_ht:pad_ht+im.shape[0], pad_side:pad_side+im.shape[1]] = im
return result
API_URL = 'http://scikit-image.org/docs/0.13.x/api/'
INTERPOLATION_DEGREE_DESC = (
'Degree of polynomial (0 - 5) used for interpolation.\n'
'0 - no interpolation, 1 - bi-linear interpolation, '
'3 - bi-cubic interpolation'
)
TRANSFORM_ALGS = {
'resize': {
'description': 'Resizes an image to match the given dimensions',
'width': 'The new width of the image',
'height': 'The new height of the image',
'interpolation degree': INTERPOLATION_DEGREE_DESC,
'multi_chromatic': True,
'aspect': 'Preserve aspect ratio (gives smaller size on one axis)',
'padding': (
'Adds padding to fill out full width/height after '
'aspect-correct scaling'),
'url': API_URL+'skimage.transform.html#skimage.transform.resize',
'algorithm': alg_resize
},
'rescale': {
'description': 'Rescales an image by a given factor',
'scale x': 'Scale factor along X direction (horizontal)',
'scale y': 'Scale factor along Y direction (vertical)',
'interpolation degree': INTERPOLATION_DEGREE_DESC,
'multi_chromatic': True,
'url': API_URL+'skimage.transform.html#skimage.transform.rescale',
'algorithm': resize_args
},
'rotate': {
'description': 'Rotates an image',
'angle': 'Angular degrees to rotate counterclockwise',
'resize': (
'If true new image dimensions are calculated '
'to exactly fit the image'),
'multi_chromatic': True,
'url': API_URL+'skimage.transform.html#skimage.transform.rotate',
'algorithm': lambda im, par: transform.rotate(
np.copy(im), par['angle'].value, resize=par['resize'].value)
},
'padding': {
'description': 'Adds independent padding to all four sides of an image',
'left': 'Amount of padding to add on the left side',
'right': 'Amount of padding to add on the right side',
'top': 'Amount of padding to add on the top side',
'bottom': 'Amount of padding to add on the bottom side',
'k': 'Constant value used in padded areas',
'add alpha': (
'Adds an alpha with value 1.0 inside image, 0.0 outside'),
'multi_chromatic': True,
'algorithm': alg_padding_twosides,
},
'crop': {
'description': 'Crops the image to the given rectanglular area',
'x': 'Left edge of image',
'y': 'Top edge of image',
'width': 'Width of image',
'height': 'Height of image',
'multi_chromatic': False,
'algorithm': alg_crop_image,
},
'pad to size': {
'description': (
'Inserts the image into a larger canvas of given size '
'with specified alignment and padding value'),
'width': 'Width of the resulting image',
'height': 'Height of the resulting image',
'alignment': (
'Alignment of the original image within the new canvas: '
'top left, top center, top right, center left, center, '
'center right, bottom left, bottom center, bottom right'),
'k': 'Constant value used in padded areas',
'multi_chromatic': True,
'algorithm': alg_resize_insert_image,
},
}
TRANSFORM_PARAMETERS = [
'x', 'y', 'left', 'right', 'top', 'bottom', 'width', 'height', 'padding', 'aspect',
'interpolation degree', 'scale x', 'scale y', 'angle', 'resize',
'alignment', 'k', 'add alpha',
]
TRANSFORM_TYPES = {
'angle': float, 'scale y': float, 'scale x': float, 'k': float,
'height': int, 'padding': bool, 'width': int, 'aspect': bool,
'left': int, 'right': int, 'top': int, 'bottom': int,
'y': int, 'x': int, 'interpolation degree': int,
'add alpha': bool, 'resize': bool,
'alignment': [
'center',
'center left',
'center right',
'top center',
'top left',
'top right',
'bottom left',
'bottom right',
'bottom center'
],
}
TRANSFORM_DEFAULTS = {
'angle': 0.0, 'scale y': 1.0, 'scale x': 1.0, 'k': 0.05, 'height': 512,
'padding': False, 'width': 512, 'aspect': False, 'y': 0, 'x': 0,
'left': 0, 'right': 0, 'top': 0, 'bottom': 0, 'interpolation degree': 3,
'add alpha': False, 'resize': True,
'alignment': 'center',
}