Understanding Image Derivative (Basic of computer vision)

Shafayet Ul Islam
3 min readJun 22, 2020

Derivatives as we all know is “the rate of change”. Suppose like speed is rate of change over distance or acceleration is a rate of change over speed.

So, if there is a function f(x), and if we change x a little bit (say from 2 to 2.0001), the derivates will be, how much the f(x) will change in terms of changing the variable x(say from 2 to 2.0001)

so if we express derivative with a formula it can be:

df/dx = lim (∆x → 0) ( f(x) — f(x-∆x) ) / ∆x = f’(x)

Now let’s think about an grey scale image. which has values between 0 to 255 and have a single channel. Thinking the image as a 2D array it now contains rows and columns.

Here, considering this 2D array of an image, the smallest possible change between two pixel value will always be 1. So, our ∆x = 1

And thus, our formula becomes:

df/dx = lim (∆x → 0) ( f(x) — f(x-1) ) / 1= f’(x)
df/dx = lim (∆x → 0) ( f(x) — f(x-1) )= f’(x) ….(1)

Now, let’s go back to 1D array for the simplicity of understanding.

suppose we have f(x) = [5 10 15 15 10 10 20]. if we consider formula (1) we get that, f’(x) = [0 5 5 0 -5 0 10]. Here we are using backward difference, which means, we are calculating difference between each value with it’s previous one. so, (2nd value of array,10) — (1st value of array, 5) = 5, then the (3rd value, 15) — (2nd value, 10) = 5 and so on…

This is how we are getting the first derivative array.

Now, here is the magic, consider an array of [-1,1] (also can call it mask) and let us take this array and multiply it with the first two value of the f(x) (which is 5, 10 ) so we get,

(5 * -1) = -5 and (10*1) = 10
and now we sum these value and we get: -5+10 = 5 (which is the second result we saw in the first derivative array)

Let’s take the last two number: 10 & 20
(10*-1) = -10 & (20*1) = 20
-10 + 20 = 10 ( the last value of f’(x))

MAGIC!!!

Now, for 2D array which is basically is an image (grey scale) we can use derivative mask like this and an important point, this is only for calculating derivatives with respect to x.

As, images are both function of x and y at the same time, different derivative mask has to be used to calculated derivative with respect to y.

A lot of these will be covered in convolution and correlation part along with filtering concept. So, let’s take a break….

--

--

Shafayet Ul Islam

A computer science undergrad who loves to design and develop. I am a machine learning enthusiast who loves to explore with a mug full of coffee.