How to do it...

  1. First, import all of the modules:
import cv2import numpy as npimport random
  1. Create an image where we're going to draw and randomly generate parameters of the ellipse, such as half axes lengths and rotation angle:
img = np.full((512, 512, 3), 255, np.uint8)axes = (int(256*random.uniform(0, 1)), int(256*random.uniform(0, 1)))angle = int(180*random.uniform(0, 1))center = (256, 256)
  1. Generate points for the ellipse with found parameters, and add random noise to them:
pts = cv2.ellipse2Poly(center, axes, angle, 0, 360, 1)pts += np.random.uniform(-10, 10, pts.shape).astype(np.int32)
  1. Draw out the ellipse and generated points on the image, and display the image:
cv2.ellipse(img, center, axes, angle, 0, 360, (0, 255, 0), 3) ...

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.