Opencv实现图片和视频的加噪、平滑处理

图片和视频的加噪、平滑处理

目录

图片加噪


还需要导入numpy库

定义的是椒盐噪声

代码展示:

python 复制代码
def nosie_img(img,n=10000):
    img = img.copy()
    # 取shape的前两个值,即图片大的高宽
    h,w = img.shape[:2]
    for  i in range(n):
    	# 随机生成像素值x,y对应的是h,w
        x = np.random.randint(1,h)
        y = np.random.randint(1,w)
        # 随机生成0,1,如果为0该点为黑点,否则为白点,黑白概率相同
        if np.random.randint(0,2) == 0:
            img[x,y] = 0
        else:
            img[x, y] = 255
	#返回图像
    return img

a = cv2.imread('at1.png')
a_noise = nosie_img(a)
cv2.imshow('a',a)
cv2.waitKey(0)
cv2.imshow('a_noise',a_noise)
cv2.waitKey(0)

运行结果:

图片平滑处理


均值滤波

  • 概念:以像素点为中心的周围的n*n个像素值的均值代替当前像素值,n尽量为奇数,边界自动扩展。
  • 格式:cv2.blur(a,(n,n))
    a:图片变量
    (n,n):均值范围

代码展示:

python 复制代码
def nosie_img(img,n=10000):
    img = img.copy()
    h,w = img.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:
            img[x,y] = 0
        else:
            img[x, y] = 255

    return img

a = cv2.imread('at1.png')
a_noise = nosie_img(a)
a_noise_blur_3 = cv2.blur(a_noise,(3,3))
cv2.imshow('a',a)
cv2.waitKey(0)
cv2.imshow('a_noise',a_noise)
cv2.waitKey(0)
cv2.imshow('a_noise_blur_3',a_noise_blur_3)
cv2.waitKey(0)

运行结果:

方框滤波

  • 概念:以像素点为中心的周围的nn个像素,normalize True时与均值滤波相同,normalize Fales时,nn个像素值的和大于255时为255,边界自动扩展。
  • 格式:cv2.boxFilter(a,(n,n),normalize=True/Fales)

代码展示:

python 复制代码
def nosie_img(img,n=10000):
    img = img.copy()
    h,w = img.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:
            img[x,y] = 0
        else:
            img[x, y] = 255

    return img
a = cv2.imread('at1.png')
a_noise = nosie_img(a)
cv2.imshow('a',a)
cv2.waitKey(0)
cv2.imshow('a_noise',a_noise)
cv2.waitKey(0)
#方框滤波
a_noise_boxfilter_T = cv2.boxFilter(a_noise,-1,(3,3),normalize=True)
a_noise_boxfilter_F = cv2.boxFilter(a_noise,-1,(3,3),normalize=False)
cv2.imshow('a_noise_boxfilter_T',a_noise_boxfilter_T)
cv2.waitKey(0)
cv2.imshow('a_noise_boxfilter_F',a_noise_boxfilter_F)
cv2.waitKey(0)

运行结果:

高斯滤波

  • 概念:以像素点为中心的周围的n*n个像素,加权运算得到最终像素值,边界自动扩展。
  • 格式:cv2.GaussianBlur(a,(n,n),1) 1表示正态分布,确定权重分布

代码展示:

python 复制代码
def nosie_img(img,n=10000):
    img = img.copy()
    h,w = img.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:
            img[x,y] = 0
        else:
            img[x, y] = 255

    return img

a = cv2.imread('at1.png')
a_noise = nosie_img(a)
cv2.imshow('a',a)
cv2.waitKey(0)
cv2.imshow('a_noise',a_noise)
cv2.waitKey(0)
#高斯滤波
a_noise_gauss = cv2.GaussianBlur(a_noise,(3,3),1)
cv2.imshow('a_noise_gauss',a_noise_gauss)
cv2.waitKey(0)

运行结果:

中值滤波

  • 概念:以像素点为中心的周围的n*n个像素,从小到大排序,取中值为像素值,边界自动扩展。
  • 格式:cv2.medianBlur(a,n)
    n:表示取中值范围,必须为奇数

代码展示:

python 复制代码
def nosie_img(img,n=10000):
    img = img.copy()
    h,w = img.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:
            img[x,y] = 0
        else:
            img[x, y] = 255

    return img

a = cv2.imread('at1.png')
a_noise = nosie_img(a)
cv2.imshow('a',a)
cv2.waitKey(0)
cv2.imshow('a_noise',a_noise)
cv2.waitKey(0)
#中值滤波
a_noise_median = cv2.medianBlur(a,3)
cv2.imshow('a_noise_median',a_noise_median)
cv2.waitKey(0)
cv2.destroyAllWindows()

运行结果:

视频平滑处理

代码展示:

python 复制代码
# 加噪处理
def nosie_img(img,n=10000):
    img = img.copy()
    h,w = img.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:
            img[x,y] = 0
        else:
            img[x, y] = 255

    return img

#视频处理
#读取视频
v = cv2.VideoCapture('test_v.mp4')
# 打不开退出
if not v.isOpened():
    print("无法打开视频")
    exit()
 #打开读取
while True:
    r, f = v.read()
    #视频加噪
    f_noise = nosie_img(f)
	#视频中值滤波处理
    f_noise_median = cv2.medianBlur(f_noise,3)
    if not r:
        break
    cv2.imshow('video',f)
    cv2.imshow('video_f_noise', f_noise)
    cv2.imshow('video_f_noise_median', f_noise_median)
    #点击空格键,退出播放
    if cv2.waitKey(10) == 32:
        break
        
v.release()
cv2.destroyAllWindows()

运行结果:

相关推荐
databook11 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室12 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
飞哥数智坊13 小时前
GPT-5-Codex 发布,Codex 正在取代 Claude
人工智能·ai编程
倔强青铜三13 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
虫无涯14 小时前
Dify Agent + AntV 实战:从 0 到 1 打造数据可视化解决方案
人工智能
Dm_dotnet16 小时前
公益站Agent Router注册送200刀额度竟然是真的
人工智能
算家计算17 小时前
7B参数拿下30个世界第一!Hunyuan-MT-7B本地部署教程:腾讯混元开源业界首个翻译集成模型
人工智能·开源
用户25191624271117 小时前
Python之语言特点
python
机器之心17 小时前
LLM开源2.0大洗牌:60个出局,39个上桌,AI Coding疯魔,TensorFlow已死
人工智能·openai
刘立军17 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql