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 −

Updated on: 27-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements