本文主要是介绍逐帧P视频水印,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.实现功能:
(1)读取本地视频
(2)逐帧鼠标左键框选水印部分,覆盖为白色
(3)按按键‘y’,'Y'使用上一次位置的框覆盖此次的水印
(4)按其他按键表示不处理当前帧
2.调库
import cv2
import numpy as np
import easygui
import copy
3.完整源码
import cv2
import numpy as np
import easygui
import copyglobal 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, point2img = 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:breakif 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 = Trueelse:key = Falsecap.release()
cv2.destroyAllWindows()
4.未完成
保存视频
5.参考文章
(1条消息) opencv进阶学习笔记6:使用鼠标在图像上绘制矩形框或者多边形框_总裁余(余登武)的博客-CSDN博客_opencv鼠标绘制多边形https://blog.csdn.net/kobeyu652453/article/details/107309456EasyGUI 学习文档【超详细中文版】(from小甲鱼 ) - 廖海清 - 博客园 (cnblogs.com)
https://www.cnblogs.com/hale547/p/13301951.html
这篇关于逐帧P视频水印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!