@@ -2030,7 +2030,8 @@ def _array_patch_perimeters(x, rstride, cstride):
2030
2030
"""Extract perimeters of patches from **arr**.
2031
2031
2032
2032
Extracted patches are of size (**rstride** + 1) x (**cstride** + 1) and
2033
- share perimeters with their neighbors.
2033
+ share perimeters with their neighbors. The ordering of the vertices matches
2034
+ that returned by ``_array_perimeter``.
2034
2035
2035
2036
Parameters
2036
2037
----------
@@ -2044,11 +2045,28 @@ def _array_patch_perimeters(x, rstride, cstride):
2044
2045
assert rstride > 0 and cstride > 0
2045
2046
assert (x .shape [0 ] - 1 ) % rstride == 0
2046
2047
assert (x .shape [1 ] - 1 ) % cstride == 0
2047
- upper = _unfold (x [:- 1 :rstride , :- 1 ], 1 , cstride , cstride )
2048
- lower = _unfold (x [rstride ::rstride , 1 :], 1 , cstride , cstride )[..., ::- 1 ]
2048
+ # We build up each perimeter from four half-open intervals. Here is an
2049
+ # illustrated explanation for rstride == cstride == 3
2050
+ #
2051
+ # T T T R
2052
+ # L R
2053
+ # L R
2054
+ # L B B B
2055
+ #
2056
+ # where T means that this element will be in the top array, R for right,
2057
+ # B for bottom and L for left. Each of the arrays below has a shape of:
2058
+ #
2059
+ # (number of perimeters that can be extracted vertically,
2060
+ # number of perimeters that can be extracted horizontally,
2061
+ # cstride for top and bottom and rstride for left and right)
2062
+ #
2063
+ # Note that _unfold doesn't incur any memory copies, so the only costly
2064
+ # operation here is the np.concatenate.
2065
+ top = _unfold (x [:- 1 :rstride , :- 1 ], 1 , cstride , cstride )
2066
+ bottom = _unfold (x [rstride ::rstride , 1 :], 1 , cstride , cstride )[..., ::- 1 ]
2049
2067
right = _unfold (x [:- 1 , cstride ::cstride ], 0 , rstride , rstride )
2050
2068
left = _unfold (x [1 :, :- 1 :cstride ], 0 , rstride , rstride )[..., ::- 1 ]
2051
- return (np .concatenate ((upper , right , lower , left ), axis = 2 )
2069
+ return (np .concatenate ((top , right , bottom , left ), axis = 2 )
2052
2070
.reshape (- 1 , 2 * (rstride + cstride )))
2053
2071
2054
2072
0 commit comments