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

运行结果:

相关推荐
子燕若水1 小时前
Unreal Engine 5中的AI知识
人工智能
极限实验室2 小时前
Coco AI 实战(一):Coco Server Linux 平台部署
人工智能
杨过过儿2 小时前
【学习笔记】4.1 什么是 LLM
人工智能
巴伦是只猫2 小时前
【机器学习笔记Ⅰ】13 正则化代价函数
人工智能·笔记·机器学习
大千AI助手3 小时前
DTW模版匹配:弹性对齐的时间序列相似度度量算法
人工智能·算法·机器学习·数据挖掘·模版匹配·dtw模版匹配
AI生存日记3 小时前
百度文心大模型 4.5 系列全面开源 英特尔同步支持端侧部署
人工智能·百度·开源·open ai大模型
LCG元3 小时前
自动驾驶感知模块的多模态数据融合:时序同步与空间对齐的框架解析
人工智能·机器学习·自动驾驶
why技术3 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
彭祥.4 小时前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类