- First, import all of the modules:
import cv2import numpy as npimport random
- 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)
- 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)
- 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) ...