1. Realize the function:
(1) Read local video
(2) Select the watermark part frame by frame with the left mouse button and cover it with white
(3) Press the button 'y', 'Y' to cover the current watermark with the frame of the previous position
(4) Press other keys to indicate that the current frame is not processed
2. Transfer library
import cv2
import numpy as np
import easygui
import copy
3. Complete source code
import cv2
import numpy as np
import easygui
import copy
global frame, point1, point2, key
key = False
point1 = point2 = (0,0)
videopath = easygui.fileopenbox()
cap = cv2.VideoCapture(videopath)
if not cap.isOpened():
easygui.msgbox("video read failure")
exit(0)
video_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
video_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
cv2.namedWindow('result')
def Rectangular_box(event, x, y, flags, param):
global frame, point1, point2
img = frame.copy()
if event == cv2.EVENT_LBUTTONDOWN:
point1 = (x, y)
cv2.circle(img, point1, 10, (0, 255, 0), 5)
cv2.imshow('result', img)
elif event == cv2.EVENT_MOUSEMOVE and (flags & cv2.EVENT_FLAG_LBUTTON):
cv2.rectangle(img, point1, (x, y), (255, 0, 0), 5)
cv2.imshow('result', img)
elif event == cv2.EVENT_LBUTTONUP:
point2 = (x, y)
cv2.rectangle(img, point1, point2, (0, 255, 255), 4)
mask = np.zeros([video_height,video_width,3],np.uint8)
max_h = point1[1] if point1[1]>point2[1] else point2[1]
min_h = point1[1] if point1[1]<point2[1] else point2[1]
max_w = point1[0] if point1[0]>point2[0] else point2[0]
min_w = point1[0] if point1[0]<point2[0] else point2[0]
for i in range(min_h,max_h):
for j in range(min_w,max_w):
mask[i][j] = [255,255,255]
img = cv2.add(frame, mask)
cv2.imshow('result', img)
cv2.setMouseCallback('result', Rectangular_box)
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if key and point1 != (0,0) and point2 != (0,0):
mask = np.zeros([video_height,video_width,3],np.uint8)
max_h = point1[1] if point1[1]>point2[1] else point2[1]
min_h = point1[1] if point1[1]<point2[1] else point2[1]
max_w = point1[0] if point1[0]>point2[0] else point2[0]
min_w = point1[0] if point1[0]<point2[0] else point2[0]
for i in range(min_h,max_h):
for j in range(min_w,max_w):
mask[i][j] = [255,255,255]
frame = cv2.add(frame, mask)
cv2.imshow('result', frame)
k = cv2.waitKey(0)
if k == ord('y') or k == ord('Y'):
key = True
else:
key = False
cap.release()
cv2.destroyAllWindows()
4. Unfinished
save video
5. Reference articles
(1 message) opencv advanced study notes 6: use the mouse to draw a rectangular frame or a polygonal frame on the image /article/details/107309456 EasyGUI learning documents [super detailed Chinese version] (from small turtle) - Liao Haiqing - Blog Park (cnblogs.com) https://www.cnblogs.com/hale547/p/13301951.html