OpenCV-Python之高斯模糊
阿新 • • 發佈:2018-11-08
1.高斯噪聲函式
//將範圍限制在0~255之間
def thresholdfn(pv):
if pv > 255:
pv = 255
elif pv < 0:
pv = 0
else:
return pv
//定義高斯噪聲函式
def gaussian_demo(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]
g = image[row, col, 1]
r = image[row, col, 2]
b = thresholdfn(b + s[0])
g = thresholdfn(g + s[1])
r = thresholdfn(r + s[2])
cv.imshow('gaussian_demo', image)
2.測試程式
image = cv.imread('./data/lena.jpg' , 1)
cv.imshow('source image', image)
t1 = cv.getTickCount()
gaussian_demo(image)
t2 = cv.getTickCount()
time = (t2 - t1) / cv.getTickFrequency()
print(time)
dst = cv.GaussianBlur(image, (0, 0), 20)
cv.imshow('GaussianBlur image', dst)
cv.waitKey(0)
cv.destroyAllWindows()
測試結果:
time: 7.665542534869055