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…

  1. Create a new Python file, and import the following packages:
    import sys
    
    import cv2
    import numpy as np 
  2. 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)
  3. Extract the height and width of the image:
    h, w = img.shape
  4. 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 ...

Get Python Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.