Life2Coding
How to Draw Arrow Lines in OpenCV Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to draw arrow lines using OpenCV python.

Documentation:

arrowedLine()

img=cv.arrowedLine(img, pt1, pt2, color[, thickness[, line_type[, shift[, tipLength]]]])

Draws a arrow segment pointing from the first point to the second one.

Parameters
img Image.
pt1 The point the arrow starts from.
pt2 The point the arrow points to.
color Line color.
thickness Line thickness.
line_type Type of the line. See LineTypes
shift Number of fractional bits in the point coordinates.
tipLength The length of the arrow tip in relation to the arrow length

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

waitKey()

retval=cv.waitKey([, delay])

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means “forever”.

destroyAllWindows()

None=cv.destroyAllWindows()

Destroys all of the HighGUI windows.

Steps:

  • Create a 512×512 black image using np.zeros()
  • Define some colors for the arrow lines
  • Draw the arrow lines using cv2.arrowedLine()
  • Display the image using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

import numpy as np
import cv2

if __name__ == '__main__':

    #create a 512x512 black image
    image=np.zeros((512,512,3),np.uint8)

    # Dictionary containing some colors
    colors = {'blue': (255, 0, 0), 'green': (0, 255, 0), 'red': (0, 0, 255), 'yellow': (0, 255, 255), 'magenta': (255, 0, 255), 'cyan': (255, 255, 0), 'white': (255, 255, 255), 'black': (0, 0, 0), 'gray': (125, 125, 125), 'rand': np.random.randint(0, high=256, size=(3,)).tolist(), 'dark_gray': (50, 50, 50), 'light_gray': (220, 220, 220)}

    cv2.arrowedLine(image, (50, 50), (200, 50), colors['red'], 3, 8, 0, 0.1)
    cv2.arrowedLine(image, (50, 120), (200, 120), colors['green'], 3, cv2.LINE_AA, 0, 0.3)
    cv2.arrowedLine(image, (50, 200), (200, 200), colors['blue'], 3, 8, 0, 0.3)

    cv2.imshow("ArrowedLine",image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Output:

5dbf86cc64201 How to Draw Arrow Lines in OpenCV Python
life2coding_icon [] How to Draw Arrow Lines in OpenCV Python
Latest posts by Life2Coding (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.