Untitled
unknown
plain_text
10 months ago
1.2 kB
15
Indexable
import cv2
cap = cv2.VideoCapture(0)
logo = cv2.imread("Picture2.png")
logo = cv2.resize(logo, (100, 100))
logo_gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
_, mask = cv2.threshold(logo_gray, 1, 255, cv2.THRESH_BINARY)
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Tạo VideoWriter để lưu file
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter("WebcamVid.avi", fourcc, 20.0, (frame_width, frame_height))
while True:
ret, frame = cap.read()
if not ret:
break
h, w = frame.shape[:2]
x = 10
y = h - logo.shape[0] - 10
roi = frame[y:y+logo.shape[0], x:x+logo.shape[1]]
roi_bg = cv2.bitwise_and(roi, roi, mask=cv2.bitwise_not(mask))
roi_fg = cv2.bitwise_and(logo, logo, mask=mask)
dst = cv2.add(roi_bg, roi_fg)
frame[y:y+logo.shape[0], x:x+logo.shape[1]] = dst
out.write(frame)
cv2.imshow("Webcam with TDTU Logo", frame)
# Nhấn 'q' để thoát
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()Editor is loading...
Leave a Comment