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

运行结果:

相关推荐
小明.杨28 分钟前
Django 中时区的理解
后端·python·django
邪恶的贝利亚32 分钟前
《ffplay 读线程与解码线程分析:从初始化到 seek 操作,对比视频与音频解码的差异》
ffmpeg·php·音视频
六bring个六35 分钟前
qtcreater配置opencv
c++·qt·opencv·计算机视觉·图形渲染·opengl
中电金信36 分钟前
重构金融数智化产业版图:中电金信“链主”之道
大数据·人工智能
奋斗者1号42 分钟前
Docker 部署 - Crawl4AI 文档 (v0.5.x)
人工智能·爬虫·机器学习
说码解字42 分钟前
ExoPlayer 如何实现音画同步
开发语言·学习·音视频
陈奕昆1 小时前
五、【LLaMA-Factory实战】模型部署与监控:从实验室到生产的全链路实践
开发语言·人工智能·python·llama·大模型微调
多巴胺与内啡肽.1 小时前
OpenCV进阶操作:光流估计
人工智能·opencv·计算机视觉
程序猿小三1 小时前
python uv的了解与使用
开发语言·python·uv
妄想成为master1 小时前
计算机视觉----时域频域在图像中的意义、傅里叶变换在图像中的应用、卷积核的频域解释
人工智能·计算机视觉·傅里叶