
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Image Gradients Using Scharr Operator in OpenCV Python
Using the Scharr operator, we can compute image gradients in horizontal as well as vertical direction using first order derivatives. The gradients are computed for a grayscale image. You can apply Scharr operation on an image using the method cv2.scharr().
Syntax
The following syntax is used to compute the image gradients using Scharr derivative ?
cv2.Scharr(img, ddepth, xorder, yorder)
Parameters
img ? The original input image
ddepth ? Desired depth of the output image. It has information about what kind of data is stored in the output image. We use cv2.CV_64F to as ddepth. It is a 64bit floating-point opencv
xorder ? The order of derivatives in horizontal direction (X-direction). Set xorder=1, yorder=0 for the 1st order derivative in X-direction.
Yorder ? The order of derivatives in vertical direction (Y-direction). Set xorder=0, yorder=1 for the 1st order derivative in Y-direction.
Steps
You can use the following steps to find image gradients using Scharr derivative ?
Import the required library. In all the following Python examples, the required Python library is OpenCV. Make sure you have already installed it.
import cv2
Read the input image using cv2.imread() as a grayscale image.
img = cv2.imread('lines.jpg',0)
Compute the Sobel or Laplacian derivative using cv2.Scharr(). This derivative refers to the image gradient.
scharrx = cv2.Scharr(img,cv2.CV_64F,1,0)
Display the image gradient using cv2.imshow() method.
cv2.imshow("Scharr X", scharrx) cv2.waitKey(0) cv2.destroyAllWindows()
Let's have a look at some examples for more clear understanding.
Example 1
In the python example below, we compute the image gradient using the Scharr operator in X (horizontal) as well as Y (vertical) directions.
# import required libraries import cv2 # read the input image as a grayscale image img = cv2.imread('window.jpg',0) # compute the 1st order Sobel derivative in x direction scharrx = cv2.Scharr(img,cv2.CV_64F,1,0) # compute the 1st order Sobel derivative in y direction scharry = cv2.Scharr(img,cv2.CV_64F,0,1) # display scharrx and scharry cv2.imshow("Scharr X", scharrx) cv2.waitKey(0) cv2.imshow("Scharr Y", scharry) cv2.waitKey(0) cv2.destroyAllWindows()
We will use the following image "window.jpg" as the Input File in the above program.
Output
When you execute the above program, it will produce the following two output windows? "Scharr X" and "Scharr Y".
Example 2
In the Python example below, we compute the image gradient using Scharr operator in X (horizontal) as well as Y (vertical) directions.
# import required libraries import cv2 # read the input image as a grayscale image img = cv2.imread('tutorialspoint.png',0) # compute the 1st order Sobel derivative in x direction scharrx = cv2.Scharr(img,cv2.CV_64F,1,0) # compute the 1st order Sobel derivative in y direction scharry = cv2.Scharr(img,cv2.CV_64F,0,1) # display scharrx and scharry cv2.imshow("Scharr X", scharrx) cv2.waitKey(0) cv2.imshow("Scharr Y", scharry) cv2.waitKey(0) cv2.destroyAllWindows()
We will use this image "tutorialspoint.png" as the Input File in the above program.
Output
The above program on execution will produce the following two output windows