python+opencv實現高斯平滑濾波
阿新 • • 發佈:2019-02-02
功能:
建立兩個滑動條來分別控制高斯核的size和
注意:由於
程式碼:
# -*- coding: utf-8 -*-
import cv2
#兩個回撥函式
def GaussianBlurSize(GaussianBlur_size):
global KSIZE
KSIZE = GaussianBlur_size * 2 +3
print KSIZE, SIGMA
dst = cv2.GaussianBlur(scr, (KSIZE,KSIZE), SIGMA, KSIZE)
cv2.imshow(window_name,dst)
def GaussianBlurSigma(GaussianBlur_sigma):
global SIGMA
SIGMA = GaussianBlur_sigma/10.0
print KSIZE, SIGMA
dst = cv2.GaussianBlur(scr, (KSIZE,KSIZE), SIGMA, KSIZE)
cv2.imshow(window_name,dst)
#全域性變數
GaussianBlur_size = 1
GaussianBlur_sigma = 15
KSIZE = 1
SIGMA = 15
max_value = 300
max_type = 6
window_name = "GaussianBlurS Demo"
trackbar_size = "Size*2+3"
trackbar_sigema = "Sigma/10"
#讀入圖片,模式為灰度圖,建立視窗
scr = cv2.imread("G:\homework\lena.bmp",0)
cv2.namedWindow(window_name)
#建立滑動條
cv2.createTrackbar( trackbar_size, window_name, \
GaussianBlur_size, max_type, GaussianBlurSize )
cv2.createTrackbar( trackbar_sigema, window_name, \
GaussianBlur_sigma, max_value, GaussianBlurSigma )
#初始化
GaussianBlurSize(1)
GaussianBlurSigma(15)
if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()
呼叫:
需要把圖片和cv2.pyd與GaussianBlur.py放在同一資料夾下
>>> import os
>>> os.chdir("g:\homework")
>>> import GaussianBlur
5 15
5 1.5
5 1.6
5 1.9
5 2.4
5 2.5
5 2.9
5 3.0
5 3.3
5 3.6
5 3.9
5 4.1
5 4.2
5 4.3
5 4.4
5 4.5
5 4.6
5 4.7
5 4.8