New BERT eBook + 11 Application Notebooks! → The BERT Collection

Gradient vectors (or “image gradients”) are one of the most fundamental concepts in computer vision; many vision algorithms involve computing gradient vectors for each pixel in an image.

After a quick introduction to how gradient vectors are computed, I’ll discuss some of its properties which make it so useful.

A gradient vector can be computed for every pixel an image. It’s simply a measure of the change in pixel values along the x-direction and the y-direction around each pixel.

Let’s look at a simple example; let’s say we want to compute the gradient vector at the pixel highlighted in red below.

This is a grayscale image, so the pixel values just range from 0 - 255 (0 is black, 255 is white). The pixel values to the left and right of our pixel are marked in the image: 56 and 94. We just take the right value minus the left value and say that the rate of change in the x direction is 38 (94 - 56 = 38).

Note: At this pixel, the pixels from dark to light as we move left to right. If we looked at the same spot on the left side of the penguin’s head where the pixels instead change from light to dark, we’d get a negative value for the change. You can compute the gradient by subtracting left from right or right from left, you just have to be consistent across the image.

We can do the same for the pixels above and below to get the change in the y-direction:

93 - 55 = 38 in the y-direction.

Putting these two values together, we now have our gradient vector.

We can also use the equations for the magnitude and angle of a vector to compute those values.

We can now draw the gradient vector as an arrow on the image. Notice how the direction of the gradient vector is perpendicular to the edge of the penguin’s head–this is an important property of gradient vectors.

Let’s see what it looks like to compute the change in the x and y direction at every pixel for the image. Note that the difference in pixel values can range from -255 to 255. This is too many values to store in a byte, so we have to map the values to the range 0 - 255. After performing this mapping, pixels with a large negative change will be black, pixels with a large positive change will be white, and pixels with little or no change will be gray.