Finding Edges


Let’s investigate how image intensity varies along the line that I have shown here in red and, in particular, let’s focus on what happens here at the tail of the P. If we look at the intensity profile here and these pixel values, these grey scale values are double precision values that vary between zero for black and one for white, and we see that it starts off with a low value of around 0.2. Whereas on the black paint — the background of the sign — they rise up to a value of around 0.8 and when we cross the white paint, which is the tail of the P, and then fall off again to low background values.

Back onto the black paint. We might be interested in looking at the horizontal gradient and we can approximate the gradient as the difference between a pixel and its neighbour on the left-hand side — a simple numerical approximation to the gradient. However, this is not a very symmetrical way to think about this. So instead we can represent it this way: the intensity in the horizontal direction of pixel U is the difference between its neighbour on the right-hand side and its neighbour on the left-hand side, divided by 2. This would represent the horizontal gradient.

Now the gradient expressed in this form we can write as a kernel — it is a small kernel represented by a 1 by 3 matrix. If we apply this kernel to the input image the result would be an output image whose pixels comprise the horizontal gradient at the corresponding point.

Here is an animation similar to ones we have seen before; in red, we see the input window marching across the image from left to right and from top to bottom. As it is moving we are computing the output values and dropping them into the output image. Note again that we are not computing the left-hand edge and right-hand edge of the output image because this is where the input window falls off the input image. We can see the gradient being computed, we can see areas of gentle horizontal gradient we can see the areas of much stronger horizontal gradient where we have this continuity and we can see areas where the gradient is negative.
Let’s apply this to the image that we were looking at earlier and we can do that quite simply using the tool box function iconv. We parse in the first argument, which is the kernel, and the second argument, which is the image.

What we can see here is that the left-hand edges of the letters are coded blue; that means they have positive gradient that is where the intensity is changing from the dark of the background to the white of the letters. On the right-hand side of the letters where the intensity falls from white down to the dark background it has got a negative value and this is shown as red. So what this kernel is doing is highlighting vertical lines within the scene because vertical lines have got a strong horizontal gradient.

I denote this image as I subscript u (Iu). This is a common way to denote image gradients. The subscript represents the direction in which the gradient is being computed.

Now we can do a very similar thing by rotating the kernel and now we can compute the gradient in the vertical direction. So the kernel now is the transpose of the kernel we used to compute horizontal gradient. It will allow us to compute vertical gradient.

Now, we can apply that to our penguin sign, this is what the kernel looks like and this is what the output looks like. It has some similarities to what we saw before. As we move down in a column, we see that the tops of letters are blue and this indicates that they have got a strong positive gradient. Remember that in images the vertical coordinate increases downwards. So at the top of a letter we are moving from the dark background onto the white paint and so we have a strong positive gradient. On the bottom of the letters there is a strong negative gradient if you fall off the white paint down onto the black background. So the vertical gradient image has highlighted the horizontal lines within the image. We can compute this in MATLAB using almost the same function as before except we have transposed the kernel. The kernel is a 1 by 3 matrix and we use MATLAB’s transpose operator the quote symbol in order to transpose that kernel into a 3 by 1 matrix.

A very common kernel used in image processing is called the Sobel kernel, and it has some similarities to the simple kernel that we used before. I like to think of this kernel as computing the horizontal gradient on the current line and averaging that with the horizontal gradient on the line above and on the line below. We can obtain the value of this kernel using the tool box function ksobel. If I apply this function to our penguin image I can obtain a horizontal gradient image that is shown here, which looks quite similar to the horizontal gradient we just looked at. The function isobel we parse at an image and it returns both a horizontal gradient image and a vertical gradient image.

A really important feature in an image — a very common feature in images of man-made scenes — are what we call edges. What I am going to do is to load an image that we have seen before and to display that, and here is the sign about penguins again, and what I am going to do is to look at an intensity profile along roughly a horizontal line from here to there, and what we see is the intensity has got a low value to start with and then as we cross each of the pieces of white paint on the sign we see the intensity goes up. Now let’s zoom in a little bit on that, it is a little bit too busy. I am going to expand this region here, which is the beginning of the word ‘look’, and what we see quite clearly here is that the pixels have got quite a low value, less than 0.2 which is the background. The dark background of the sign moves up to around 0.6, 0.7. The white paint as we cross the letter L—down again to a value of 0.2, and then pixel value increases as we cross the remaining letters. What we can see is that as we move from the dark to the light the intensity increases quite rapidly. Then it falls quite rapidly on the other side of the character, so we are going to explore this idea of gradient or the rate of change at the grey level as we move horizontally across the sign.

Often in image processing we are interested in areas of the image where there is a lot of gradient. We don’t care whether it is horizontal or vertical; we don’t care whether it is positive or negative. We just want to know where there is a lot of gradient.

And this is computed quite simply by computing the square root of the sum of the squares of the horizontal and vertical gradients and results in an image like this.

Most of it is black because there isn’t much gradient over the background. Where there is a lot of gradient around the edges of letters this function has got a very high value. So this is a great operator for highlighting the edges of objects within a scene. And we can obtain an image like this using the function isobel with no output arguments.

We might also be interested in the direction of maximum gradient; in this image pixels are increasing by a value of 10 every pixel in the horizontal direction and by 5 every pixel vertically. So the direction of maximum gradient change is in this direction, 26° with respect to horizontal.

Now we can apply that same technique to the Sobel image that we computed before. We can compute a gradient image and that is the arc tangent of the vertical gradient image element wise divided by the horizontal gradient image. It looks a little bit too busy, so let’s zoom in on the letter P.
And if we look at the gradient direction on the left-hand edge of the P, we see that it has got a gradient direction value of 0. It’s coded as white.

So if we look at the colour bar over here, we can see that 0 corresponds to zero gradient angle and we expect this on this side of the letter; the gradient is increasing in the horizontal direction from left to right. On the other side of the letter the gradient is, in fact, increasing from right to left because it is at this point that we are following from the high white value down to the dark value. So the gradient is right to left and we can see on this side of the letter the gradient angles are either plus π, which are shown as dark blue or negative π, which is shown as dark red.


There is no code in this lesson.

When we look at an image we discern objects, and these tend to be groups of similar pixels surrounded by a distinctive edge. We look at intensity profiles in images and use spatial operators with kernels such as the Sobel kernel to find the intensity gradients in an image, and from these find edges in an image.

Professor Peter Corke

Professor of Robotic Vision at QUT and Director of the Australian Centre for Robotic Vision (ACRV). Peter is also a Fellow of the IEEE, a senior Fellow of the Higher Education Academy, and on the editorial board of several robotics research journals.

Skill level

This content assumes an understanding of high school level mathematics; for example, trigonometry, algebra, calculus, physics (optics) and experience with MATLAB command line and programming, for example workspace, variables, arrays, types, functions and classes.

More information...

Rate this lesson


Check your understanding

Leave a comment