|
8 | 8 | import copy |
9 | 9 |
|
10 | 10 | __all__ = ['extract_array_2d', 'add_array_2d', 'subpixel_indices', |
11 | | - 'mask_to_mirrored_num'] |
| 11 | + 'mask_to_mirrored_num', 'get_slices'] |
12 | 12 |
|
13 | 13 |
|
14 | | -def _get_slices(large_array_shape, small_array_shape, position): |
| 14 | +def get_slices(large_array_shape, small_array_shape, position): |
15 | 15 | """ |
16 | 16 | Get slices for the overlapping part of a small and a large array. |
17 | 17 |
|
@@ -43,10 +43,10 @@ def _get_slices(large_array_shape, small_array_shape, position): |
43 | 43 | Slice in x direction for the small array. |
44 | 44 | """ |
45 | 45 | # Get edge coordinates |
46 | | - y_min = int(position[1] + 0.5) - small_array_shape[0] // 2 |
47 | | - x_min = int(position[0] + 0.5) - small_array_shape[1] // 2 |
48 | | - y_max = int(position[1] + 0.5) + small_array_shape[0] // 2 + 1 |
49 | | - x_max = int(position[0] + 0.5) + small_array_shape[1] // 2 + 1 |
| 46 | + y_min = int(position[1] + 0.5 - small_array_shape[0] / 2.) |
| 47 | + x_min = int(position[0] + 0.5 - small_array_shape[1] / 2.) |
| 48 | + y_max = int(position[1] + 0.5 + small_array_shape[0] / 2.) |
| 49 | + x_max = int(position[0] + 0.5 + small_array_shape[1] / 2.) |
50 | 50 |
|
51 | 51 | # Set up slices in x direction |
52 | 52 | s_x = slice(max(0, x_min), min(large_array_shape[1], x_max)) |
@@ -76,21 +76,21 @@ def extract_array_2d(array_large, shape, position): |
76 | 76 |
|
77 | 77 | Examples |
78 | 78 | -------- |
79 | | - We consider a large array of zeros with the shape 11x10 and a small |
80 | | - array of ones with a shape of 3x4: |
| 79 | + We consider a large array with the shape 11x10, from which we extract |
| 80 | + a small array of shape 3x5: |
81 | 81 |
|
82 | 82 | >>> import numpy as np |
83 | 83 | >>> from imageutils.array_utils import extract_array_2d |
84 | 84 | >>> large_array = np.arange(110).reshape((11, 10)) |
85 | 85 | >>> large_array[4:9, 4:9] = np.ones((5, 5)) |
86 | | - >>> extract_array_2d(large_array, (3, 4), (7, 7)) |
| 86 | + >>> extract_array_2d(large_array, (3, 5), (7, 7)) |
87 | 87 | array([[ 1, 1, 1, 1, 69], |
88 | 88 | [ 1, 1, 1, 1, 79], |
89 | 89 | [ 1, 1, 1, 1, 89]]) |
90 | 90 | """ |
91 | 91 | # Check if larger array is really larger |
92 | 92 | if array_large.shape >= shape: |
93 | | - s_y, s_x, _, _ = _get_slices(array_large.shape, shape, position) |
| 93 | + s_y, s_x, _, _ = get_slices(array_large.shape, shape, position) |
94 | 94 | return array_large[s_y, s_x] |
95 | 95 | else: |
96 | 96 | raise Exception("Can't extract array. Shape too large.") |
@@ -128,7 +128,7 @@ def add_array_2d(array_large, array_small, position): |
128 | 128 | """ |
129 | 129 | # Check if larger array is really larger |
130 | 130 | if array_large.shape >= array_small.shape: |
131 | | - s_y, s_x, b_y, b_x = _get_slices(array_large.shape, |
| 131 | + s_y, s_x, b_y, b_x = get_slices(array_large.shape, |
132 | 132 | array_small.shape, position) |
133 | 133 | array_large[s_y, s_x] += array_small[b_y, b_x] |
134 | 134 | return array_large |
|
0 commit comments