<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Creating a Filter, Edge Detection
Import resources and display image
Step1:  Convert the image to grayscale
Step2:  TODO
Step3:  Test out other filters!
You're encouraged to create other kinds of filters and apply them to see what happens! As an optional exercise, try the following
<ASSISTANT_TASK:>
Python Code:
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import cv2
import numpy as np
%matplotlib inline
# Read in the image
image = mpimg.imread(fname='images/curved_lane.jpg')
plt.imshow(X=image)
Explanation: Creating a Filter, Edge Detection
Import resources and display image
End of explanation
# Convert to grayscale for filtering
gray = cv2.cvtColor(src=image,
                    code=cv2.COLOR_RGB2GRAY)
plt.imshow(X=gray, cmap='gray')
Explanation: Convert the image to grayscale
End of explanation
# Create a custom kernel
# 3x3 array for edge detection
sobel_y = np.array([[ -1, -2, -1],
                    [ 0, 0, 0], 
                    [ 1, 2, 1]])
## TODO: Create and apply a Sobel x operator
sobel_x = np.array([[ -1, 0, 1],
                    [ -2, 0, 2], 
                    [ -1, 0, 1]])
# Filter the image using filter2D, which has inputs: (grayscale image, bit-depth, kernel)  
filtered_image = cv2.filter2D(src=gray,
                              ddepth=-1,
                              kernel=sobel_y)
filtered_image2 = cv2.filter2D(src=gray,
                               ddepth=-1,
                               kernel=sobel_x)
plt.imshow(X=filtered_image,
           cmap='gray')
plt.imshow(X=filtered_image2,
           cmap='gray')
Explanation: TODO: Create a custom kernel
Below, you've been given one common type of edge detection filter: a Sobel operator.
The Sobel filter is very commonly used in edge detection and in finding patterns in intensity in an image. Applying a Sobel filter to an image is a way of taking (an approximation) of the derivative of the image in the x or y direction, separately. The operators look as follows.
<img src="images/sobel_ops.png" width=200 height=200>
It's up to you to create a Sobel x operator and apply it to the given image.
For a challenge, see if you can put the image through a series of filters: first one that blurs the image (takes an average of pixels), and then one that detects the edges.
End of explanation
image2 = mpimg.imread(fname='images/white_lines.jpg')
gray2 = cv2.cvtColor(src=image2,
                     code=cv2.COLOR_RGB2GRAY)
sample_filter = np.array([[ 0, 0, 2, 0, 0],
                          [ 0, 0, 1, 0, 0],
                          [ 2, -1, 1, -1, 2],
                          [ 0, 0, 1, 0, 0],
                          [ 0, 0, 2, 0, 0]])
filtered_image3 = cv2.filter2D(src=gray2,
                               ddepth=-1,
                               kernel=sample_filter)
plt.imshow(X=gray2,
           cmap='gray')
plt.imshow(X=filtered_image3,
           cmap='gray')
Explanation: Test out other filters!
You're encouraged to create other kinds of filters and apply them to see what happens! As an optional exercise, try the following:
* Create a filter with decimal value weights.
* Create a 5x5 filter
* Apply your filters to the other images in the images directory.
End of explanation
<END_TASK>