Cam's Blog

The Sobel Operator


April 24, 2020

Sobel operator? Whats that?

The sobel operator is an algorithm used to detect "edges" in images. Let define an edge as a point where there is a difference between it and the neighboring pixels. We can define the intensity of this edge by how big the difference is.

How does it work?

Humans can easily identify edges in images, the sobel operator is an algorithm that finds edges in images. Lets take an example pixel in an image and determine if it is an edge.

In this example, we are evaluating a single pixel in the middle of the building. This location is obviously an edge. But how can we prove it?

Select pixel to evaluate
Get the surrounding pixels
Convert the section to greyscale
Apply kernel to section

You can see the values for each pixel in our section above. Remember, we are evaluating the pixel in the middle.

What is a kernel and how do we use it?

The kernel is a set of values that we will overlay on the 3x3 set of pixels we are evaluating.

Use the kernel by simply multiplying each kernel value by the corresponding pixel value in the grid. Here are the two basic kernels for the sobel operator, one for vertical edges and one for horizontal edges.

Vertical Kernel (Y)
Horizontal Kernel (X)

Lets perform the math for the vertical kernel, because this "edge" is clearly vertical. All you need to do is multiply the kernel values with the corresponding pixel values in. Then, add up all the values from each cell for a final vertical edge score!

Vertical Kernel (Y)
Greyscale Values
54 * 1 = 5481 * 0 = 0175 * -1 = -175
57 * 2 = 11491 * 0 = 0168 * -2 = -336
58 * 1 = 5897 * 0 = 0159 * -1 = -159

Finally, lets add up these values and take the absolute value to get the edge score!

54 + 0 + -175 + 114 + 0 + -336 + 58 + 0 + -159 = -444 -> 444

To summarize this operation elegantly:

"A" = the 3x3 section we are analyzing

What does this number even mean?

Wow a score of 444! Fantastic! In order to figure out what this means we need to compare it to the other pixels in the image.

Take the scores for every other pixel in the image then normalize them to the range [0, 255] to get a feel for how strong this edge is compared to the others. I know you dont care enough to do this yourself, so I did it for you! (High scores are mapped to white pixels, low scores mapped to black)

Original Image
Horizontal (X) Kernel
Vertical (Y) Kernel

Putting it all together

So now we know how to compute the horizontal and vertical components of the sobel operator on any image. Next, lets combine them!

In order to combine each horizontal and vertical "score" into one unified score we use the pythagorean theorem.

"G" = the edge score

Here is the final image with both components combined! Notice how the left side of the building has less definiton on the edges than the right side. Take a look at the original image and try to figure out why! (hint: contrast)

Original Image
Horizontal (X) & Vertical (Y) Kernels

Extra epic moves


We can also determine percise angle of the edge using the atan of the ratio between the two kernel scores.


Lots of images contain noise. Noise will be picked up as small edges. To get a crisp result, apply a gaussian blur of 1-2px on the entire image before processing.

Multiple Dimensions

The sobel operator is not limited to two dimensions, or even just images. You can use the sobel operator on a n'th dimensional dataset and compute edges in data. For example, we could add a "time" dimension and determine how edges change in a video.

Taking this to the next level

For my next project, I will be building off of the sobel operator software I have written. I'm not sure if this will even work, but I will give my best shot at explaining it: Many edges in images are built from rough lines. These lines can be represented as y = mx + b. What if we could plot every possible line connecting the sobel edges using linear regression? Using this technique we could theoretically develop a vector outline of high level features in an image. This would be very epic. Coming soon.

Source Code

You should give this a shot yourself! I made it super easy to execute the sobel operator on whatever image you want with customizable kernels! You can even apply more than 2 kernels!

PS I wrote this like 3 years ago so its probably trash code.

Click here for the src