
On 6 Dec 2019, at 5:20 am, Stefan van der Walt <stefanv@berkeley.edu> wrote:
I’ll submit a PR soon but the outcome was clean enough that I wanted to share with you ahead of time my n-dimensional Sobel filter here:
https://nbviewer.jupyter.org/github/jni/skimage-tutorials/blob/monash-df2-co... <https://nbviewer.jupyter.org/github/jni/skimage-tutorials/blob/monash-df2-co...>
(search for "def sobel”).
That's too smart for me; could you explain the construction of the kernel?
Yep: sobel 2D is separable into two convolutions, performed along orthogonal axes: 1. A smoothing convolution [1, 2, 1], and 2. A difference convolution, [1, 0, -1], in the orthogonal direction. This gives the filter: [1, 2, 1] [0, 0, 0] [-1, -2, -1] or its transpose. The generalisation to higher dimensions is to have an edge filter along one axis and the smoothing filter along all the remaining axes. This can be done by reshaping each kernel to have extent 3 along the correct dims, then multiplying — numpy broadcasting takes care of the rest. e.g. a 3D Sobel with the difference along the 0th axis is: [[[-1]], [[0]], [[1]]] * [[[1], [2], [1]]] * [[[1, 2, 1]]] (where the above shapes are (3, 1, 1), (1, 3, 1), and (1, 1, 3)).