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.
import sys import cv2 import numpy as np
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)
h, w = img.shape
sobel_horizontal = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
laplacian = cv2.Laplacian(img, cv2.CV_64F)
canny = cv2.Canny(img, 50, 240)
cv2.imshow('Original', img) cv2.imshow('Sobel horizontal', sobel_horizontal) cv2.imshow('Sobel vertical', sobel_vertical) cv2.imshow('Laplacian', laplacian) cv2.imshow('Canny', canny) cv2.waitKey()
edge_detector.py
file that is already provided to you. The original input image looks like the following:18.216.29.195