From b6b2eb708c87c3a961cb78c6c24e18c700c1fa22 Mon Sep 17 00:00:00 2001 From: Tony S Yu Date: Mon, 8 Oct 2012 20:30:33 -0400 Subject: [PATCH 1/3] DOC: Add imports to doctests to fix errors --- skimage/morphology/grey.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/skimage/morphology/grey.py b/skimage/morphology/grey.py index e7e52de4..45d11729 100644 --- a/skimage/morphology/grey.py +++ b/skimage/morphology/grey.py @@ -45,6 +45,7 @@ def erosion(image, selem, out=None, shift_x=False, shift_y=False): Examples -------- >>> # Erosion shrinks bright regions + >>> import numpy as np >>> from skimage.morphology import square >>> bright_square = np.array([[0, 0, 0, 0, 0], ... [0, 1, 1, 1, 0], @@ -97,6 +98,7 @@ def dilation(image, selem, out=None, shift_x=False, shift_y=False): Examples -------- >>> # Dilation enlarges bright regions + >>> import numpy as np >>> from skimage.morphology import square >>> bright_pixel = np.array([[0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0], @@ -146,6 +148,7 @@ def opening(image, selem, out=None): Examples -------- >>> # Open up gap between two bright regions (but also shrink regions) + >>> import numpy as np >>> from skimage.morphology import square >>> bad_connection = np.array([[1, 0, 0, 0, 1], ... [1, 1, 0, 1, 1], @@ -196,6 +199,7 @@ def closing(image, selem, out=None): Examples -------- >>> # Close a gap between two bright lines + >>> import numpy as np >>> from skimage.morphology import square >>> broken_line = np.array([[0, 0, 0, 0, 0], ... [0, 0, 0, 0, 0], @@ -245,6 +249,7 @@ def white_tophat(image, selem, out=None): Examples -------- >>> # Subtract grey background from bright peak + >>> import numpy as np >>> from skimage.morphology import square >>> bright_on_grey = np.array([[2, 3, 3, 3, 2], ... [3, 4, 5, 4, 3], @@ -294,6 +299,7 @@ def black_tophat(image, selem, out=None): Examples -------- >>> # Change dark peak to bright peak and subtract background + >>> import numpy as np >>> from skimage.morphology import square >>> dark_on_grey = np.array([[7, 6, 6, 6, 7], ... [6, 5, 4, 5, 6], From 3b67c8204255757391b3a4f76e4cc778a6ad5072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sun, 2 Sep 2012 22:54:24 +0200 Subject: [PATCH 2/3] Add support for 3-dimensional image resizing --- skimage/transform/_warps.py | 49 ++++++++++++++++++++------- skimage/transform/tests/test_warps.py | 34 ++++++++++++++++++- 2 files changed, 70 insertions(+), 13 deletions(-) diff --git a/skimage/transform/_warps.py b/skimage/transform/_warps.py index 71bc41b8..bde13f3d 100644 --- a/skimage/transform/_warps.py +++ b/skimage/transform/_warps.py @@ -1,4 +1,5 @@ import numpy as np +from scipy import ndimage from ._geometric import (warp, SimilarityTransform, AffineTransform, ProjectiveTransform) @@ -11,7 +12,10 @@ def resize(image, output_shape, order=1, mode='constant', cval=0.): image : ndarray Input image. output_shape : tuple or ndarray - Size of the generated output image `(rows, cols)`. + Size of the generated output image `(rows, cols[, dim])`. If `dim` is + not provided, the number of channels are preserved. In case the number + of input channels does not equal the number of output channels a + 3-dimensional interpolation is applied. Returns ------- @@ -32,24 +36,45 @@ def resize(image, output_shape, order=1, mode='constant', cval=0.): """ - rows, cols = output_shape + rows, cols = output_shape[0], output_shape[1] orig_rows, orig_cols = image.shape[0], image.shape[1] row_scale = float(orig_rows) / rows col_scale = float(orig_cols) / cols - # 3 control points necessary to estimate exact AffineTransform - src_corners = np.array([[1, 1], [1, rows], [cols, rows]]) - 1 - dst_corners = np.zeros(src_corners.shape, dtype=np.double) - # take into account that 0th pixel is at position (0.5, 0.5) - dst_corners[:, 0] = col_scale * (src_corners[:, 0] + 0.5) - 0.5 - dst_corners[:, 1] = row_scale * (src_corners[:, 1] + 0.5) - 0.5 + # 3-dimensional interpolation + if len(output_shape) == 3 and (image.ndim == 2 + or output_shape[2] != image.shape[2]): + dim = output_shape[2] + orig_dim = 1 if image.ndim == 2 else image.shape[2] + dim_scale = float(orig_dim) / dim - tform = AffineTransform() - tform.estimate(src_corners, dst_corners) + map_rows, map_cols, map_dims = np.mgrid[:rows, :cols, :dim] + map_rows = row_scale * (map_rows + 0.5) - 0.5 + map_cols = col_scale * (map_cols + 0.5) - 0.5 + map_dims = dim_scale * (map_dims + 0.5) - 0.5 - return warp(image, tform, output_shape=output_shape, order=order, - mode=mode, cval=cval) + coord_map = np.array([map_rows, map_cols, map_dims]) + + out = ndimage.map_coordinates(image, coord_map, order=order, mode=mode, + cval=cval) + + else: # 2-dimensional interpolation + + # 3 control points necessary to estimate exact AffineTransform + src_corners = np.array([[1, 1], [1, rows], [cols, rows]]) - 1 + dst_corners = np.zeros(src_corners.shape, dtype=np.double) + # take into account that 0th pixel is at position (0.5, 0.5) + dst_corners[:, 0] = col_scale * (src_corners[:, 0] + 0.5) - 0.5 + dst_corners[:, 1] = row_scale * (src_corners[:, 1] + 0.5) - 0.5 + + tform = AffineTransform() + tform.estimate(src_corners, dst_corners) + + out = warp(image, tform, output_shape=output_shape, order=order, + mode=mode, cval=cval) + + return out def rescale(image, scale, order=1, mode='constant', cval=0.): diff --git a/skimage/transform/tests/test_warps.py b/skimage/transform/tests/test_warps.py index 69980b25..e40d2bf0 100644 --- a/skimage/transform/tests/test_warps.py +++ b/skimage/transform/tests/test_warps.py @@ -85,7 +85,7 @@ def test_rotate(): assert_array_almost_equal(x90, np.rot90(x)) -def test_resize(): +def test_resize2d(): x = np.zeros((5, 5), dtype=np.double) x[1, 1] = 1 resized = resize(x, (10, 10), order=0) @@ -112,6 +112,38 @@ def test_scale(): assert_array_almost_equal(scaled, ref) +def test_resize3d(): + # keep 3rd dimension + x = np.zeros((5, 5, 3), dtype=np.double) + x[1, 1, :] = 1 + resized = resize(x, (10, 10), order=0) + ref = np.zeros((10, 10, 3)) + ref[2:4, 2:4, :] = 1 + assert_array_almost_equal(resized, ref) + resized = resize(x, (10, 10, 3), order=0) + assert_array_almost_equal(resized, ref) + + # resize 3rd dimension + x = np.zeros((5, 5, 3), dtype=np.double) + x[1, 1, :] = 1 + resized = resize(x, (10, 10, 1), order=0) + ref = np.zeros((10, 10, 1)) + ref[2:4, 2:4] = 1 + assert_array_almost_equal(resized, ref) + + # bilinear 3rd dimension + x = np.zeros((5, 5, 2), dtype=np.double) + x[1, 1, 0] = 0 + x[1, 1, 1] = 1 + resized = resize(x, (10, 10, 1), order=1) + ref = np.zeros((10, 10, 1)) + ref[1:5, 1:5, :] = 0.03125 + ref[1:5, 2:4, :] = 0.09375 + ref[2:4, 1:5, :] = 0.09375 + ref[2:4, 2:4, :] = 0.28125 + assert_array_almost_equal(resized, ref) + + def test_swirl(): image = img_as_float(data.checkerboard()) From 0856dad9889d76768e8b2d452af88db9259cc5d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 9 Oct 2012 11:19:27 +0200 Subject: [PATCH 3/3] Split 3d resize test function --- skimage/transform/_warps.py | 3 +++ skimage/transform/tests/test_warps.py | 26 +++++++++++++++----------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/skimage/transform/_warps.py b/skimage/transform/_warps.py index bde13f3d..6689430b 100644 --- a/skimage/transform/_warps.py +++ b/skimage/transform/_warps.py @@ -348,3 +348,6 @@ def homography(image, H, output_shape=None, order=1, tform = ProjectiveTransform(H) return warp(image, inverse_map=tform.inverse, output_shape=output_shape, order=order, mode=mode, cval=cval) + + return warp(image, inverse_map=tform.inverse, output_shape=output_shape, + order=order, mode=mode, cval=cval) diff --git a/skimage/transform/tests/test_warps.py b/skimage/transform/tests/test_warps.py index e40d2bf0..1993f932 100644 --- a/skimage/transform/tests/test_warps.py +++ b/skimage/transform/tests/test_warps.py @@ -85,16 +85,7 @@ def test_rotate(): assert_array_almost_equal(x90, np.rot90(x)) -def test_resize2d(): - x = np.zeros((5, 5), dtype=np.double) - x[1, 1] = 1 - resized = resize(x, (10, 10), order=0) - ref = np.zeros((10, 10)) - ref[2:4, 2:4] = 1 - assert_array_almost_equal(resized, ref) - - -def test_scale(): +def test_rescale(): # same scale factor x = np.zeros((5, 5), dtype=np.double) x[1, 1] = 1 @@ -112,7 +103,16 @@ def test_scale(): assert_array_almost_equal(scaled, ref) -def test_resize3d(): +def test_resize2d(): + x = np.zeros((5, 5), dtype=np.double) + x[1, 1] = 1 + resized = resize(x, (10, 10), order=0) + ref = np.zeros((10, 10)) + ref[2:4, 2:4] = 1 + assert_array_almost_equal(resized, ref) + + +def test_resize3d_keep(): # keep 3rd dimension x = np.zeros((5, 5, 3), dtype=np.double) x[1, 1, :] = 1 @@ -123,6 +123,8 @@ def test_resize3d(): resized = resize(x, (10, 10, 3), order=0) assert_array_almost_equal(resized, ref) + +def test_resize3d_resize(): # resize 3rd dimension x = np.zeros((5, 5, 3), dtype=np.double) x[1, 1, :] = 1 @@ -131,6 +133,8 @@ def test_resize3d(): ref[2:4, 2:4] = 1 assert_array_almost_equal(resized, ref) + +def test_resize3d_bilinear(): # bilinear 3rd dimension x = np.zeros((5, 5, 2), dtype=np.double) x[1, 1, 0] = 0