1. 程式人生 > >opencv筆記之--高斯模糊處理

opencv筆記之--高斯模糊處理

高斯噪點
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import cv2 as cv
import numpy as np


def clamp(pv):
    if pv > 255:
        return 255
    if pv < 0:
        return 0
    else:
        return pv

def gaussian_noise(image):
    h, w, c = image.shape
    #對每一行每一列每一個畫素點三個通道加上一個隨機數的值就產生了一個隨機高斯的隨機噪聲的圖片
    for row in range(h):
        for col in range(w):
            s = np.random.normal(0, 20, 3)
            b = image[row, col, 0] # blue
            g = image[row, col, 1] # green
            r = image[row, col, 2] # red
            image[row, col, 0] = clamp(b + s[0])
            image[row, col, 1] = clamp(g + s[1])
            image[row, col, 2] = clamp(r + s[2])
    cv.imshow("noise image", image)

src = cv.imread("F:\miao.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
gaussian_noise(src)
cv.waitKey(0)
cv.destroyAllWindows()

執行結果,如圖:


高斯模糊處理

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
import cv2 as cv
import numpy as np


def clamp(pv):
    if pv > 255:
        return 255
    if pv < 0:
        return 0
    else:
        return pv

src = cv.imread("F:\miao.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)

t1 = cv.getTickCount()
t2 = cv.getTickCount()
time = (t2 - t1)/cv.getTickFrequency()
print("time consume : %s"%(time*1000))
dst = cv.GaussianBlur(src, (0, 0), 15)
cv.imshow("Gaussian Blur", dst)
cv.waitKey(0)
cv.destroyAllWindows()

執行結果,如下圖: