How to Draw an Arrowed Line on an Image in OpenCV Python
OpenCV is an open-source computer vision and machine learning library that is used extensively in image and video processing. It has a vast collection of algorithms that can be used for various computer vision tasks like object detection, face detection, and so on. In this article, we will focus on how to draw an arrowed line on an image in OpenCV Python.
Prerequisites
Before we start with the implementation of our code, we need to install two important packages: OpenCV and NumPy. You can install these packages using the following command:
!pip install opencv-python numpy
Steps to Draw an Arrowed Line on an Image in OpenCV Python
- Import Libraries
The first step is to import the necessary libraries. We will be using NumPy and OpenCV libraries in our code.
import cv2
import numpy as np
- Read Image
We will read an image on which we want to draw an arrowed line. The cv2.imread()
function is used to read an image.
img = cv2.imread("image.jpg")
- Draw an Arrowed Line
We will be using the cv2.arrowedLine()
function to draw an arrowed line on the image. This function takes the following arguments:
cv2.arrowedLine(image, startPoint, endPoint, color, thickness, line_type, shift, tipLength)
image
: Image on which the arrowed line will be drawnstartPoint
: Starting point of the arrowed line. The format is(x, y)
endPoint
: Ending point of the arrowed line. The format is(x, y)
color
: Color of the line. The format is(B, G, R)
thickness
: Thickness of the lineline_type
: Type of the lineshift
: Number of fractional bits in the point coordinatestipLength
: Length of the arrow tip in relation to the arrow length. The default value is 0.1.
Here’s an example code that will draw an arrowed line on the image.
# Set starting and ending points
start_point = (100, 100)
end_point = (350, 350)
# Set arrow tip length
tip_length = 0.2
# Set color and thickness of the line
color = (255, 0, 0)
thickness = 2
# Draw an arrowed line on the image
cv2.arrowedLine(img, start_point, end_point, color, thickness, tipLength=tip_length)
# Display the image
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Conclusion
In conclusion, drawing an arrowed line on an image in OpenCV Python is a simple process. We can use the cv2.arrowedLine()
function to draw an arrowed line on the image. We need to provide the starting and ending points of the line, the color and thickness of the line, and the length of the arrow tip. By using this function, we can easily annotate or mark important points on an image.