一些美顏軟件、美顏相機上的磨皮和毛玻璃特效基本上都是用的高斯模糊娘纷,并且大部分圖像處理軟件中都有高斯模糊的操作惜索,除此之外氛悬,高斯模糊還具有減少圖像層次和深度的功能
GaussianBlur():定義:GaussianBlur(src, ksize, sigmaX, dst=None, sigmaY=None, borderType=None)
給圖片創(chuàng)建毛玻璃特效
第二個參數(shù):高斯核的寬和高(建議是奇數(shù))
第三個參數(shù):x和y軸的標準差
代碼
import cv2 as cv
import numpy
def clamp(pv):
"""防止顏色值超出顏色取值范圍(0-255)"""
if pv > 255:
return 255
if pv < 0:
return 0
return pv
def gaussian_noise(image):
"""高斯噪聲"""
height, width, channel = image.shape
for row in range(height):
for col in range(width):
for c in range(channel):
s = numpy.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("./data/lena.jpg", cv.IMREAD_COLOR)
cv.namedWindow("lena", cv.WINDOW_AUTOSIZE)
cv.imshow("lena", src)
# 給圖片創(chuàng)建毛玻璃特效
# 第二個參數(shù):高斯核的寬和高(建議是奇數(shù))
# 第三個參數(shù):x和y軸的標準差
dst = cv.GaussianBlur(src, (5, 5), 0)
cv.imshow("GaussianBlur", dst)
# start = cv.getTickCount()
# gaussian_noise(src)
# end = cv.getTickCount()
# time = (end - start) / cv.getTickFrequency()
# print("耗時 = ", time * 1000, "ms")
cv.waitKey(0)
cv.destroyAllWindows()
numpy.random.normal(loc=0.0, scale=1.0, size=None)
loc:float 此概率分布的均值(對應著整個分布的中心centre)
scale:float 此概率分布的標準差(對應于分布的寬度,scale越大越矮胖粗悯,scale越小虚循,越瘦高)
size:int or tuple of ints 輸出的shape,默認為None样傍,只輸出一個值