def coarse_to_fine(I0, I1, solver, downscale=2, nlevel=10, min_size=16, dtype=np.float32): """Generic coarse to fine solver. Parameters ---------- I0 : ndarray The first gray scale image of the sequence. I1 : ndarray The second gray scale image of the sequence. solver : callable The solver applyed at each pyramid level. downscale : float The pyramid downscale factor. nlevel : int The maximum number of pyramid levels. min_size : int The minimum size for any dimension of the pyramid levels. dtype : dtype Output data type. Returns ------- flow : ndarray The estimated optical flow components for each axis. """ if I0.shape != I1.shape: raise ValueError("Input images should have the same shape") if np.dtype(dtype).char not in 'efdg': raise ValueError("Only floating point data type are valid" " for optical flow") pyramid = list( zip(get_pyramid(_convert(I0, dtype), downscale, nlevel, min_size), get_pyramid(_convert(I1, dtype), downscale, nlevel, min_size))) # Initialization to 0 at coarsest level. flow = np.zeros((pyramid[0][0].ndim, ) + pyramid[0][0].shape, dtype=dtype) flow = solver(pyramid[0][0], pyramid[0][1], flow) for J0, J1 in pyramid[1:]: flow = solver(J0, J1, resize_flow(flow, J0.shape)) return flow
def test_keypoints_orb_less_than_desired_no_of_keypoints(dtype): _img = _convert(img, dtype) detector_extractor = ORB(n_keypoints=15, fast_n=12, fast_threshold=0.33, downscale=2, n_scales=2) detector_extractor.detect(_img) exp_rows = np.array([108., 203., 140., 65., 58.]) exp_cols = np.array([293., 267., 202., 130., 291.]) exp_scales = np.array([1., 1., 1., 1., 1.]) exp_orientations = np.array( [151.93906, -56.90052, -79.46341, -59.42996, -158.26941]) exp_response = np.array( [-0.1764169, 0.2652126, -0.0324343, 0.0400902, 0.2667641]) assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0]) assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1]) assert_almost_equal(exp_scales, detector_extractor.scales) assert_almost_equal(exp_response, detector_extractor.responses) assert_almost_equal(exp_orientations, np.rad2deg(detector_extractor.orientations), 3) detector_extractor.detect_and_extract(img) assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0]) assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
def test_keypoints_orb_desired_no_of_keypoints(dtype): _img = _convert(img, dtype) detector_extractor = ORB(n_keypoints=10, fast_n=12, fast_threshold=0.20) detector_extractor.detect(_img) exp_rows = np.array( [141., 108., 214.56, 131., 214.272, 67., 206., 177., 108., 141.]) exp_cols = np.array( [323., 328., 282.24, 292., 281.664, 85., 260., 284., 328.8, 267.]) exp_scales = np.array([1, 1, 1.44, 1, 1.728, 1, 1, 1, 1.2, 1]) exp_orientations = np.array([ -53.97446153, 59.5055285, -96.01885186, -149.70789506, -94.70171899, -45.76429535, -51.49752849, 113.57081195, 63.30428063, -79.56091118 ]) exp_response = np.array([ 1.01168357, 0.82934145, 0.67784179, 0.57176438, 0.56637459, 0.52248355, 0.43696175, 0.42992376, 0.37700486, 0.36126832 ]) assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0]) assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1]) assert_almost_equal(exp_scales, detector_extractor.scales) assert_almost_equal(exp_response, detector_extractor.responses, 5) assert_almost_equal(exp_orientations, np.rad2deg(detector_extractor.orientations), 4) detector_extractor.detect_and_extract(img) assert_almost_equal(exp_rows, detector_extractor.keypoints[:, 0]) assert_almost_equal(exp_cols, detector_extractor.keypoints[:, 1])
def test_subclass_conversion(): """Check subclass conversion behavior""" x = np.array([-1, 1]) for dtype in float_dtype_list: x = x.astype(dtype) y = _convert(x, np.floating) assert y.dtype == x.dtype
def test_order_0_warp_dtype(dtype): img = _convert(astronaut()[:10, :10, 0], dtype) assert resize(img, (12, 12), order=0).dtype == dtype assert rescale(img, 0.5, order=0).dtype == dtype assert rotate(img, 45, order=0).dtype == dtype assert warp_polar(img, order=0).dtype == dtype assert swirl(img, order=0).dtype == dtype
def color_deconvolution(img, sep_matrix, comb_matrix, component_idx): separated = separate_stains(img, sep_matrix) stains = np.zeros_like(separated) stains[:, :, component_idx] = separated[:, :, component_idx] combined = combine_stains(stains, comb_matrix) return _convert(combined, img.dtype)
def test_range_extra_dtypes(dtype_in, dt): """Test code paths that are not skipped by `test_range`""" imin, imax = dtype_range_extra[dtype_in] x = np.linspace(imin, imax, 10).astype(dtype_in) y = _convert(x, dt) omin, omax = dtype_range_extra[dt] _verify_range("From %s to %s" % (np.dtype(dtype_in), np.dtype(dt)), y, omin, omax, np.dtype(dt))
def test_nonzero_order_warp_dtype(dtype, order): img = _convert(astronaut()[:10, :10, 0], dtype) float_dtype = _supported_float_type(dtype) assert resize(img, (12, 12), order=order).dtype == float_dtype assert rescale(img, 0.5, order=order).dtype == float_dtype assert rotate(img, 45, order=order).dtype == float_dtype assert warp_polar(img, order=order).dtype == float_dtype assert swirl(img, order=order).dtype == float_dtype
def test_float_conversion_dtype(): """Test any convertion from a float dtype to an other.""" x = np.array([-1, 1]) # Test all combinations of dtypes conversions dtype_combin = np.array(np.meshgrid(float_dtype_list, float_dtype_list)).T.reshape(-1, 2) for dtype_in, dtype_out in dtype_combin: x = x.astype(dtype_in) y = _convert(x, dtype_out) assert y.dtype == np.dtype(dtype_out)
def apply_C_filter(image, dll, c_function, filter_size, mode='constant', cval=0): """ Apply a C function based filter on image Parameters ---------- image : 2D array Input image. dll: str or a ctypes dll object If str: A dll name (including full path if not on the default search path). c_function: str or int If str: The name of the variable in the dll pointing to the function. If int: A Function pointer. filter_size : (2,) array Filter shape. mode : str Padding mode. cval : a scalar Padding fill value (applicable is mode == 'const') Returns ------- output : array A 2D array of the same dtype and shape as the input array. """ # A temporary confinment to float32 image = _convert(image, np.float32) if type(dll) is str: dll = ctypes.cdll.LoadLibrary(dll) if type(c_function) is str: pfcn = ctypes.c_voidp.in_dll(dll, c_function).value else: pfcn = c_function # Prepare paded data padded = fiter.gen_filter_matrix(image, filter_size, mode=mode, cval=cval) output = np.empty_like(image) padded.filter_with_C_callback_float(pfcn, output) return output
def test_keypoints_sift(dtype): _img = _convert(img, dtype) detector_extractor = SIFT() detector_extractor.detect_and_extract(_img) exp_keypoint_rows = np.array([18, 18, 19, 22, 26, 26, 30, 31, 31, 32]) exp_keypoint_cols = np.array( [331, 331, 325, 330, 310, 330, 205, 323, 149, 338]) exp_octaves = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) exp_position_rows = np.array([ 17.81909936, 17.81909936, 19.05454661, 21.85933727, 25.54800708, 26.25710504, 29.90826307, 30.78713806, 30.87953572, 31.72557969 ]) exp_position_cols = np.array([ 331.49693187, 331.49693187, 325.24476016, 330.44616424, 310.33932904, 330.46155224, 204.74535177, 322.84100812, 149.43192282, 337.89643013 ]) exp_orientations = np.array([ 0.26391655, 0.26391655, 0.39134262, 1.77063053, 0.98637565, 1.37997279, 0.4919992, 1.48615988, 0.33753212, 1.64859617 ]) exp_scales = np.array([2, 2, 1, 3, 3, 1, 2, 1, 1, 1]) exp_sigmas = np.array([ 1.35160379, 1.35160379, 0.94551567, 1.52377498, 1.55173233, 0.93973722, 1.37594124, 1.06663786, 1.04827034, 1.0378916 ]) exp_scalespace_sigmas = np.array( [[0.8, 1.00793684, 1.26992084, 1.6, 2.01587368, 2.53984168], [1.6, 2.01587368, 2.53984168, 3.2, 4.03174736, 5.07968337], [3.2, 4.03174736, 5.07968337, 6.4, 8.06349472, 10.15936673], [6.4, 8.06349472, 10.15936673, 12.8, 16.12698944, 20.31873347], [12.8, 16.12698944, 20.31873347, 25.6, 32.25397888, 40.63746693], [25.6, 32.25397888, 40.63746693, 51.2, 64.50795775, 81.27493386]]) assert_almost_equal(exp_keypoint_rows, detector_extractor.keypoints[:10, 0]) assert_almost_equal(exp_keypoint_cols, detector_extractor.keypoints[:10, 1]) assert_almost_equal(exp_octaves, detector_extractor.octaves[:10]) assert_almost_equal(exp_position_rows, detector_extractor.positions[:10, 0], decimal=4) assert_almost_equal(exp_position_cols, detector_extractor.positions[:10, 1], decimal=4) assert_almost_equal(exp_orientations, detector_extractor.orientations[:10], decimal=4) assert_almost_equal(exp_scales, detector_extractor.scales[:10]) assert_almost_equal(exp_sigmas, detector_extractor.sigmas[:10], decimal=4) assert_almost_equal(exp_scalespace_sigmas, detector_extractor.scalespace_sigmas, decimal=4) detector_extractor2 = SIFT() detector_extractor2.detect(img) detector_extractor2.extract(img) assert_almost_equal(detector_extractor.keypoints[:10, 0], detector_extractor2.keypoints[:10, 0]) assert_almost_equal(detector_extractor.keypoints[:10, 0], detector_extractor2.keypoints[:10, 0])