# why do directional derivatives work better with a gaussian filter?

I've been working on image segmentation and computing the directional derivatives of a grey-scaled image, with the objective of detecting contours and edges. I have realised that if I apply a gaussian filter before computing the directional derivatives, the edges get enhanced better. So my question is - why does that happen?

Here's the original image:

This is what the first order derivatives (dx and dy) look like (without gaussian filter):

here's the derivatives after applying gaussian filtering to the image, with sigma = 6:

*******Python code to compute the derivatives

with rio.open("vein_template.tif", 'r') as ds:
rgb_1 = np.rot90(np.transpose(RGB_arr, [1, 2, 0]))
#the rgb image contains 4 channels, so I'm changing their order, and making the image upright

%matplotlib
plt.imshow(rgb_1)

# Transform to grey-level img
gsi = 0.2989 * rgb_1[:, :, 0] + 0.5870 * rgb_1[:, :, 1] + 0.1140 * rgb_1[:, :, 2]
plt.imshow(gsi, cmap = "gist_gray")

#compute derivatives
dx = np.diff(gsi, axis = 0)
dy = np.diff(gsi, axis = 1)

f = plt.figure()
plt.imshow(dx, cmap = "Greys")
plt.title("dx")
plt.imshow(dy, cmap = "Greys")
plt.title("dy")

• The images shown in the question seem decimated. This is why the horizontal derivative has gaps at the edges of the black figure. Please show the actual images or a region of the actual images in their true resolution. – Olli Niemitalo Nov 13 '19 at 6:33
• @OlliNiemitalo, I have not downsampled the image. This is a toy example that I have drawn in illustrator, with the sole objective of testing different workflows for edge detection. – Angela R. Nov 14 '19 at 2:39
• The image height as shown in the figures is 368 pixels whereas the labels indicate an image height of about 2900 pixels. Whatever you draw the figures with seems to be decimating the images. The quality of the derivatives cannot be safely assessed from these figures. – Olli Niemitalo Nov 14 '19 at 4:59
• There are very anisotropic images in some domains, like in seismic. Yet, the isolated black/white dots in the first derivatives are strange to me. – Laurent Duval Nov 14 '19 at 22:15
• @LaurentDuval I've edited the question with the python code I've used to compute the derivatives. Could you let me know what might be wrong? – Angela R. Nov 16 '19 at 2:03

I would rephrase your terms as:

• the "directional derivatives" are not so directional (although sometimes called similarly in lecture, they are only horizontal and vertical. Truer "directional derivatives" would allow angular refinement, cf. non-separable filters (Deformable Kernels for Early Vision, Perona)
• the "directional derivatives" are not so derivatives. They are the most basic two-point differences. Better separable (Optimally Rotation-Equivariant Directional Derivative Kernels, Farid, Simoncelli) filters can be designed.

The 2-point gradients are very poor filters in terms of frequency separation, and indeed your image is locally of quite high frequency content, because it is piecewise continuous. Hence, the filters behave clumsily. This is even worse if you subsample, as you can miss edges. If you apply blockwise the convolution $$[1\,,-1]$$ on pixels $$[0\,,0\,,1\,,1]$$, you will only see zeroes. This happen for instance with Haar wavelets.

Most used derivative filters are based on the observation that patches are often:

• singular across the edge (sharp variation)
• regular along the edge (smooth variation)

This is not fully true on crossings and corners. However, one often combines a smoothing and a (better) derivative, like in the very simple Sobel edge detector filter:

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

combining a 3-point derivative $$\begin{bmatrix}1 & 0& -1 \end{bmatrix}$$ (a little better than $$\begin{bmatrix}1 & -1 \end{bmatrix}$$), and a 3-point smoother $$\begin{bmatrix}1 & 2& 1 \end{bmatrix}$$, which turne out to be a very short discrete Gaussian approximation, based on Pascal's triangle.

In your case, you both, with the Gaussian:

• created a longer smoothing filter in one direction,
• created a longer gradient filter in other direction, as it looks like a Gaussian derivative.

This combination is better adapted to your image morphology. Yet, other more directional filter designs are possible.

• Thank you for the thorough answer, Laurent. Could you point me to bibliography that contains this kind of "theory"? In other words, do you recommend a good book that could help me understand image processing theory better, just like the way you just explained? – Angela R. Nov 14 '19 at 2:44
• Do you mean: a "theory" on how to build edge detectors? – Laurent Duval Nov 14 '19 at 22:20
• Yes, I meant that – Angela R. Nov 15 '19 at 7:12
• Still digging, I have not found a clear source, yet those could help: unit.eu/cours/videocommunication/Linear_filtering.pdf (p. 23 sq.) or me.umn.edu/courses/me5286/vision/VisionNotes/2017/… (p. 30 sq.) – Laurent Duval Nov 23 '19 at 13:23

Well, look at your original picture: it's constant for all points but the edges, which means your derivative is zero for all points but these edges.

By applying a "rounding, smoothing" filter to it, you "smear" the edges enough to make the derivative be non-zero for multiple pixels, in every direction.