# Bluring/Smoothing examples Normalized Box Filter, Gaussian Blur, and # custom Kernel. (also a box filter...) # # Jay Summet 2015 # #Python 2.7, OpenCV 2.4.x # import cv2 import numpy as np #Linux window threading setup code. cv2.startWindowThread() cv2.namedWindow("Original") cv2.namedWindow("Normalized Box 9x9") cv2.namedWindow("our own 9x9 kernel") cv2.namedWindow("GaussianBlur") #Load source / input image as grayscale, also works on color images... imgIn = cv2.imread("oscar.jpg", cv2.IMREAD_GRAYSCALE) cv2.imshow("Original", imgIn) #The blur function does a normalized box filter. nBoxFilter = cv2.blur(imgIn, (9,9) ) cv2.imshow("Normalized Box 9x9", nBoxFilter) #Or, you can create your OWN filter kernel where each value is # 1.0 / 81 (9x9) kernel = np.ones( (9,9), np.float32) kernel = kernel / 81 #try leaving this line out! custom = cv2.filter2D(imgIn, -1, kernel) cv2.imshow("our own 9x9 kernel", custom) #Using the GaussianBlur function...(You could also use getGaussianKernel with the filter2D function...) #Using sigmal of 1.7, try 2.5 and 0.5 instead... gBlurImg = cv2.GaussianBlur(imgIn, (9,9), 1.7) cv2.imshow("GaussianBlur", gBlurImg) cv2.waitKey(0)