1.使用cv2将png图片作为蒙版进行应用,特别优雅的进行修改img[imgMask==255] = [128, 128, 128]
import cv2
img = cv2.imread('ori.png',cv2.IMREAD_COLOR)
imgMask = cv2.imread('1.png',cv2.IMREAD_GRAYSCALE)
img[imgMask==255] = [128, 128, 128]
res = img
cv2.imshow('maskimg',res)
cv2.waitKey(0)
2.对于视频来说,也是类似操作
import cv2
cap = cv2.VideoCapture('../videos/1.mp4')
imgMask = cv2.imread('1.png',cv2.IMREAD_GRAYSCALE)
# 检查视频是否成功打开
if not cap.isOpened():
print("无法打开视频文件")
exit()
while cap.isOpened():
ret,img = cap.read()
img[imgMask==255] = [128, 128, 128]
cv2.imshow('maskimg',img)
cv2.waitKey(1)
cap.release()