Image Denoising html
OpenCV提供了這種技術的四種變體。dom
Common arguments:ide
cv2.fastNlMeansDenoisingColored()
如上所述,它用於從彩色圖像中去除噪聲。 (噪音預計是高斯噪音)ui
import numpy as np import cv2 import matplotlib.pyplot as plt img = cv2.imread('img.jpg') dst = cv2.fastNlMeansDenoisingColored(img,None,10,10,7,21) plt.subplot(121),plt.imshow(img) plt.subplot(122),plt.imshow(dst) plt.show()
cv2.fastNlMeansDenoisingMulti()
this
如今咱們將相同的方法應用於視頻。 第一個參數是嘈雜幀的列表。 第二個參數imgToDenoiseIndex指定咱們須要去噪的幀,由於咱們在輸入列表中傳遞了frame的索引。 第三個是temporalWindowSize,它指定了用於去噪的附近幀的數量。 在這種狀況下,使用總共temporalWindowSize幀,其中中心幀是要去噪的幀。 例如,傳遞了5個幀的列表做爲輸入。 設imgToDenoiseIndex = 2和temporalWindowSize = 3.而後使用frame-1,frame-2和frame-3對幀-2進行去噪spa
import numpy as np import cv2 import matplotlib.pyplot as plt cap = cv2.VideoCapture('test.mp4') # create a list of first 5 frames img = [cap.read()[1] for i in range(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()