@@ -2030,7 +2030,8 @@ def _array_patch_perimeters(x, rstride, cstride):
20302030 """Extract perimeters of patches from **arr**.
20312031
20322032 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``.
20342035
20352036 Parameters
20362037 ----------
@@ -2044,11 +2045,28 @@ def _array_patch_perimeters(x, rstride, cstride):
20442045 assert rstride > 0 and cstride > 0
20452046 assert (x .shape [0 ] - 1 ) % rstride == 0
20462047 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 ]
20492067 right = _unfold (x [:- 1 , cstride ::cstride ], 0 , rstride , rstride )
20502068 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 )
20522070 .reshape (- 1 , 2 * (rstride + cstride )))
20532071
20542072
0 commit comments