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

运行结果:

相关推荐
乾巫宇宙国监察特使7 分钟前
Python的设计模式
python·测试
Hockor15 分钟前
写给前端的 Python 教程四(列表/元组)
前端·后端·python
Se7en25816 分钟前
Prefix Caching 详解:实现 KV Cache 的跨请求高效复用
人工智能
山顶听风22 分钟前
多层感知器MLP实现非线性分类(原理)
人工智能·分类·数据挖掘
佛喜酱的AI实践22 分钟前
5分钟入门Google ADK -- 从零构建你的第一个AI Agent
人工智能
用户387754343356324 分钟前
Midjourney Imagine API 申请及使用
人工智能·后端
这里有鱼汤24 分钟前
熟练掌握MACD这8种形态,让你少走三年弯路(附Python量化代码)| 建议收藏
后端·python
山顶听风25 分钟前
MLP实战二:MLP 实现图像数字多分类
人工智能·机器学习·分类
mengyoufengyu32 分钟前
DeepSeek12-Open WebUI 知识库配置详细步骤
人工智能·大模型·deepseek
404.Not Found33 分钟前
Day46 Python打卡训练营
开发语言·python