《Opencv》小练习--处理带有椒盐噪声的视频
代码如下:
import cv2
import numpy as np
# 定义添加椒盐噪声的函数
def add_peppersalt_noise(image,n):
result = image.copy()
h,w = image.shape[:2]
for i in range(n):
x = np.random.randint(1,h)
y = np.random.randint(1,w)
if np.random.randint(0,2)==0:
result[x,y]=0
else:
result[x,y]=255
return result
Video = cv2.VideoCapture(r'./images/test.avi')
if not Video.isOpened():
print('视频无法打开!')
while True:
ret,frame = Video.read()
if not ret:
break
frame_copy = frame.copy()
# 对每一帧图像添加椒盐噪声
v_noise = add_peppersalt_noise(frame_copy,10000)
# 使用中值滤波对带有椒盐噪声的图像进行处理
v_median = cv2.medianBlur(v_noise,3)
cv2.imshow('test',frame)
cv2.imshow('v_noise',v_noise)
cv2.imshow('v_median',v_median)
if cv2.waitKey(1) == 27:
break
Video.release()
cv2.destroyAllWindows()
结果如下:
原文地址:https://blog.csdn.net/qq_61600833/article/details/144702525
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!