| 1 |
# -*- coding: utf-8 -*- |
| 2 |
import scipy.ndimage |
| 3 |
|
| 4 |
from ..dispatch._dispatch_ndfilters import (dispatch_convolve, |
| 5 |
dispatch_correlate) |
| 6 |
from ..dispatch._utils import check_arraytypes_compatible |
| 7 |
from . import _utils |
| 8 |
|
| 9 |
__all__ = [ |
| 10 |
"convolve", |
| 11 |
"correlate", |
| 12 |
] |
| 13 |
|
| 14 |
|
| 15 |
@_utils._update_wrapper(scipy.ndimage.convolve) |
| 16 |
def convolve(image, weights, mode="reflect", cval=0.0, origin=0): |
| 17 |
check_arraytypes_compatible(image, weights) |
| 18 |
|
| 19 |
origin = _utils._get_origin(weights.shape, origin) |
| 20 |
depth = _utils._get_depth(weights.shape, origin) |
| 21 |
depth, boundary = _utils._get_depth_boundary(image.ndim, depth, "none") |
| 22 |
|
| 23 |
if mode == "wrap": # Fixes https://github.com/dask/dask-image/issues/242 |
| 24 |
boundary = "periodic" |
| 25 |
mode = "constant" |
| 26 |
|
| 27 |
result = image.map_overlap( |
| 28 |
dispatch_convolve(image), |
| 29 |
depth=depth, |
| 30 |
boundary=boundary, |
| 31 |
dtype=image.dtype, |
| 32 |
meta=image._meta, |
| 33 |
weights=weights, |
| 34 |
mode=mode, |
| 35 |
cval=cval, |
| 36 |
origin=origin, |
| 37 |
) |
| 38 |
|
| 39 |
return result |
| 40 |
|
| 41 |
|
| 42 |
@_utils._update_wrapper(scipy.ndimage.correlate) |
| 43 |
def correlate(image, weights, mode="reflect", cval=0.0, origin=0): |
| 44 |
check_arraytypes_compatible(image, weights) |
| 45 |
|
| 46 |
origin = _utils._get_origin(weights.shape, origin) |
| 47 |
depth = _utils._get_depth(weights.shape, origin) |
| 48 |
depth, boundary = _utils._get_depth_boundary(image.ndim, depth, "none") |
| 49 |
|
| 50 |
if mode == "wrap": # Fixes https://github.com/dask/dask-image/issues/242 |
| 51 |
boundary = "periodic" |
| 52 |
mode = "constant" |
| 53 |
|
| 54 |
result = image.map_overlap( |
| 55 |
dispatch_correlate(image), |
| 56 |
depth=depth, |
| 57 |
boundary=boundary, |
| 58 |
dtype=image.dtype, |
| 59 |
meta=image._meta, |
| 60 |
weights=weights, |
| 61 |
mode=mode, |
| 62 |
cval=cval, |
| 63 |
origin=origin, |
| 64 |
) |
| 65 |
|
| 66 |
return result |