# 目标# • 学习使用非局部平均值去噪算法去除图像中的噪声# • 学习函数 cv2.fastNlMeansDenoising() cv2.fastNlMeansDenoisingColored() 等# 噪声是平均值为1的随机变量# 对于彩色图像 先转换到 CIELAB 颜色空间 然后对 L 和 AB 成分 分别去噪# OpenCV 中的图像去噪# 1. cv2.fastNlMeansDenoising() 使用对象为灰度图。# 2. cv2.fastNlMeansDenoisingColored() 使用对象为彩色图。# 3. cv2.fastNlMeansDenoisingMulti() 用于短时 的图像序列 灰度图像# 4. cv2.fastNlMeansDenoisingColoredMulti() 用于短时 的图 像序列 彩色图像import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('cats.png')
img = cv2.cvtColor(img, code=cv2.COLOR_BGR2RGB)
dst = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21)
dst2=cv2.cvtColor(dst,code=cv2.COLOR_BGR2RGB)
plt.subplot(121), plt.imshow(img)
plt.subplot(122), plt.imshow(dst)
plt.subplot(122), plt.imshow(dst2)
plt.show()import numpy as np
import cv2
from matplotlib import pyplot as plt
cap = cv2.VideoCapture('../data/vtest.avi')# create a list of first 5 frames
img =[cap.read()[1]for i inrange(5)]# convert all to grayscale
gray =[cv2.cvtColor(i, cv2.COLOR_BGR2GRAY)for i in img]# convert all to float64
gray =[np.float64(i)for i in gray]# create a noise of variance 25
noise = np.random.randn(*gray[1].shape)*10# Add this noise to images
noisy =[i + noise for i in gray]# Convert back to uint8
noisy =[np.uint8(np.clip(i,0,255))for i in noisy]# Denoise 3rd frame considering all the 5 frames
dst = cv2.fastNlMeansDenoisingMulti(noisy,2,5,None,4,7,35)
plt.subplot(131), plt.imshow(gray[2],'gray')
plt.subplot(132), plt.imshow(noisy[2],'gray')
plt.subplot(133), plt.imshow(dst,'gray')
plt.show()