- Import the necessary packages:
import sys import cv2 import numpy as np
- Read the input image:
in_file = sys.argv[1] image = cv2.imread(in_file, cv2.IMREAD_GRAYSCALE)
- Implement the Sobel edge detection scheme:
horizontal_sobel = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5) vertical_sobel = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5) laplacian_img = cv2.Laplacian(image, cv2.CV_64F) canny_img = cv2.Canny(image, 30, 200)
- Display the input image and its corresponding output:
cv2.imshow('Original', image) cv2.imshow('horizontal Sobel', horizontal_sobel) cv2.imshow('vertical Sobel', vertical_sobel) cv2.imshow('Laplacian image', laplacian_img) cv2.imshow('Canny image', canny_img)
- Wait for the instruction from the operator:
cv2.waitKey()
- Display the input image and the corresponding results:
cv2.imshow('Original', image) cv2.imshow('horizontal Sobel', horizontal_sobel) cv2.imshow('vertical Sobel', vertical_sobel) cv2.imshow('Laplacian image', laplacian_img) cv2.imshow('Canny image', canny_img)
- Wait for the instruction from the operator:
cv2.waitKey()
- The command used to execute the Detecting_edges.py Python program file, along with the input image (baby.jpg), is shown here:
- The input image and the horizontal Sobel filter output obtained after executing the Detecting_edges.py file is shown here:
- The vertical Sobel filter output and the Laplacian image output obtained after executing the Detecting_edges.py file is shown here:
Following is the Laplacian image output:
- The Canny edge detection output obtained after executing the Detecting_edges.py file is shown here: