How to do it...

You will need to perform the following steps for this recipe:

  1. As done previously, open an image and get its width and height. Also, make a copy of the original image and define a simple function that returns a random point with coordinates inside our image:
import cv2, numpy as np, randomimage = cv2.imread('../data/Lena.png')w, h = image.shape[1], image.shape[0]image_to_show = np.copy(image)def rand_pt(): return (random.randrange(w), random.randrange(h))
  1. Now when the user presses P, L, R, E, or T draw points, lines, rectangles, ellipses, or text, respectively. Also, we will clear an image when the user hits C and closes the application when the Esc key is pushed:
finish = Falsewhile not finish: cv2.imshow("result", image_to_show) ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.