Opencv4 官方文档 : https://docs.opencv.org/4.2.0/
Opencv4 for Python中文文档点击下载:OpenCV4 for Python 中文文档
demo:
#均值模糊、中值模糊、自定义模糊 模糊是卷积的一种表象
import cv2 as cv
import numpy as np
def blur_demo(image): #均值模糊 去随机噪声有很好的去燥效果
dst = cv.blur(image, (1, 15)) #(1, 15)是垂直方向模糊,(15, 1)水平方向模糊
cv.namedWindow('blur_demo', cv.WINDOW_NORMAL)
cv.imshow("blur_demo", dst)
def median_blur_demo(image): # 中值模糊 对椒盐噪声有很好的去燥效果
dst = cv.medianBlur(image, 5)
cv.namedWindow('median_blur_demo', cv.WINDOW_NORMAL)
cv.imshow("median_blur_demo", dst)
def custom_blur_demo(image): # 用户自定义模糊
kernel = np.ones([5, 5], np.float32)/25 #除以25是防止数值溢出
dst = cv.filter2D(image, -1, kernel)
cv.namedWindow('custom_blur_demo', cv.WINDOW_NORMAL)
cv.imshow("custom_blur_demo", dst)
result:
demo:
#高斯模糊 轮廓还在,保留图像的主要特征 高斯模糊比均值模糊去噪效果好
import cv2 as cv
import numpy as np
def clamp(pv):
if pv > 255:
return 255
if pv < 0:
return 0
else:
return pv
def gaussian_noise(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] #blue
g = image[row, col, 1] #green
r = image[row, col, 2] #red
image[row, col, 0] = clamp(b + s[0])
image[row, col, 1] = clamp(g + s[1])
image[row, col, 2] = clamp(r + s[2])
cv.namedWindow("noise image", cv.WINDOW_NORMAL)
cv.imshow("noise image", image)
dst = cv.GaussianBlur(image, (15, 15), 0) # 高斯模糊
cv.namedWindow("Gaussian", cv.WINDOW_NORMAL)
cv.imshow("Gaussian", dst)
gaussian_noise(src)
dst = cv.GaussianBlur(src, (15,15), 0)
result:
实现方法有高斯双边, 均值迁移,局部均方差
demo:
def bi_demo(image): #双边滤波
dst = cv.bilateralFilter(image, 0, 100, 15)
cv.namedWindow("bi_demo", cv.WINDOW_NORMAL)
cv.imshow("bi_demo", dst)
def shift_demo(image): #均值迁移
dst = cv.pyrMeanShiftFiltering(image, 10, 50)
cv.namedWindow("shift_demo", cv.WINDOW_NORMAL)
cv.imshow("shift_demo", dst)
result:
add demo:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import random
import scipy.misc
import scipy.signal
import scipy.ndimage
from matplotlib.font_manager import FontProperties
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=10)
def medium_filter(im, x, y, step):
sum_s = []
for k in range(-int(step / 2), int(step / 2) + 1):
for m in range(-int(step / 2), int(step / 2) + 1):
sum_s.append(im[x + k][y + m])
sum_s.sort()
return sum_s[(int(step * step / 2) + 1)]
def mean_filter(im, x, y, step):
sum_s = 0
for k in range(-int(step / 2), int(step / 2) + 1):
for m in range(-int(step / 2), int(step / 2) + 1):
sum_s += im[x + k][y + m] / (step * step)
return sum_s
def convert_2d(r):
n = 3
# 3*3 滤波器, 每个系数都是 1/9
window = np.ones((n, n)) / n ** 2
# 使用滤波器卷积图像
# mode = same 表示输出尺寸等于输入尺寸
# boundary 表示采用对称边界条件处理图像边缘
s = scipy.signal.convolve2d(r, window, mode='same', boundary='symm')
return s.astype(np.uint8)
def convert_3d(r):
s_dsplit = []
for d in range(r.shape[2]):
rr = r[:, :, d]
ss = convert_2d(rr)
s_dsplit.append(ss)
s = np.dstack(s_dsplit)
return s
def add_salt_noise(img):
rows, cols, dims = img.shape
R = np.mat(img[:, :, 0])
G = np.mat(img[:, :, 1])
B = np.mat(img[:, :, 2])
Grey_sp = R * 0.299 + G * 0.587 + B * 0.114
Grey_gs = R * 0.299 + G * 0.587 + B * 0.114
snr = 0.9
noise_num = int((1 - snr) * rows * cols)
for i in range(noise_num):
rand_x = random.randint(0, rows - 1)
rand_y = random.randint(0, cols - 1)
if random.randint(0, 1) == 0:
Grey_sp[rand_x, rand_y] = 0
else:
Grey_sp[rand_x, rand_y] = 255
#给图像加入高斯噪声
Grey_gs = Grey_gs + np.random.normal(0, 48, Grey_gs.shape)
Grey_gs = Grey_gs - np.full(Grey_gs.shape, np.min(Grey_gs))
Grey_gs = Grey_gs * 255 / np.max(Grey_gs)
Grey_gs = Grey_gs.astype(np.uint8)
# 中值滤波
Grey_sp_mf = scipy.ndimage.median_filter(Grey_sp, (7, 7))
Grey_gs_mf = scipy.ndimage.median_filter(Grey_gs, (8, 8))
# 均值滤波
Grey_sp_me = convert_2d(Grey_sp)
Grey_gs_me = convert_2d(Grey_gs)
plt.subplot(321)
plt.title('加入椒盐噪声',fontproperties=font_set)
plt.imshow(Grey_sp, cmap='gray')
plt.subplot(322)
plt.title('加入高斯噪声',fontproperties=font_set)
plt.imshow(Grey_gs, cmap='gray')
plt.subplot(323)
plt.title('中值滤波去椒盐噪声(8*8)',fontproperties=font_set)
plt.imshow(Grey_sp_mf, cmap='gray')
plt.subplot(324)
plt.title('中值滤波去高斯噪声(8*8)',fontproperties=font_set)
plt.imshow(Grey_gs_mf, cmap='gray')
plt.subplot(325)
plt.title('均值滤波去椒盐噪声',fontproperties=font_set)
plt.imshow(Grey_sp_me, cmap='gray')
plt.subplot(326)
plt.title('均值滤波去高斯噪声',fontproperties=font_set)
plt.imshow(Grey_gs_me, cmap='gray')
plt.show()
def main():
img = np.array(Image.open('./files/images/testthree.png'))
add_salt_noise(img)
if __name__ == '__main__':
main()
result:
转载请注明转自:https://leejason.blog.csdn.net/article/details/106442435