python OpenCV学习笔记(八):模糊操作

文章目录

  • 1.均值模糊
  • 2. 中值模糊(去除椒盐噪声)
  • 3.自定义模糊
  • 4.高斯模糊

1.均值模糊

import cv2 as cv
import numpy as np

def blur_demo(image):
	dst = cv.blur(image, (1, 3))#,前后为xy轴模糊的程度
	cv.imshow("blur_demo", dst)

src = cv.imread("D:/.......")
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
blur_demo(src)
cv.waitKey(0)

cv.destroyALLWindows()

2. 中值模糊(去除椒盐噪声)

import cv2 as cv
import numpy as np
def median_blur_demo(image):
	dst = cv.medianBlur(image, 5)#5代表模糊的程度
	cv.imshow("median_blur_demo", dst)
src = cv.imread("D:/.......")
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
median_blur_demo(src)
cv.waitKey(0)

cv.destroyALLWindows()

3.自定义模糊

 import cv2 as cv
 import numpy as np 
 
 def custom_blur_demo(image):  
 	#kernel = np.ones([5,5],np.float32)/25 均值
 	#kernel = np.ones([[0, -1, 0],[-1, 5, -1],[0, -1, 0]], np.float32) 锐化 加和等于1或0
 	dst = cv.filter2D(image,-1,kernel=kernel)  
 	cv.imshow("median_blur_demo", dst) 
 	
 src = cv.imread("D:/.......") 
 cv.namedWindow("input image",cv.WINDOW_AUTOSIZE) 
 cv.imshow("input image", src) 
 custom_blur_demo(src) 
 cv.waitKey(0) 
 
 cv.destroyALLWindows()

4.高斯模糊

dst = cv.GaussianBlur(src, (5,5), 0)
cv.imshow("Gaussian Blur",dst)

区分好椒盐噪声与高斯噪声,椒盐噪声为黑白像素点,高斯噪声为不规律的像素点。

你可能感兴趣的:(语法基础,Python)