- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to draw an arrowed line on an image in OpenCV Python?
OpenCV provides the function cv2.arrowedLine() to draw an arrowed line on an image. This function takes different arguments to draw the line. See the syntax below.
cv2.arrowedLine(img, start, end, color, thickness, line_type, shift, tip_length)
img − The input image on which the line is to be drawn.
Start − Start coordinate of the line in (width, height) format.
End − End coordinate of the line in (width, height) format.
Color − Color of the line. For a red color in BGR format we pass (0, 0, 255)
Thickness − Thickness of the line in pixels.
line_type − Type of the line.
shift − The number of fractional bits.
tip_length − The length of the arrow tip in relation to the arrow length.
Output − It returns the image with lines drawn on it.
Steps
Follow the steps given below to draw arrowed lines on an image −
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().
image = cv2.imread('cabinet.jpg')
Draw the arrowed lines on image using cv2.arrowedLine() passing the required arguments.
cv2.arrowedLine(image, (50, 100), (300, 450), (0,0,255), 3, 5, 0, 0.1)
Display the image with arrowed lines drawn on it.
cv2.imshow("ArrowedLine",image) cv2.waitKey(0) cv2.destroyAllWindows()
Let's have a look at some examples for a clear understanding of how it is done.
Example 1
In this program, we draw a red line on the image with the following line properties −
start_point = (50, 100),
end_point = (300, 450),
color = (0,0,255),
thickness = 3,
line_type = 5,
shift = 0, and
tip_length = 0.1
# import required libraries import cv2 # read the input image image=cv2.imread('cabinet.jpg') # Draw the arrowed line passing the arguments cv2.arrowedLine(image, (50, 100), (300, 450), (0,0,255), 3, 5, 0, 0.1) cv2.imshow("ArrowedLine",image) cv2.waitKey(0) cv2.destroyAllWindows()
Output
When you run the above program, it will produce the following output −
Example 2
In this program, we draw three different lines with different line properties −
import cv2 image=cv2.imread('cabinet.jpg') cv2.arrowedLine(image, (50, 50), (200, 150), (0,0,255), 3, 7, 0, 0.2) cv2.arrowedLine(image, (300, 120), (50, 320), (0,255,255), 3, 7, 0, 0.2) cv2.arrowedLine(image, (50, 200), (500, 400), (255,0,255), 3, 7, 0, 0.05) cv2.imshow("ArrowedLines",image) cv2.waitKey(0) cv2.destroyAllWindows()
Output
On execution, it will produce the following output window −
- Related Articles
- How to draw an arrowed line in OpenCV using Java?
- Draw a line on an image using OpenCV
- How to draw polylines on an image in OpenCV using Python?
- Draw an ellipse on an image using OpenCV
- Draw rectangle on an image using OpenCV
- OpenCV Python – How to find and draw extreme points of an object on an image?
- How to draw markers on an image using Java OpenCV library?
- OpenCV Python – How to detect and draw keypoints in an image using SIFT?
- How to apply Affine Transformation on an image in OpenCV Python?
- How to find and draw Convex Hull of an image contour in OpenCV Python?
- How to read an image in Python OpenCV?
- How to flip an image in OpenCV Python?
- How to mask an image in OpenCV Python?
- How to normalize an image in OpenCV Python?
- How to rotate an image in OpenCV Python?
