OpenCV 图像退化与增强

退化

滤波

img_averaging=cv2.blur(img2,(3,3)) #均值滤波
img_median = cv2.medianBlur(img2,3) #中值滤波

高斯模糊

result = cv2.GaussianBlur(source, (11,11), 0)

高斯噪声

def add_noise_Guass(img, mean=0, var=0.01):  # 添加高斯噪声
    img = np.array(img / 255, dtype=float)# 将原始图像的像素值进行归一化,除以255使得像素值在0-1之间
    noise = np.random.normal(mean, var ** 0.5, img.shape)
    #0.01的0.5次幂,ctrl点击normal函数可见参数
    #给出均值为loc,标准差为scale的高斯随机数(场)
    '''
    numpy.random.normal(loc=0.0, scale=1.0, size=None)
    loc:float
    此概率分布的均值(对应着整个分布的中心centre)
    scale:float
    此概率分布的标准差(对应于分布的宽度,scale越大越矮胖,scale越小,越瘦高)
    size:int or tuple of ints
    输出的shape,默认为None,只输出一个值
    '''
    out_img = img + noise# 将噪声和原始图像进行相加得到加噪后的图像
    if out_img.min() < 0:
        low_clip = -1
    else:
        low_clip = 0
        out_img = np.clip(out_img, low_clip, 1.0)#clip函数将元素的大小限制在了low_clip和1之间了,小于的用low_clip代替,大于1的用1代替
        out_img = np.uint8(out_img * 255)# 解除归一化,乘以255将加噪后的图像的像素值恢复
    return out_img

椒盐噪声

def sp_noise(image, amount):

    output = image.copy()
    threshold = 1 - amount#传入的参数,设置一个阙值
    #amount 越大,白色越多
    for i in range(image.shape[0]):#shape[0]表示图片高
        for j in range(image.shape[1]):#图片宽
            rdm = random.random()#取0到1之间的浮点数
            if rdm < amount: #如果随机数小于参数,那么像素点取黑色
                output[i][j] = 0  #亮度0%,取黑色
            elif rdm > threshold:
                output[i][j] = 255#取白色

    return output

增强

修复破损

import cv2
import numpy as np
# 读取照片
image = cv2.imread('old_photo.jpg')
# 将图像转换为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 应用高斯模糊来减少图像噪声
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# 使用Canny边缘检测器识别边缘
edges = cv2.Canny(blurred, 50, 150)
# 使用膨胀操作将边缘连接在一起形成轮廓
dilated = cv2.dilate(edges, None, iterations=2)
# 在原始图像上绘制轮廓,以便于可视化结果
result = cv2.inpaint(image, dilated, (3, 3), cv2.INPAINT_TELEA)
# 显示修复后的照片
cv2.imshow('Result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()

先读取照片,并将其转换为灰度图。然后,我们使用高斯模糊减少图像噪声,并使用Canny边缘检测器识别边缘。接下来,我们通过膨胀操作将边缘连接在一起形成轮廓,以便识别损坏区域。最后,我们使用OpenCV的inpaint函数来修复损坏区域,并将结果显示出来。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AICVer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值