I want to find the partial derivative of an image with respect to its X coordinate. I read that it can be done by convolution of the image with a mask like:

             fx = conv2(im1, 0.25 * [-1 1; -1 1]) 

I do not understand how is it calculating the partial derivative - can anyone explain?


2 Answers 2


Partial derivatives in discrete domains are computed using differences (finite difference in more proper terms). There are many methods to do that such as central finite difference or forward, backward and so on.

The convolution operation you mentioned computes forward distances in horizontal direction and results in x-derivatives (namely the $x$ component of the gradient).

To also compute in $y$ direction you might do:

fx = conv2(im1,0.25* [-1 -1; 1 1]) 

0.25 is there just to scale the values to appropriate range (weights each pixel the same).

For more information on finite differencing you might check: http://en.wikipedia.org/wiki/Finite_difference#Forward.2C_backward.2C_and_central_differences

Convolution is a local-masking operation where at each pixel you compute the dot product of the kernel and matrix part that corresponds to the kernel. Then, kernel is shifted. I won't go to the details of convolution as it is already explaind in http://en.wikipedia.org/wiki/Convolution. However, let's say we have a matrix

$ I= \left[ {\begin{array}{cc} 1 & 2 & 4 \\ 6 & 14 & 17\\ 16 & 41 & 18\\ \end{array} } \right] $

If we convolve this with the kernel mentioned and discard for now the boundary conditions, we end up with

$ G= \left[ {\begin{array}{cc} -2.25 & -1.25 \\ -8.25 & 5.0 \\ \end{array} } \right] $

(this is part in the middle of the result).

So, you should notice that basically, per each element, we compute a sum of 2 neighbors vertically on the left, sum of 2 neighbors vertically on the right and subtract these intermediate results. As subtraction is done horizontally, it computes the horizontal derivatives. This is what convolution intrinsically does.

  • $\begingroup$ please explain how this convolution is computing forward distances in horizontal direction $\endgroup$
    – Amit_DSP
    Jan 10, 2014 at 8:27
  • $\begingroup$ Please see updated post. $\endgroup$ Jan 10, 2014 at 9:16

@tbirdal's explanation using finite differences is of course right, but there's another way to understand this: Linear operators like convolution or taking a partial derivative are associative. Meaning:

$\frac{\partial}{\partial x}\text{convolution}(f,g) = convolution(\frac{\partial}{\partial x}f,g)$

So, even when the partial derivative of a function is undefined, the partial derivative of a convolution of that function with some kernel function may be defined.

For example, the partial derivative of the convolution with a triangle function is equivalent to convolution with the derivative of the triangle function - which is just your kernel (note the shift by 0.5 pixels):

enter image description here

If you think of your image as a series of dirac pulses or as a piecewise constant function, convolving it with a smoothing kernel, then taking the derivative makes perfect sense. And that's just what your convolution mask does.

Another common derivative kernel is the Gaussian derivative, which is equivalent to Gaussian smoothing, then taking the derivative.


Your Answer

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

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