import random
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
def fft():
"""
傅里叶变化
:return:
"""
img = cv.imread('orig.png', 0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.log(np.abs(fshift))
f = 20 * np.log(np.abs(f))
cv.imwrite("magnitude_spectrum.png", magnitude_spectrum)
cv.imwrite("f.png", f)
def xiangwei():
"""
相位
:return:
"""
img = cv.imread('orig.png', 0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude_spectrum = 20 * np.angle(fshift)
f = 20 * np.angle(f)
cv.imwrite("xiangwei1.png", magnitude_spectrum)
cv.imwrite("xiangwei2.png", f)
def ifft():
"""
逆傅里叶
:return:
"""
img = cv.imread('orig.png', 0)
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
f1shift = np.fft.ifftshift(fshift)
img_back = np.fft.ifft2(f1shift)
img_back = np.abs(img_back)
cv.imwrite("img3_1back.png", img_back)
def gaotonglvbo():
"""
高通滤波
:return:
"""
img_man = cv.imread('3_1.png', 0)
rows, cols = img_man.shape
h = int(rows / 2)
v = int(cols / 2)
mask = np.ones(img_man.shape, np.uint8)
mask[h - 30:h + 30,v - 30:v + 30] = 0
f1 = np.fft.fft2(img_man)
f1shift = np.fft.fftshift(f1)
f1shift = f1shift * mask
f2shift = np.fft.ifftshift(f1shift)
img_new = np.fft.ifft2(f2shift)
img_new = np.abs(img_new)
cv.imwrite("gaotonglvbo.png", img_new)
def ditonglvbo():
"""
低通滤波
:return:
"""
img_man = cv.imread('3_1.png', 0)
rows, cols = img_man.shape
h = int(rows / 2)
v = int(cols / 2)
mask = np.zeros(img_man.shape, np.uint8)
mask[h - 30:h + 30,v - 30:v + 30] = 1
f1 = np.fft.fft2(img_man)
f1shift = np.fft.fftshift(f1)
f1shift = f1shift * mask
f2shift = np.fft.ifftshift(f1shift)
img_new = np.fft.ifft2(f2shift)
img_new = np.abs(img_new)
cv.imwrite("ditonglvbo.png", img_new)
def index_low_pass_kernel(img,D0,n=1):
"""
指数滤波
:return:
"""
r,c = img.shape[1],img.shape[0]
u = np.arange(r)
v = np.arange(c)
u, v = np.meshgrid(u, v)
low_pass = np.sqrt((u - r / 2) ** 2 + (v - c / 2) ** 2)
low_pass = np.exp(-1 * (D0 / low_pass) ** n)
return low_pass
def index_high_pass_filter(D0=50,n=1):
"""
指数滤波
:param D0:
:param n:
:return:
"""
src = cv.imread('3_1.png', 0)
kernel = 1 - index_low_pass_kernel(src, D0, n)
gray = np.float64(src)
gray_fft = np.fft.fft2(gray)
gray_fftshift = np.fft.fftshift(gray_fft)
dst_filtered = kernel * gray_fftshift
dst_ifftshift = np.fft.ifftshift(dst_filtered)
dst_ifft = np.fft.ifft2(dst_ifftshift)
dst = np.abs(np.real(dst_ifft))
dst = np.clip(dst, 0, 255)
cv.imwrite("kernel.png", kernel * 255)
cv.imwrite("index_high_pass_filter.png", dst)
def batewo_low_pass_kernel(img,D0,n=1):
"""
巴特沃
:param img:
:param D0:
:param n:
:return:
"""
r,c = img.shape[1],img.shape[0]
u = np.arange(r)
v = np.arange(c)
u, v = np.meshgrid(u, v)
low_pass = np.sqrt((u - r / 2) ** 2 + (v - c / 2) ** 2)
low_pass = 1 / (1 + (D0 / low_pass) ** (2 * n))
return low_pass
def btwindex_high_pass_filter(D0=50,n=1):
"""
巴特沃
:param D0:
:param n:
:return:
"""
src = cv.imread('3_1.png', 0)
kernel = 1 - batewo_low_pass_kernel(src, D0, n)
gray = np.float64(src)
gray_fft = np.fft.fft2(gray)
gray_fftshift = np.fft.fftshift(gray_fft)
dst_filtered = kernel * gray_fftshift
dst_ifftshift = np.fft.ifftshift(dst_filtered)
dst_ifft = np.fft.ifft2(dst_ifftshift)
dst = np.abs(np.real(dst_ifft))
dst = np.clip(dst, 0, 255)
cv.imwrite("btwkernel.png", kernel * 255)
cv.imwrite("btwindex_high_pass_filter.png", dst)
def dct():
"""
离散余弦
:return:
"""
img = cv.imread('1.png')
img = img[:, :, 0]
img = np.float32(img)
img_dct = cv.dct(img)
img_recor2 = cv.idct(img_dct)
cv.imwrite("dct.png", img_recor2)