# Image Processing/ Computer Vision: How is separability of filters implemented?

I am trying to convince myself that a separable 2D filter can be implemented via two 1D filters. So, I took the example of following Sobel filter:

$$1/8\begin{bmatrix}-1&0&1\\-2&0&2\\-1&0&1\end{bmatrix}$$

Based on Szeliski

This can be separated as :

$$1/2\begin{bmatrix}-1&0&1\end{bmatrix}$$ and $$1/2\begin{bmatrix}-1\\0\\1\end{bmatrix}$$

Consider an image as shown below:

$$\begin{bmatrix}1&2&3\\4&5&6\\7&8&9\end{bmatrix}$$

Let us first perform a normal 2D convolution which involves:

1) flipping the kernel horizontally and vertically (i.e first interchange first and last rows, then interchange first and last columns).

2) applying the flipped kernel on the image (the border is assumed to be zero padded).

3) Normalizing values by dividing them by 8.

This yields the output:

$$\begin{bmatrix}-1.125&-0.75&1.125\\-2.5&-1&2.5\\-2.625&-0.75&2.625\end{bmatrix}$$

Now we can try applying 1D filters:

1) The 1D filter represented by the row vector is flipped (columns are interchanged).

2) The flipped filter is applied to the entire image.

3) The 1D filter represented by the column vector is flipped (rows are interchanged).

4) The flipped filter is applied to the result from first 1D convolution.

5) The values are normalized by dividing them by 4.

This yields the output:

$$\begin{bmatrix}1.25&0.5&-1.25\\1.5&0&-1.5\\-1.25&-0.5&1.25\end{bmatrix}$$

The two results are different. What's the mistake and how to perform these operations properly?

Szeliski isn't saying what you think they're saying.

The vertical part of the separable filter is: $$\mathbf{v_2} = 1/4\begin{bmatrix}1\\2\\1\end{bmatrix}$$ not $$\mathbf{v_1} = 1/2\begin{bmatrix}-1\\0\\1\end{bmatrix}.$$

You can see this by defining: $$\mathbf{h}= 1/2\begin{bmatrix}-1&0&1\end{bmatrix}$$ and looking at

$$\mathbf{K_1} = \mathbf{v_1} \mathbf{h} = \begin{bmatrix} 1/4 & 0 & -1/4\\ 0 & 0 & 0\\ -1/4 & 0 & 1/4 \end{bmatrix}$$

and

$$\mathbf{K_2} = \mathbf{v_2} \mathbf{h}= \begin{bmatrix} -1/8 & 0 & 1/8\\ -1/4 & 0 & 1/4\\ -1/8 & 0 & 1/8 \end{bmatrix}$$

You can see this effect in the image I included in your question: the vertical edges are highlighted, but the horizontal ones disappear. This is because the $$\mathbf{v_2}$$ filter is a low-pass filter, not enhancing edges.

• Thank you for pointing out the error. However, shouldn't the normalization factor for $v_2$ be 4 instead of 2?
– skr
Commented Dec 12, 2018 at 18:08
• @skr_robo Correct! I'll edit to fix.
– Peter K.
Commented Dec 12, 2018 at 18:28