-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapegen.py
53 lines (40 loc) · 1.16 KB
/
shapegen.py
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
50
51
52
53
import cv2
import numpy as np
# Video dimensions
width = 640
height = 480
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('moving_rectangle.avi', fourcc, 20.0, (width, height))
# Initial position of the rectangle
x = 0
y = 0
# Speed and direction of the rectangle
speed_x = 5
speed_y = 2
# Size of the rectangle
rect_width = 50
rect_height = 30
while True:
# Create a white background
frame = np.full((height, width, 3), 255, dtype=np.uint8)
# Draw a black rectangle
cv2.rectangle(frame, (x, y), (x + rect_width, y + rect_height), (0, 0, 0), -1)
# Write the frame
out.write(frame)
# Display the frame
cv2.imshow('Video', frame)
# Move the rectangle
x += speed_x
y += speed_y
# Change direction if the rectangle reaches the boundaries
if x <= 0 or x + rect_width >= width:
speed_x = -speed_x
if y <= 0 or y + rect_height >= height:
speed_y = -speed_y
# Break the loop when 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release everything when done
out.release()
cv2.destroyAllWindows()