There are many different image processing techniques out there, and today we will cover a method to detect edges in images using special kernels called ‘Sobel Filters’.
First of all, let’s upload an image to our Colab environment.
In case you have not heard of what Colab is, don’t panic but just click the URL below. This is an interactive iPython Notebook powered by Google.
We will use a robot image today.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2import numpy as np%matplotlib inline# Read in the image
image = mpimg.imread(‘FILE NAME’)
We will load ‘matplotlib.pyplot’ to picture the image, ‘matplotlib.image’ to read an image. ‘cv2’ is for OpenCV library (https://opencv.org). OpenCV offers a wide variety of image processing tools.
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
plt.imshow(gray, cmap=’gray’)
Now, after loading the image, we have converted it from RGB to that of grayscale. It looks like this.
Now, we will use a custom made kernels. You can modify the values in the kernel or the size of the kernel to see how the kernel works on an input image. Here, we will use a Sobel filter, also called a Sobel Operator. I will add the Wiki URL below so that you can check.
Briefly, it is an algorithm to detect edges in an image. Using a Sobel filter alongside the x-axis or y-axis is like obtaining the derivative of the image in each direction.
Sobel filters look like below picture (I drew this, lol).
We can see that each filter is the transpose of the other.
Let us apply this filter to our cute little robot image!
sobel_x = np.array([[ -1, 0, 1],
[ -2, 0, 2],
[ -1, 2, 1]])sobel_y = np.array([[ -1, -2, -1],
[ 0, 0, 0],
[ 1, 2, 1]])
We have declared both Sobel filter on the x-axis, and on the y-axis using Numpy array.
And by suing filter2D function of OpenCV library, we will apply this custom made kernel on our image.
filtered_image = cv2.filter2D(gray, -1, sobel_y)plt.imshow(filtered_image, cmap=’gray’)
Filter2D’s inputs are a grayscale image, and value for bit-depth, and the kernel that we are going to use. Let’s see the result of the Sobel filter operated on the y-axis.
Very creepy, but we can surely see that this works.
Actually, OpenCV has already made a Sobel Operater function in its package.
sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1)plt.imshow(sobely, cmap = ‘gray’)
The name of the function is just ‘Sobel’ so we can use it very easily.
The result of the above line is,
Like this. Very gray, and not really visually well defined. To deal with this, we will set up a threshold and make the value to 0 and 1, so that it shows the detected lines more clearly. (Code credits to https://inspro9.wordpress.com/2016/12/15/sobel-operator/)
thresh_min = 20thresh_max = 100sxbinary = np.zeros_like(scaled_sobel_y)sxbinary[(scaled_sobel_y >= thresh_min) & (scaled_sobel_y <= thresh_max)] = 1plt.imshow(sxbinary, cmap=’gray’)
And the result is,
Way better!
I hope you enjoyed the posting, and you can just play with many different custom made kernels of yours to see how that works on the image!
I will come back with better stories! :)