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()

运行结果:

相关推荐
MicrosoftReactor35 分钟前
技术速递|构建你的第一个 MCP 服务器:如何使用自定义功能扩展 AI 工具
运维·服务器·人工智能
minhuan2 小时前
构建AI智能体:二十、妙笔生花:Gradio集成DashScope Qwen-Image模型实现文生图
人工智能·prompt·qwen·gradio·千问大模型
siliconstorm.ai5 小时前
OpenAI重组受阻:微软“锁链”与生态博弈
人工智能
Fabarta技术团队6 小时前
Fabarta个人专属智能体赋能媒体:从过载信息到深度可控的创作体系
人工智能·智能体
动能小子ohhh7 小时前
django的URL路由配置常用方式
后端·python·django
电气铺二表姐137744166157 小时前
【WIFI电表】物联网无线通讯光伏储能三相单相智能电表
运维·人工智能·能源
胶带码农7 小时前
机器学习-时序预测2
人工智能·机器学习
心动啊1217 小时前
神经网络2——使用Keras实现MLP
人工智能·深度学习·神经网络
AI 嗯啦8 小时前
Python 爬虫案例:爬取豆瓣电影 Top250 数据
开发语言·爬虫·python
stjiejieto8 小时前
教育 AI 的下半场:个性化学习路径生成背后,技术如何平衡效率与教育本质?
大数据·人工智能·学习