From 3e881fe35c373802fd83d0bef44704076a9482c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 16 Dec 2014 02:22:12 +0100 Subject: [PATCH] Create helper function for image conversion --- skimage/transform/_geometric.py | 20 +++++++++++++------- skimage/transform/_warps.py | 12 ++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/skimage/transform/_geometric.py b/skimage/transform/_geometric.py index 0b74fe48..a44a5eb5 100644 --- a/skimage/transform/_geometric.py +++ b/skimage/transform/_geometric.py @@ -995,6 +995,18 @@ def warp_coords(coord_map, shape, dtype=np.float64): return coords +def _convert_warp_input(image, keep_range): + """Convert input image to double image with the appropriate range.""" + if keep_range: + image = image.astype(np.double) + else: + if image.dtype == np.double: + image = rescale_intensity(image) + else: + image = img_as_float(image) + return image + + def _clip_warp_output(input_image, output_image, clip, mode, order, cval): """Clip output image to range of values of input image, considering the parameters of a call to warp. @@ -1146,13 +1158,7 @@ def warp(image, inverse_map=None, map_args={}, output_shape=None, order=1, """ - if keep_range: - image = image.astype(np.double) - else: - if image.dtype == np.double: - image = rescale_intensity(image) - else: - image = img_as_float(image) + image = _convert_warp_input(image, keep_range) input_shape = np.array(image.shape) diff --git a/skimage/transform/_warps.py b/skimage/transform/_warps.py index 7ebcad6f..6dad0865 100644 --- a/skimage/transform/_warps.py +++ b/skimage/transform/_warps.py @@ -1,11 +1,9 @@ import numpy as np from scipy import ndimage -from ..util import img_as_float -from ..exposure import rescale_intensity from ..measure import block_reduce from ._geometric import (warp, SimilarityTransform, AffineTransform, - _clip_warp_output) + _convert_warp_input, _clip_warp_output) def resize(image, output_shape, order=1, mode='constant', cval=0, clip=True, @@ -81,13 +79,7 @@ def resize(image, output_shape, order=1, mode='constant', cval=0, clip=True, coord_map = np.array([map_rows, map_cols, map_dims]) - if keep_range: - image = image.astype(np.double) - else: - if image.dtype == np.double: - image = rescale_intensity(image) - else: - image = img_as_float(image) + image = _convert_warp_input(image, keep_range) out = ndimage.map_coordinates(image, coord_map, order=order, mode=mode, cval=cval)