opencv python 圖像去噪

Image Denoising html

OpenCV提供了這種技術的四種變體。dom

  1. cv2.fastNlMeansDenoising() - 使用單個灰度圖像
  2. cv2.fastNlMeansDenoisingColored() - 使用彩色圖像。
  3. cv2.fastNlMeansDenoisingMulti() - 用於在短期內捕獲的圖像序列(灰度圖像)
  4. cv2.fastNlMeansDenoisingColoredMulti() - 與上面相同,但用於彩色圖像。

Common arguments:ide

  • h:參數決定濾波器強度。較高的h值能夠更好地消除噪聲,但也會刪除圖像的細節 (10 is ok)
  • hForColorComponents:與h相同,但僅適用於彩色圖像。 (一般與h相同)
  • templateWindowSize:應該是奇數。 (recommended 7)
  • searchWindowSize:應該是奇數。 (recommended 21)

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

clipboard.png

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

clipboard.png

相關文章
相關標籤/搜索