Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Sobel operator looks that way?

For image derivative computation, Sobel operator looks this way:

[-1 0 1] [-2 0 2] [-1 0 1] 

I don't quite understand 2 things about it,

1.Why the centre pixel is 0? Can't I just use an operator like below,

[-1 1] [-1 1] [-1 1] 

2.Why the centre row is 2 times the other rows?

I googled my questions, didn't find any answer which can convince me. Please help me.

like image 725
Alcott Avatar asked Jun 13 '13 02:06

Alcott


People also ask

How does the Sobel operator work?

The Sobel filter is used for edge detection. It works by calculating the gradient of image intensity at each pixel within the image. It finds the direction of the largest increase from light to dark and the rate of change in that direction.

Why are there Sobel operators?

The sobel operator is very similar to Prewitt operator. It is also a derivate mask and is used for edge detection. Like Prewitt operator sobel operator is also used to detect two kinds of edges in an image: Vertical direction.

Why does Sobel operator have 2?

A factor of 2 gives the original Sobel filter.

Why do we use Sobel filters?

The Sobel operator, sometimes called the Sobel–Feldman operator or Sobel filter, is used in image processing and computer vision, particularly within edge detection algorithms where it creates an image emphasising edges.


2 Answers

In computer vision, there's very often no perfect, universal way of doing something. Most often, we just try an operator, see its results and check whether they fit our needs. It's true for gradient computation too: Sobel operator is one of many ways of computing an image gradient, which has proved its usefulness in many usecases.

In fact, the simpler gradient operator we could think of is even simpler than the one you suggest above:

[-1 1] 

Despite its simplicity, this operator has a first problem: when you use it, you compute the gradient between two positions and not at one position. If you apply it to 2 pixels (x,y) and (x+1,y), have you computed the gradient at position (x,y) or (x+1,y)? In fact, what you have computed is the gradient at position (x+0.5,y), and working with half pixels is not very handy. That's why we add a zero in the middle:

[-1 0 1] 

Applying this one to pixels (x-1,y), (x,y) and (x+1,y) will clearly give you a gradient for the center pixel (x,y).

This one can also be seen as the convolution of two [-1 1] filters: [-1 1 0] that computes the gradient at position (x-0.5,y), at the left of the pixel, and [0 -1 1] that computes the gradient at the right of the pixel.

Now this filter still has another disadvantage: it's very sensitive to noise. That's why we decide not to apply it on a single row of pixels, but on 3 rows: this allows to get an average gradient on these 3 rows, that will soften possible noise:

[-1 0 1] [-1 0 1] [-1 0 1] 

But this one tends to average things a little too much: when applied to one specific row, we lose much of what makes the detail of this specific row. To fix that, we want to give a little more weight to the center row, which will allow us to get rid of possible noise by taking into account what happens in the previous and next rows, but still keeping the specificity of that very row. That's what gives the Sobel filter:

[-1 0 1] [-2 0 2] [-1 0 1] 

Tampering with the coefficients can lead to other gradient operators such as the Scharr operator, which gives just a little more weight to the center row:

[-3  0 3 ] [-10 0 10] [-3  0 3 ] 

There are also mathematical reasons to this, such as the separability of these filters... but I prefer seeing it as an experimental discovery which proved to have interesting mathematical properties, as experiment is in my opinion at the heart of computer vision. Only your imagination is the limit to create new ones, as long as it fits your needs...

like image 109
mbrenon Avatar answered Nov 13 '22 20:11

mbrenon


EDIT The true reason that the Sobel operator looks that way can be be found by reading an interesting article by Sobel himself. My quick reading of this article indicates Sobel's idea was to get an improved estimate of the gradient by averaging the horizontal, vertical and diagonal central differences. Now when you break the gradient into vertical and horizontal components, the diagonal central differences are included in both, while the vertical and horizontal central differences are only included in one. Two avoid double counting the diagonals should therefore have half the weights of the vertical and horizontal. The actual weights of 1 and 2 are just convenient for fixed point arithmetic (and actually include a scale factor of 16).

I agree with @mbrenon mostly, but there are a couple points too hard to make in a comment.

Firstly in computer vision, the "Most often, we just try an operator" approach just wastes time and gives poor results compared to what might have been achieved. (That said, I like to experiment too.)

It is true that a good reason to use [-1 0 1] is that it centres the derivative estimate at the pixel. But another good reason is that it is the central difference formula, and you can prove mathematically that it gives a lower error in its estmate of the true derivate than [-1 1].

[1 2 1] is used to filter noise as mbrenon, said. The reason these particular numbers work well is that they are an approximation of a Gaussian which is the only filter that does not introduce artifacts (although from Sobel's article, this seems to be coincidence). Now if you want to reduce noise and you are finding a horizontal derivative you want to filter in the vertical direction so as to least affect the derivate estimate. Convolving transpose([1 2 1]) with [-1 0 1] we get the Sobel operator. i.e.:

[1]            [-1 0 1] [2]*[-1 0 1] = [-2 0 2] [1]            [-1 0 1] 
like image 35
Bull Avatar answered Nov 13 '22 20:11

Bull