Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to find the image gradients using Sobel and Laplacian derivatives in OpenCV Python?
Image gradients are essential in computer vision for detecting edges and boundaries. OpenCV provides Sobel and Laplacian operators to compute these gradients. The Sobel operator uses first-order derivatives to find gradients in horizontal and vertical directions, while the Laplacian operator uses second-order derivatives.
Syntax
The following syntaxes are used to compute image gradients ?
cv2.Sobel(img, ddepth, dx, dy, ksize) cv2.Laplacian(img, ddepth)
Parameters
img The input grayscale image.
ddepth Output image depth. Use
cv2.CV_64Ffor 64-bit floating-point precision.dx Order of derivative in X-direction (horizontal). Set
dx=1, dy=0for horizontal gradients.dy Order of derivative in Y-direction (vertical). Set
dx=0, dy=1for vertical gradients.ksize Kernel size (odd number). Common values are 3, 5, or 7.
Using Sobel Operator
The Sobel operator computes gradients in X and Y directions separately ?
import cv2
import numpy as np
# Read input image as grayscale
img = cv2.imread('/images/sample.jpg', 0)
# Compute Sobel derivatives in X and Y directions
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5) # Horizontal gradients
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5) # Vertical gradients
# Display results
cv2.imshow("Original", img)
cv2.imshow("Sobel X", np.absolute(sobel_x))
cv2.imshow("Sobel Y", np.absolute(sobel_y))
cv2.waitKey(0)
cv2.destroyAllWindows()
Using Laplacian Operator
The Laplacian operator computes second-order derivatives to detect edges ?
import cv2
import numpy as np
# Read input image as grayscale
img = cv2.imread('/images/sample.jpg', 0)
# Compute Laplacian derivative
laplacian = cv2.Laplacian(img, cv2.CV_64F)
# Display results
cv2.imshow("Original", img)
cv2.imshow("Laplacian", np.absolute(laplacian))
cv2.waitKey(0)
cv2.destroyAllWindows()
Complete Example with Multiple Gradients
This example demonstrates all gradient types together ?
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Read input image
img = cv2.imread('/images/sample.jpg', 0)
# Compute different gradients
sobel_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)
sobel_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
sobel_combined = np.sqrt(sobel_x**2 + sobel_y**2)
laplacian = cv2.Laplacian(img, cv2.CV_64F)
# Create subplot for comparison
fig, axes = plt.subplots(2, 3, figsize=(12, 8))
axes[0,0].imshow(img, cmap='gray')
axes[0,0].set_title('Original')
axes[0,1].imshow(np.absolute(sobel_x), cmap='gray')
axes[0,1].set_title('Sobel X')
axes[0,2].imshow(np.absolute(sobel_y), cmap='gray')
axes[0,2].set_title('Sobel Y')
axes[1,0].imshow(sobel_combined, cmap='gray')
axes[1,0].set_title('Sobel Combined')
axes[1,1].imshow(np.absolute(laplacian), cmap='gray')
axes[1,1].set_title('Laplacian')
axes[1,2].axis('off')
plt.tight_layout()
plt.show()
Key Differences
| Operator | Derivative Order | Detects | Best For |
|---|---|---|---|
| Sobel X | First-order | Vertical edges | Directional edge detection |
| Sobel Y | First-order | Horizontal edges | Directional edge detection |
| Laplacian | Second-order | All edges | General edge detection |
Conclusion
Use Sobel operators for directional gradient computation and edge detection in specific directions. Use Laplacian for general edge detection with second-order derivatives. Both methods are essential for image processing and computer vision applications.
