Detecting edges
Edge detection is one of the most popular techniques in Computer Vision. It is used as a preprocessing step in many applications. Let's look at how to use different edge detectors to detect edges in the input image.
How to do it…
Create a new Python file, and import the following packages:
import sys import cv2 import numpy as np
Load the input image. We will use
chair.jpg
:# Load the input image -- 'chair.jpg' # Convert it to grayscale input_file = sys.argv[1] img = cv2.imread(input_file, cv2.IMREAD_GRAYSCALE)
Extract the height and width of the image:
h, w = img.shape
Sobel filter is a type of edge detector that uses a 3x3 kernel to detect horizontal and vertical edges separately. You can learn more about it at http://www.tutorialspoint.com/dip/sobel_operator.htm. Let's start with the horizontal detector:
sobel_horizontal = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
Run the vertical Sobel detector:
sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
Laplacian edge detector...