数字图像处理 均值滤波

import numpy as np
import cv2
import scipy.signal

def convert_2d(r):
    n = 3
    kernel = np.ones((n, n)) / n ** 2
    s = scipy.signal.convolve2d(r,kernel,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

im = cv2.imread('tetet.jpg')
im_converted_mat = convert_3d(im)
cv2.imshow('original',im)
cv2.imshow('mean',im_converted_mat)
cv2.imwrite('mean.jpg',im_converted_mat)
cv2.waitKey()

你可能感兴趣的:(图像处理)