Sobel operator is used in computer vision particularly in edge detection algorithms. The operator uses two 3ร3 kernels which are convolved with the original image to calculate the image derivatives โ one for horizontal changes, and one for vertical. If we define A as the original image, and Gx and Gy are two images which at each point contain the horizontal and vertical derivative approximations respectively, the computations are as follows:
#Let A denote a grayscale image
A = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
The image output is:
Now, we have to find the horizontal and vertical derivative of the input image using the kernels as shown below.
Convolution process:
from scipy import signal
kernel_horizontal = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]])
kernel_vertical = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]])
Gx = signal.convolve2d(A, kernel_horizontal, boundary='symm', mode='same')
Gy = signal.convolve2d(A, kernel_vertical, boundary='symm', mode='same')
output images after convolution:
Now we find the sum of squares of the gradients to get the gradient magnitude.
Gradient magnitude:
The above output may not seem like getting us anywhere but when the same is applied to a full real image, this is the output I get.
You can find the full source of the above examples here: