O'Reilly logo

Computer Vision with Python 3 by Saurabh Kapur

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sobel edge detection

The Sobel() function in OpenCV can be used to find the edges of an image. The Sobel() function takes the image, output image depth, order of derivative in x and y direction, and the size of the kernel as input. In the following code, we will look at both horizontal and vertical edges:

>>> import cv2>>> img = cv2.imread("image.jpg")>>> gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)>>> x_edges = cv2.Sobel(gray,-1,1,0,ksize=5)>>> cv2.imwrite("sobel_edges_x.jpg", x_edges)>>> y_edges = cv2.Sobel(gray,-1,0,1,ksize=5)>>> cv2.imwrite("sobel_edges_y.jpg", y_edges)>>> cv2.imshow("xedges", x_edges)>>> cv2.imshow("yedges", y_edges)

In the code, we have taken the order of derivative in x direction equal to 1 and the order of derivative ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required