def map_coordinates(cls, image, dx, dy, order=1, cval=0, mode="constant"): # small debug message to be sure that the right function is executed print("map_coordinates() with cv2") if order == 0 and image.dtype.name in ["uint64", "int64"]: raise Exception(( "dtypes uint64 and int64 are only supported in ElasticTransformation for order=0, " + "got order=%d with dtype=%s.") % (order, image.dtype.name)) input_dtype = image.dtype if image.dtype.name == "bool": image = image.astype(np.float32) elif order == 1 and image.dtype.name in ["int8", "int16", "int32"]: image = image.astype(np.float64) elif order >= 2 and image.dtype.name == "int8": image = image.astype(np.int16) elif order >= 2 and image.dtype.name == "int32": image = image.astype(np.float64) shrt_max = 32767 backend = "cv2" if order == 0: bad_dtype_cv2 = (image.dtype.name in [ "uint32", "uint64", "int64", "float128", "bool" ]) elif order == 1: bad_dtype_cv2 = (image.dtype.name in [ "uint32", "uint64", "int8", "int16", "int32", "int64", "float128", "bool" ]) else: bad_dtype_cv2 = (image.dtype.name in [ "uint32", "uint64", "int8", "int32", "int64", "float128", "bool" ]) bad_dx_shape_cv2 = (dx.shape[0] >= shrt_max or dx.shape[1] >= shrt_max) bad_dy_shape_cv2 = (dy.shape[0] >= shrt_max or dy.shape[1] >= shrt_max) if bad_dtype_cv2 or bad_dx_shape_cv2 or bad_dy_shape_cv2: backend = "scipy" ia.do_assert(image.ndim == 3) result = np.copy(image) height, width = image.shape[0:2] # False was added here, only difference to usual code if False or backend == "scipy": h, w = image.shape[0:2] y, x = np.meshgrid(np.arange(h).astype(np.float32), np.arange(w).astype(np.float32), indexing='ij') x_shifted = x + (-1) * dx y_shifted = y + (-1) * dy for c in sm.xrange(image.shape[2]): remapped_flat = ndimage.interpolation.map_coordinates( image[..., c], (x_shifted.flatten(), y_shifted.flatten()), order=order, cval=cval, mode=mode) remapped = remapped_flat.reshape((height, width)) result[..., c] = remapped else: h, w, nb_channels = image.shape y, x = np.meshgrid(np.arange(h).astype(np.float32), np.arange(w).astype(np.float32), indexing='ij') x_shifted = x + (-1) * dx y_shifted = y + (-1) * dy if image.dtype.kind == "f": cval = float(cval) else: cval = int(cval) border_mode = cls._MAPPING_MODE_SCIPY_CV2[mode] interpolation = cls._MAPPING_ORDER_SCIPY_CV2[order] is_nearest_neighbour = (interpolation == cv2.INTER_NEAREST) map1, map2 = cv2.convertMaps(x_shifted, y_shifted, cv2.CV_16SC2, nninterpolation=is_nearest_neighbour) # remap only supports up to 4 channels if nb_channels <= 4: result = cv2.remap(image, map1, map2, interpolation=interpolation, borderMode=border_mode, borderValue=cval) if image.ndim == 3 and result.ndim == 2: result = result[..., np.newaxis] else: current_chan_idx = 0 result = [] while current_chan_idx < nb_channels: channels = image[..., current_chan_idx:current_chan_idx + 4] result_c = cv2.remap(channels, map1, map2, interpolation=interpolation, borderMode=border_mode, borderValue=cval) if result_c.ndim == 2: result_c = result_c[..., np.newaxis] result.append(result_c) current_chan_idx += 4 result = np.concatenate(result, axis=2) if result.dtype.name != input_dtype.name: result = meta.restore_dtypes_(result, input_dtype) return result
def map_coordinates(cls, image, dx, dy, order=1, cval=0, mode="constant"): # small debug message to be sure that the right function is executed print("map_coordinates() with cv2") if order == 0 and image.dtype.name in ["uint64", "int64"]: raise Exception(("dtypes uint64 and int64 are only supported in ElasticTransformation for order=0, " + "got order=%d with dtype=%s.") % (order, image.dtype.name)) input_dtype = image.dtype if image.dtype.name == "bool": image = image.astype(np.float32) elif order == 1 and image.dtype.name in ["int8", "int16", "int32"]: image = image.astype(np.float64) elif order >= 2 and image.dtype.name == "int8": image = image.astype(np.int16) elif order >= 2 and image.dtype.name == "int32": image = image.astype(np.float64) shrt_max = 32767 backend = "cv2" if order == 0: bad_dtype_cv2 = (image.dtype.name in ["uint32", "uint64", "int64", "float128", "bool"]) elif order == 1: bad_dtype_cv2 = (image.dtype.name in ["uint32", "uint64", "int8", "int16", "int32", "int64", "float128", "bool"]) else: bad_dtype_cv2 = (image.dtype.name in ["uint32", "uint64", "int8", "int32", "int64", "float128", "bool"]) bad_dx_shape_cv2 = (dx.shape[0] >= shrt_max or dx.shape[1] >= shrt_max) bad_dy_shape_cv2 = (dy.shape[0] >= shrt_max or dy.shape[1] >= shrt_max) if bad_dtype_cv2 or bad_dx_shape_cv2 or bad_dy_shape_cv2: backend = "scipy" ia.do_assert(image.ndim == 3) result = np.copy(image) height, width = image.shape[0:2] # False was added here, only difference to usual code if False or backend == "scipy": h, w = image.shape[0:2] y, x = np.meshgrid(np.arange(h).astype(np.float32), np.arange(w).astype(np.float32), indexing='ij') x_shifted = x + (-1) * dx y_shifted = y + (-1) * dy for c in sm.xrange(image.shape[2]): remapped_flat = ndimage.interpolation.map_coordinates( image[..., c], (x_shifted.flatten(), y_shifted.flatten()), order=order, cval=cval, mode=mode ) remapped = remapped_flat.reshape((height, width)) result[..., c] = remapped else: h, w, nb_channels = image.shape y, x = np.meshgrid(np.arange(h).astype(np.float32), np.arange(w).astype(np.float32), indexing='ij') x_shifted = x + (-1) * dx y_shifted = y + (-1) * dy if image.dtype.kind == "f": cval = float(cval) else: cval = int(cval) border_mode = cls._MAPPING_MODE_SCIPY_CV2[mode] interpolation = cls._MAPPING_ORDER_SCIPY_CV2[order] is_nearest_neighbour = (interpolation == cv2.INTER_NEAREST) map1, map2 = cv2.convertMaps(x_shifted, y_shifted, cv2.CV_16SC2, nninterpolation=is_nearest_neighbour) # remap only supports up to 4 channels if nb_channels <= 4: result = cv2.remap(image, map1, map2, interpolation=interpolation, borderMode=border_mode, borderValue=cval) if image.ndim == 3 and result.ndim == 2: result = result[..., np.newaxis] else: current_chan_idx = 0 result = [] while current_chan_idx < nb_channels: channels = image[..., current_chan_idx:current_chan_idx+4] result_c = cv2.remap(channels, map1, map2, interpolation=interpolation, borderMode=border_mode, borderValue=cval) if result_c.ndim == 2: result_c = result_c[..., np.newaxis] result.append(result_c) current_chan_idx += 4 result = np.concatenate(result, axis=2) if result.dtype.name != input_dtype.name: result = meta.restore_dtypes_(result, input_dtype) return result