1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| from collections.abc import Generator
import cv2 as cv import numpy as np
def bezier(p1: np.ndarray, p2: np.ndarray, p3: np.ndarray) -> Generator[np.ndarray, None, None]: def calc(t): return t * t * p1 + 2 * t * (1 - t) * p2 + (1 - t) * (1 - t) * p3
approx = cv.arcLength(np.array([calc(t)[:2] for t in np.linspace(0, 1, 10)], dtype=np.float32), False) for t in np.linspace(0, 1, round(approx * 1.2)): yield np.round(calc(t)).astype(np.int32)
def generate_scratch(img: np.ndarray, max_length: float, end_brush_range: tuple[float, float], mid_brush_range: tuple[float, float]) -> np.ndarray: H, W = img.shape x, y, rho1, theta1 = np.random.uniform([0] * 4, [W, H, max_length, np.pi * 2]) p1 = np.array([x, y, 0]) p3 = p1 + [rho1 * np.cos(theta1), rho1 * np.sin(theta1), 0]
rho2, theta2 = np.random.uniform([0], [rho1 / 2, np.pi * 2]) p2 = (p1 + p3) / 2 + [rho2 * np.cos(theta2), rho2 * np.sin(theta2), 0]
p1[2], p2[2], p3[2] = np.random.uniform(*np.transpose([end_brush_range, mid_brush_range, end_brush_range]))
for x, y, brush in bezier(p1, p2, p3): cv.circle(img, (x, y), brush, 255, -1) return img
if __name__ == "__main__": W, H = 640, 480
MAX_LENGTH = 100 END_BRUSH_RANGE = (0, 1) MID_BRUSH_RANGE = (2, 5) SCRATCH_CNT = 30
img = np.zeros((H, W), np.uint8) for _ in range(SCRATCH_CNT): generate_scratch(img, MAX_LENGTH, END_BRUSH_RANGE, MID_BRUSH_RANGE)
cv.imshow("img", img) cv.waitKey(0)
|