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:

enter image description here

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

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

enter image description here

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

with rio.open("vein_template.tif", 'r') as ds:
    RGB_arr = ds.read(masked=True)  # read all raster values
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


# 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()
f.add_subplot(1, 2, 1)
plt.imshow(dx, cmap = "Greys")
f.add_subplot(1, 2, 2)
plt.imshow(dy, cmap = "Greys")
  • 1
    $\begingroup$ 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. $\endgroup$ Nov 13, 2019 at 6:33
  • $\begingroup$ @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. $\endgroup$ Nov 14, 2019 at 2:39
  • 1
    $\begingroup$ 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. $\endgroup$ Nov 14, 2019 at 4:59
  • $\begingroup$ 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. $\endgroup$ Nov 14, 2019 at 22:15
  • $\begingroup$ @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? $\endgroup$ Nov 16, 2019 at 2:03

2 Answers 2


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.

  • 1
    $\begingroup$ 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? $\endgroup$ Nov 14, 2019 at 2:44
  • $\begingroup$ Do you mean: a "theory" on how to build edge detectors? $\endgroup$ Nov 14, 2019 at 22:20
  • $\begingroup$ Yes, I meant that $\endgroup$ Nov 15, 2019 at 7:12
  • $\begingroup$ 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.) $\endgroup$ Nov 23, 2019 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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.