How to do it...

  1. Open an image and get its width and height. Also, define a simple function that returns a random point inside our image:
import cv2, randomimage = cv2.imread('../data/Lena.png')w, h = image.shape[1], image.shape[0]def rand_pt(mult=1.):    return (random.randrange(int(w*mult)),            random.randrange(int(h*mult)))
  1. Let's draw something! Let's go for circles:
cv2.circle(image, rand_pt(), 40, (255, 0, 0))cv2.circle(image, rand_pt(), 5, (255, 0, 0), cv2.FILLED)cv2.circle(image, rand_pt(), 40, (255, 85, 85), 2)cv2.circle(image, rand_pt(), 40, (255, 170, 170), 2, cv2.LINE_AA)
  1. Now let's try to draw lines:
cv2.line(image, rand_pt(), rand_pt(), (0, 255, 0))cv2.line(image, rand_pt(), rand_pt(), (85, 255, 85), 3)cv2.line(image, rand_pt(), ...

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.