数字图像处理 高通滤波

import numpy as np
import cv2

def convert_2d(r):
    r_ext = np.zeros((r.shape[0] * 2, r.shape[1] * 2))
    for i in range(r.shape[0]):
        for j in range(r.shape[1]):
            r_ext[i][j] = r[i][j]
    # 傅里叶变换
    r_ext_fu = np.fft.fft2(r_ext)
    # 将低频信号移至中间,等效于在时域上对f(x,y)乘以(-1)^(m+n)
    r_ext_fu = np.fft.fftshift(r_ext_fu)

    # 截止频率
    d0 = 20
    n = 2
    # 频率域中心坐标
    center = (r_ext_fu.shape[0] // 2, r_ext_fu.shape[1] // 2)
    h = np.empty(r_ext_fu.shape)
    # 绘制滤波器
    for u in range(h.shape[0]):
        for v in range(h.shape[1]):
            duv = ((u - center[0]) ** 2 + (v - center[1]) ** 2) ** 0.5
            if duv == 0:
                h[u][v] = 0
            else:
                h[u][v] = 1/((1+(d0/duv))**(2*n))

    s_ext_fu = r_ext_fu * h
    s_ext = np.fft.ifft2(np.fft.ifftshift(s_ext_fu))
    s_ext = np.abs(s_ext)
    s = s_ext[0:r.shape[0], 0:r.shape[1]]

    for i in range(s.shape[0]):
        for j in range(s.shape[1]):
            s[i][j] = min(max(s[i][j], 0), 255)
    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('ditong', im_converted_mat)
cv2.imwrite('ditong4.jpg', im_converted_mat)
cv2.waitKey()

数字图像处理 高通滤波_第1张图片
数字图像处理 高通滤波_第2张图片

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