python opencv 灰度图非局部平均去噪

python opencv 灰度图非局部平均去噪

代码:

import cv2
import numpy as np
# 灰度图像去噪
def MeansDenoising(img,h,templateWindowSize,searchWindowSize):
    dst = cv2.fastNlMeansDenoising(img,h,templateWindowSize,searchWindowSize)
    return dst
# 回调函数,因为只能传一个参数,不方便,所以pass
def nothing(pos):
    pass
#读取图片
img = cv2.imread("2.jpg",0)
# 创建老窗口
cv2.namedWindow('OldImg')
# 绑定老窗口和滑动条(滑动条的数值)
cv2.createTrackbar('h', 'OldImg', 10, 100, nothing)
cv2.createTrackbar('templateWindowSize', 'OldImg', 7, 100, nothing)
cv2.createTrackbar('searchWindowSize', 'OldImg', 21, 100, nothing)
while True:
    # 提取滑动条的数值d
    h = cv2.getTrackbarPos('h', 'OldImg')
    templateWindowSize =cv2.getTrackbarPos('templateWindowSize', 'OldImg')
    searchWindowSize = cv2.getTrackbarPos('searchWindowSize', 'OldImg')
    # 滑动条数字传入函数img_dilated中,并且调用函数img_dilated
    dilated = MeansDenoising(img,h,templateWindowSize,searchWindowSize)
    # 绑定 img 和 dilated
    result = np.hstack([img,dilated])
    cv2.imshow('OldImg', result)
    # 设置推出键
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
# 关闭窗口
cv2.destroyAllWindows()

效果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8D5Yl7m2-1570706125957)(C:\Users\xiahuadong\Pictures\博客\40.png)]

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Python中的OpenCV库来实现对灰度图像的加高斯噪声和去噪操作,同时使用skimage库来计算图像对比度。 首先,加载图像并将其转换为灰度图像: ```python import cv2 import numpy as np from skimage.measure import compare_ssim # 加载图像并转换为灰度图像 img = cv2.imread('lena.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) ``` 接下来,可以使用OpenCV的`randn`函数来生成高斯噪声,并添加到灰度图像中: ```python # 生成高斯噪声 mean = 0 var = 10 sigma = var ** 0.5 noise = np.random.randn(gray.shape[0], gray.shape[1]) * sigma + mean noise = noise.reshape(gray.shape) # 添加高斯噪声 noisy = gray + noise.astype(np.uint8) ``` 这里使用了均值为0,方差为10的高斯噪声,并将其添加到灰度图像中,得到了添加噪声后的图像`noisy`。 接下来,可以使用OpenCV的`fastNlMeansDenoising`函数来对加噪图像进行去噪处理: ```python # 去噪 denoised = cv2.fastNlMeansDenoising(noisy, None, 10, 7, 21) ``` 这里使用了快速局部均值去噪(fastNlMeansDenoising),其中第二个参数为输出图像(为None时,函数会自动创建一个与输入图像大小和类型相同的输出图像)。第三个参数是滤波器强度,第四个参数是相邻像素之间的距离(越大表示平滑的区域越大),第五个参数是搜索窗口的大小。 最后,可以使用skimage库中的`compare_ssim`函数来计算去噪前后的图像对比度(结构相似性指数): ```python # 计算对比度 score1 = compare_ssim(gray, noisy) score2 = compare_ssim(gray, denoised) print('加噪前对比度:', score1) print('去噪后对比度:', score2) ``` 这里使用了结构相似性指数(SSIM)来评估图像质量,该指数的取值范围为[-1, 1],越接近1表示两张图像越相似,越接近-1表示两张图像越不相似。`compare_ssim`函数的第一个参数是原图像,第二个参数是要比较的图像。 完整代码如下: ```python import cv2 import numpy as np from skimage.measure import compare_ssim # 加载图像并转换为灰度图像 img = cv2.imread('lena.png') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 生成高斯噪声 mean = 0 var = 10 sigma = var ** 0.5 noise = np.random.randn(gray.shape[0], gray.shape[1]) * sigma + mean noise = noise.reshape(gray.shape) # 添加高斯噪声 noisy = gray + noise.astype(np.uint8) # 去噪 denoised = cv2.fastNlMeansDenoising(noisy, None, 10, 7, 21) # 计算对比度 score1 = compare_ssim(gray, noisy) score2 = compare_ssim(gray, denoised) print('加噪前对比度:', score1) print('去噪后对比度:', score2) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夏华东的博客

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

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

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

打赏作者

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

抵扣说明:

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

余额充值