OpenCV-Python模糊處理
阿新 • • 發佈:2018-11-07
- 均值模糊
- 中值模糊
- 自定義模糊
均值模糊
import cv2 as cv
import numpy as np
# 均值模糊
def blur_demo(img):
dst = cv.blur(img, (5, 5))
cv.imshow("blur image", dst)
中值模糊
import cv2 as cv
import numpy as np
# 中值模糊
def median_blur_demo(img):
dst = cv.medianBlur(img, 5)
cv.imshow("median_blur_demo" , dst)
對比均值模糊和中值模糊
image = cv.imread('./data/ai.png', 1)
cv.imshow('source image', image)
median_blur_demo(image)
blur_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()
中值濾波處理椒鹽噪聲效果顯著!
自定義模糊
注意:要做防止溢位的處理
def custom_blur_demo(img):
kernel = np.ones([5, 5], np.float32)/25
dst = cv.filter2D( img, -1, kernel=kernel)
cv.imshow("custom_blur_demo", dst)
自定義銳化操作
def custom_blur_demo(img):
#kernel = np.ones([5, 5], np.float32)/25
kernel = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]], np.float32)
dst = cv.filter2D(img, -1, kernel= kernel)
cv.imshow("custom_blur_demo", dst)
演示demo:
image = cv.imread('./data/lena.jpg', 1)
cv.imshow('source image', image)
custom_blur_demo(image)
cv.waitKey(0)
cv.destroyAllWindows()
原圖:
自定義1:
自定義2: