import cv2
import ffmpeg
import cv2
import numpy
import numpy as np
import glob
video_f = 'D:/CCTV/CCTV.mp4' # 视频文件名
output_f = 'D:/CCTV/logoafter.mp4' # 输出视频文件名
video = cv2.VideoCapture(video_f)
# 获取视频宽度
frame_width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))#480
# 获取视频高度
frame_height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))#852
start_x, start_y, w, h = 740, 420, 100, 28 # logo位置
logo_f = 'shrink.png'
logo = cv2.imdecode(np.fromfile(logo_f, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
alpha = logo[..., 3] #取出索引号为3的通道
alpha[alpha>0]=1 #透明度不为0的地方,置为1
alpha=np.expand_dims(alpha, axis=-1) #广播机制,扩充维度,axis=0 [1,28,100], axis=1 [28,1,100] , axis=2 [28,100,1] , axis=-1 [28,100,1]
alpha=np.expand_dims(alpha, axis=0) #(1, 28, 100, 1)
logo = cv2.imread(logo_f)
logo = cv2.resize(logo, (w, h))
logo=np.expand_dims(logo,axis=0)
print("logo:",logo.shape)
fps = video.get(cv2.CAP_PROP_FPS) #视频帧速率
sum_fps=video.get(cv2.CAP_PROP_FRAME_COUNT) #总帧数
print("总帧数:",sum_fps)#1214
ret, frame = video.read()
img_h, img_w = frame.shape[:2]
batch_size=300
listframes = []
idx=0
def save_to_list(batch_size,idx,listframes):
while True:
ret, frame = video.read() #读出来是一帧一帧的
idx += 1
if frame is None:
break
if idx >0 and idx <=batch_size:
listframes.append(frame)
if idx==300:
new_Matrix = numpy.stack(listframes)
print(new_Matrix.shape) # (300, 480, 852, 3)
frame_mask = np.ones_like(new_Matrix) # 返回一个用1填充的形状和类型一样的数组,全黑。构造了一个与视频帧形状大小一样的4维数组
frame_mask[:, start_y:start_y + h, start_x:start_x + w, :] = 1 - alpha # 将放置logo区域全部弄成0
new_Matrix *= frame_mask # 保留 非logo区域的原值 , 空出logo大小的矩形框区域给logo
new_Matrix[:, start_y:start_y + h, start_x:start_x + w, :] += logo * alpha
idx = 0
listframes = []
for i in range(300):
cv2.imshow('f', new_Matrix[i, :, :, :])
cv2.waitKey(1)
save_to_list(batch_size,idx,listframes)
04-19
5147
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-01
9731
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-10
4379
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交