parent
6c1bb98f70
commit
4dc8b82e85
@ -1,336 +1,326 @@
|
|||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from tkinter import filedialog, messagebox
|
from tkinter import filedialog, messagebox
|
||||||
from tkinter import Toplevel
|
from tkinter import Toplevel
|
||||||
from PIL import Image, ImageTk
|
from PIL import Image, ImageTk
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
import cv2
|
||||||
import os
|
import os
|
||||||
|
|
||||||
img_path = "" # 全局变量,用于存储图像路径
|
img_path = "" # 全局变量,用于存储图像路径
|
||||||
src = None # 全局变量,用于存储已选择的图像
|
src = None # 全局变量,用于存储已选择的图像
|
||||||
img_label = None # 全局变量,用于存储显示选择的图片的标签
|
img_label = None # 全局变量,用于存储显示选择的图片的标签
|
||||||
edge = None
|
edge = None
|
||||||
|
|
||||||
FreqsmoWin = None
|
FreqsmoWin = None
|
||||||
AirsmoWin = None
|
AirsmoWin = None
|
||||||
|
|
||||||
def select_image(root):
|
def select_image(root):
|
||||||
global img_path, src, img_label, edge
|
global img_path, src, img_label, edge
|
||||||
|
|
||||||
img_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.png;*.jpeg;*.bmp")])
|
img_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.png;*.jpeg;*.bmp")])
|
||||||
if img_path:
|
if img_path:
|
||||||
# 确保路径中的反斜杠正确处理,并使用 UTF-8 编码处理中文路径
|
# 确保路径中的反斜杠正确处理,并使用 UTF-8 编码处理中文路径
|
||||||
img_path_fixed = os.path.normpath(img_path)
|
img_path_fixed = os.path.normpath(img_path)
|
||||||
|
|
||||||
# 图像输入
|
# 图像输入
|
||||||
src_temp = cv2.imdecode(np.fromfile(img_path_fixed, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
|
src_temp = cv2.imdecode(np.fromfile(img_path_fixed, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
|
||||||
if src_temp is None:
|
if src_temp is None:
|
||||||
messagebox.showerror("错误", "无法读取图片,请选择有效的图片路径")
|
messagebox.showerror("错误", "无法读取图片,请选择有效的图片路径")
|
||||||
return
|
return
|
||||||
src = cv2.cvtColor(src_temp, cv2.COLOR_BGR2RGB)
|
src = cv2.cvtColor(src_temp, cv2.COLOR_BGR2RGB)
|
||||||
|
|
||||||
# 检查 img_label 是否存在且有效
|
# 检查 img_label 是否存在且有效
|
||||||
if img_label is None or not img_label.winfo_exists():
|
if img_label is None or not img_label.winfo_exists():
|
||||||
img_label = tk.Label(root)
|
img_label = tk.Label(root)
|
||||||
img_label.pack(side=tk.TOP, pady=10)
|
img_label.pack(side=tk.TOP, pady=10)
|
||||||
|
|
||||||
img = Image.open(img_path)
|
img = Image.open(img_path)
|
||||||
img.thumbnail((160, 160))
|
img.thumbnail((160, 160))
|
||||||
img_tk = ImageTk.PhotoImage(img)
|
img_tk = ImageTk.PhotoImage(img)
|
||||||
img_label.configure(image=img_tk)
|
img_label.configure(image=img_tk)
|
||||||
img_label.image = img_tk
|
img_label.image = img_tk
|
||||||
|
|
||||||
# 定义 edge 变量为 PIL.Image 对象,以便稍后保存
|
# 定义 edge 变量为 PIL.Image 对象,以便稍后保存
|
||||||
edge = Image.fromarray(src)
|
edge = Image.fromarray(src)
|
||||||
else:
|
else:
|
||||||
messagebox.showerror("错误", "没有选择图片路径")
|
messagebox.showerror("错误", "没有选择图片路径")
|
||||||
|
|
||||||
def show_selected_image(root):
|
def changeSize(event, img, LabelPic):
|
||||||
global img_label
|
img_aspect = img.shape[1] / img.shape[0]
|
||||||
img_label = tk.Label(root)
|
new_aspect = event.width / event.height
|
||||||
img_label.pack(side=tk.TOP, pady=10)
|
|
||||||
img = Image.open(img_path)
|
if new_aspect > img_aspect:
|
||||||
img.thumbnail((160, 160))
|
new_width = int(event.height * img_aspect)
|
||||||
img_tk = ImageTk.PhotoImage(img)
|
new_height = event.height
|
||||||
img_label.configure(image=img_tk)
|
else:
|
||||||
img_label.image = img_tk
|
new_width = event.width
|
||||||
|
new_height = int(event.width / img_aspect)
|
||||||
def changeSize(event, img, LabelPic):
|
|
||||||
img_aspect = img.shape[1] / img.shape[0]
|
resized_image = cv2.resize(img, (new_width, new_height))
|
||||||
new_aspect = event.width / event.height
|
image1 = ImageTk.PhotoImage(Image.fromarray(resized_image))
|
||||||
|
LabelPic.image = image1
|
||||||
if new_aspect > img_aspect:
|
LabelPic['image'] = image1
|
||||||
new_width = int(event.height * img_aspect)
|
|
||||||
new_height = event.height
|
def savefile():
|
||||||
else:
|
global edge
|
||||||
new_width = event.width
|
|
||||||
new_height = int(event.width / img_aspect)
|
filename = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg"), ("PNG files", "*.png"), ("BMP files", "*.bmp")])
|
||||||
|
if not filename:
|
||||||
resized_image = cv2.resize(img, (new_width, new_height))
|
return
|
||||||
image1 = ImageTk.PhotoImage(Image.fromarray(resized_image))
|
# 确保 edge 变量已定义
|
||||||
LabelPic.image = image1
|
if edge is not None:
|
||||||
LabelPic['image'] = image1
|
try:
|
||||||
|
edge.save(filename)
|
||||||
def savefile():
|
messagebox.showinfo("保存成功", "图片保存成功!")
|
||||||
global edge
|
except Exception as e:
|
||||||
|
messagebox.showerror("保存失败", f"无法保存图片: {e}")
|
||||||
filename = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg"), ("PNG files", "*.png"), ("BMP files", "*.bmp")])
|
else:
|
||||||
if not filename:
|
messagebox.showerror("保存失败", "没有图像可保存")
|
||||||
return
|
|
||||||
# 确保 edge 变量已定义
|
#频域平滑
|
||||||
if edge is not None:
|
def freq_smo(root):
|
||||||
try:
|
global src, FreqsmoWin, edge
|
||||||
edge.save(filename)
|
|
||||||
messagebox.showinfo("保存成功", "图片保存成功!")
|
# 判断是否已经选取图片
|
||||||
except Exception as e:
|
if src is None:
|
||||||
messagebox.showerror("保存失败", f"无法保存图片: {e}")
|
messagebox.showerror("错误", "没有选择图片!")
|
||||||
else:
|
return
|
||||||
messagebox.showerror("保存失败", "没有图像可保存")
|
|
||||||
|
# 理想低通滤波器
|
||||||
#频域平滑
|
def Ideal_LowPassFilter(rows, cols, crow, ccol, D0=20):
|
||||||
def freq_smo(root):
|
# 创建一个与输入图像大小相同的空白图像
|
||||||
global src, FreqsmoWin, edge
|
Ideal_LowPass = np.zeros((rows, cols), dtype=np.uint8)
|
||||||
|
# 创建理想低通滤波器
|
||||||
# 判断是否已经选取图片
|
for i in range(rows):
|
||||||
if src is None:
|
for j in range(cols):
|
||||||
messagebox.showerror("错误", "没有选择图片!")
|
x = i - crow
|
||||||
return
|
y = j - ccol
|
||||||
|
D = np.sqrt(x**2 + y**2)
|
||||||
# 理想低通滤波器
|
if D <= D0:
|
||||||
def Ideal_LowPassFilter(rows, cols, crow, ccol, D0=20):
|
Ideal_LowPass[i, j] = 255
|
||||||
# 创建一个与输入图像大小相同的空白图像
|
# 应用滤波器到频域表示
|
||||||
Ideal_LowPass = np.zeros((rows, cols), dtype=np.uint8)
|
mask = Ideal_LowPass[:, :, np.newaxis]
|
||||||
# 创建理想低通滤波器
|
fshift = dft_shift * mask
|
||||||
for i in range(rows):
|
# 逆傅里叶变换以获得平滑后的图像
|
||||||
for j in range(cols):
|
f_ishift = np.fft.ifftshift(fshift)
|
||||||
x = i - crow
|
img_back = cv2.idft(f_ishift)
|
||||||
y = j - ccol
|
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
|
||||||
D = np.sqrt(x**2 + y**2)
|
# 归一化图像到0-255
|
||||||
if D <= D0:
|
cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
|
||||||
Ideal_LowPass[i, j] = 255
|
img_back = np.uint8(img_back)
|
||||||
# 应用滤波器到频域表示
|
|
||||||
mask = Ideal_LowPass[:, :, np.newaxis]
|
return img_back
|
||||||
fshift = dft_shift * mask
|
|
||||||
# 逆傅里叶变换以获得平滑后的图像
|
# 布特沃斯低通滤波器
|
||||||
f_ishift = np.fft.ifftshift(fshift)
|
def ButterWorth_LowPassFilter(rows, cols, crow, ccol, D0=20, n=2):
|
||||||
img_back = cv2.idft(f_ishift)
|
# 创建一个与输入图像大小相同的空白图像
|
||||||
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
|
ButterWorth_LowPass = np.zeros((rows, cols), dtype=np.uint8)
|
||||||
# 归一化图像到0-255
|
# 创建巴特沃斯低通滤波器
|
||||||
cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
|
for i in range(rows):
|
||||||
img_back = np.uint8(img_back)
|
for j in range(cols):
|
||||||
|
x = i - crow
|
||||||
return img_back
|
y = j - ccol
|
||||||
|
D = np.sqrt(x ** 2 + y ** 2)
|
||||||
# 布特沃斯低通滤波器
|
ButterWorth_LowPass[i, j] = 255 / (1 + (D / D0) ** (2 * n))
|
||||||
def ButterWorth_LowPassFilter(rows, cols, crow, ccol, D0=20, n=2):
|
# 应用滤波器到频域表示
|
||||||
# 创建一个与输入图像大小相同的空白图像
|
mask = ButterWorth_LowPass[:, :, np.newaxis]
|
||||||
ButterWorth_LowPass = np.zeros((rows, cols), dtype=np.uint8)
|
fshift = dft_shift * mask
|
||||||
# 创建巴特沃斯低通滤波器
|
# 逆傅里叶变换以获得平滑后的图像
|
||||||
for i in range(rows):
|
f_ishift = np.fft.ifftshift(fshift)
|
||||||
for j in range(cols):
|
img_back = cv2.idft(f_ishift)
|
||||||
x = i - crow
|
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
|
||||||
y = j - ccol
|
# 归一化图像到0-255
|
||||||
D = np.sqrt(x ** 2 + y ** 2)
|
cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
|
||||||
ButterWorth_LowPass[i, j] = 255 / (1 + (D / D0) ** (2 * n))
|
img_back = np.uint8(img_back)
|
||||||
# 应用滤波器到频域表示
|
|
||||||
mask = ButterWorth_LowPass[:, :, np.newaxis]
|
return img_back
|
||||||
fshift = dft_shift * mask
|
|
||||||
# 逆傅里叶变换以获得平滑后的图像
|
# 高斯低通滤波器
|
||||||
f_ishift = np.fft.ifftshift(fshift)
|
def Gauss_LowPassFilter(rows, cols, crow, ccol, D0=20):
|
||||||
img_back = cv2.idft(f_ishift)
|
# 创建一个与输入图像大小相同的空白图像
|
||||||
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
|
Gauss_LowPass = np.zeros((rows, cols), dtype=np.uint8)
|
||||||
# 归一化图像到0-255
|
# 创建高斯低通滤波器
|
||||||
cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
|
for i in range(rows):
|
||||||
img_back = np.uint8(img_back)
|
for j in range(cols):
|
||||||
|
x = i - crow
|
||||||
return img_back
|
y = j - ccol
|
||||||
|
D = np.sqrt(x ** 2 + y ** 2)
|
||||||
# 高斯低通滤波器
|
Gauss_LowPass[i, j] = 255 * np.exp(-0.5 * (D ** 2) / (D0 ** 2))
|
||||||
def Gauss_LowPassFilter(rows, cols, crow, ccol, D0=20):
|
# 应用滤波器到频域表示
|
||||||
# 创建一个与输入图像大小相同的空白图像
|
mask = Gauss_LowPass[:, :, np.newaxis]
|
||||||
Gauss_LowPass = np.zeros((rows, cols), dtype=np.uint8)
|
fshift = dft_shift * mask
|
||||||
# 创建高斯低通滤波器
|
# 逆傅里叶变换以获得平滑后的图像
|
||||||
for i in range(rows):
|
f_ishift = np.fft.ifftshift(fshift)
|
||||||
for j in range(cols):
|
img_back = cv2.idft(f_ishift)
|
||||||
x = i - crow
|
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
|
||||||
y = j - ccol
|
# 归一化图像到0-255
|
||||||
D = np.sqrt(x ** 2 + y ** 2)
|
cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
|
||||||
Gauss_LowPass[i, j] = 255 * np.exp(-0.5 * (D ** 2) / (D0 ** 2))
|
img_back = np.uint8(img_back)
|
||||||
# 应用滤波器到频域表示
|
|
||||||
mask = Gauss_LowPass[:, :, np.newaxis]
|
return img_back
|
||||||
fshift = dft_shift * mask
|
|
||||||
# 逆傅里叶变换以获得平滑后的图像
|
# 读取灰度图像
|
||||||
f_ishift = np.fft.ifftshift(fshift)
|
im = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||||
img_back = cv2.idft(f_ishift)
|
|
||||||
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
|
# 获取图像的频域表示
|
||||||
# 归一化图像到0-255
|
dft = cv2.dft(np.float32(im), flags=cv2.DFT_COMPLEX_OUTPUT)
|
||||||
cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
|
dft_shift = np.fft.fftshift(dft)
|
||||||
img_back = np.uint8(img_back)
|
|
||||||
|
# 获取图像的尺寸
|
||||||
return img_back
|
rows, cols = im.shape
|
||||||
|
crow, ccol = rows // 2, cols // 2
|
||||||
# 读取灰度图像
|
|
||||||
im = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
# 理想低通滤波器
|
||||||
|
Ideal_LowPass = Ideal_LowPassFilter(rows, cols, crow, ccol)
|
||||||
# 获取图像的频域表示
|
# 巴特沃斯低通滤波器
|
||||||
dft = cv2.dft(np.float32(im), flags=cv2.DFT_COMPLEX_OUTPUT)
|
ButterWorth_LowPass = ButterWorth_LowPassFilter(rows, cols, crow, ccol)
|
||||||
dft_shift = np.fft.fftshift(dft)
|
# 高斯低通滤波器
|
||||||
|
Gauss_LowPass = Gauss_LowPassFilter(rows, cols, crow, ccol)
|
||||||
# 获取图像的尺寸
|
|
||||||
rows, cols = im.shape
|
combined = np.hstack((Ideal_LowPass, ButterWorth_LowPass, Gauss_LowPass))
|
||||||
crow, ccol = rows // 2, cols // 2
|
# 更新 edge 变量
|
||||||
|
edge = Image.fromarray(combined)
|
||||||
# 理想低通滤波器
|
|
||||||
Ideal_LowPass = Ideal_LowPassFilter(rows, cols, crow, ccol)
|
# 创建Toplevel窗口
|
||||||
# 巴特沃斯低通滤波器
|
try:
|
||||||
ButterWorth_LowPass = ButterWorth_LowPassFilter(rows, cols, crow, ccol)
|
FreqsmoWin.destroy()
|
||||||
# 高斯低通滤波器
|
except Exception as e:
|
||||||
Gauss_LowPass = Gauss_LowPassFilter(rows, cols, crow, ccol)
|
print("NVM")
|
||||||
|
finally:
|
||||||
combined = np.hstack((Ideal_LowPass, ButterWorth_LowPass, Gauss_LowPass))
|
FreqsmoWin = Toplevel()
|
||||||
# 更新 edge 变量
|
FreqsmoWin.attributes('-topmost', True)
|
||||||
edge = Image.fromarray(combined)
|
FreqsmoWin.geometry("720x300")
|
||||||
|
FreqsmoWin.resizable(True, True) # 可缩放
|
||||||
# 创建Toplevel窗口
|
FreqsmoWin.title("频域平滑结果")
|
||||||
try:
|
|
||||||
FreqsmoWin.destroy()
|
# 显示图像
|
||||||
except Exception as e:
|
LabelPic = tk.Label(FreqsmoWin, text="IMG", width=720, height=240)
|
||||||
print("NVM")
|
image = ImageTk.PhotoImage(Image.fromarray(combined))
|
||||||
finally:
|
LabelPic.image = image
|
||||||
FreqsmoWin = Toplevel()
|
LabelPic['image'] = image
|
||||||
FreqsmoWin.attributes('-topmost', True)
|
|
||||||
FreqsmoWin.geometry("720x300")
|
LabelPic.bind('<Configure>', lambda event: changeSize(event, combined, LabelPic))
|
||||||
FreqsmoWin.resizable(True, True) # 可缩放
|
LabelPic.pack(fill=tk.BOTH, expand=tk.YES)
|
||||||
FreqsmoWin.title("频域平滑结果")
|
|
||||||
|
# 添加保存按钮
|
||||||
# 显示图像
|
btn_save = tk.Button(FreqsmoWin, text="保存", bg='#add8e6', fg='black', font=('Helvetica', 14), width=20,
|
||||||
LabelPic = tk.Label(FreqsmoWin, text="IMG", width=720, height=240)
|
command=savefile)
|
||||||
image = ImageTk.PhotoImage(Image.fromarray(combined))
|
btn_save.pack(pady=10)
|
||||||
LabelPic.image = image
|
|
||||||
LabelPic['image'] = image
|
return
|
||||||
|
|
||||||
LabelPic.bind('<Configure>', lambda event: changeSize(event, combined, LabelPic))
|
#空域平滑
|
||||||
LabelPic.pack(fill=tk.BOTH, expand=tk.YES)
|
def air_smo(root):
|
||||||
|
global src, AirsmoWin, edge
|
||||||
# 添加保存按钮
|
|
||||||
btn_save = tk.Button(FreqsmoWin, text="保存", bg='#add8e6', fg='black', font=('Helvetica', 14), width=20,
|
# 判断是否已经选取图片
|
||||||
command=savefile)
|
if src is None:
|
||||||
btn_save.pack(pady=10)
|
messagebox.showerror("错误", "没有选择图片!")
|
||||||
|
return
|
||||||
return
|
|
||||||
|
# 均值平滑滤波
|
||||||
#空域平滑
|
def mean_filter(image, height, width):
|
||||||
def air_smo(root):
|
# 创建空白图像以存储滤波结果
|
||||||
global src, AirsmoWin, edge
|
filtered_image = np.zeros((height - 2, width - 2), dtype=np.uint8)
|
||||||
|
|
||||||
# 判断是否已经选取图片
|
# 执行3x3均值滤波
|
||||||
if src is None:
|
for i in range(1, height - 1):
|
||||||
messagebox.showerror("错误", "没有选择图片!")
|
for j in range(1, width - 1):
|
||||||
return
|
tmp = (int(image[i - 1, j - 1]) + int(image[i - 1, j]) + int(image[i - 1, j + 1]) +
|
||||||
|
int(image[i, j - 1]) + int(image[i, j]) + int(image[i, j + 1]) +
|
||||||
# 均值平滑滤波
|
int(image[i + 1, j - 1]) + int(image[i + 1, j]) + int(image[i + 1, j + 1])) // 9
|
||||||
def mean_filter(image, height, width):
|
filtered_image[i - 1, j - 1] = tmp
|
||||||
# 创建空白图像以存储滤波结果
|
|
||||||
filtered_image = np.zeros((height - 2, width - 2), dtype=np.uint8)
|
return filtered_image
|
||||||
|
|
||||||
# 执行3x3均值滤波
|
# 中值平滑滤波
|
||||||
for i in range(1, height - 1):
|
def median_filter(image, height, width):
|
||||||
for j in range(1, width - 1):
|
# 创建空白图像以存储滤波结果
|
||||||
tmp = (int(image[i - 1, j - 1]) + int(image[i - 1, j]) + int(image[i - 1, j + 1]) +
|
filtered_image = np.zeros((height - 2, width - 2), dtype=np.uint8)
|
||||||
int(image[i, j - 1]) + int(image[i, j]) + int(image[i, j + 1]) +
|
|
||||||
int(image[i + 1, j - 1]) + int(image[i + 1, j]) + int(image[i + 1, j + 1])) // 9
|
# 执行3x3中值滤波
|
||||||
filtered_image[i - 1, j - 1] = tmp
|
for i in range(1, height - 1):
|
||||||
|
for j in range(1, width - 1):
|
||||||
return filtered_image
|
# 取3x3邻域
|
||||||
|
region = [
|
||||||
# 中值平滑滤波
|
image[i - 1, j - 1], image[i - 1, j], image[i - 1, j + 1],
|
||||||
def median_filter(image, height, width):
|
image[i, j - 1], image[i, j], image[i, j + 1],
|
||||||
# 创建空白图像以存储滤波结果
|
image[i + 1, j - 1], image[i + 1, j], image[i + 1, j + 1]
|
||||||
filtered_image = np.zeros((height - 2, width - 2), dtype=np.uint8)
|
]
|
||||||
|
# 计算中值
|
||||||
# 执行3x3中值滤波
|
filtered_image[i - 1, j - 1] = np.median(region)
|
||||||
for i in range(1, height - 1):
|
|
||||||
for j in range(1, width - 1):
|
return filtered_image
|
||||||
# 取3x3邻域
|
|
||||||
region = [
|
# 5x5 中值平滑滤波
|
||||||
image[i - 1, j - 1], image[i - 1, j], image[i - 1, j + 1],
|
def med_filter_5x5(image, height, width):
|
||||||
image[i, j - 1], image[i, j], image[i, j + 1],
|
# 创建空白图像以存储滤波结果
|
||||||
image[i + 1, j - 1], image[i + 1, j], image[i + 1, j + 1]
|
filtered_image = np.zeros((height - 4, width - 4), dtype=np.uint8)
|
||||||
]
|
|
||||||
# 计算中值
|
# 执行5x5中值滤波
|
||||||
filtered_image[i - 1, j - 1] = np.median(region)
|
for i in range(2, height - 2):
|
||||||
|
for j in range(2, width - 2):
|
||||||
return filtered_image
|
# 取5x5邻域的所有值
|
||||||
|
neighbors = [
|
||||||
# 5x5 中值平滑滤波
|
image[i - 2, j - 2], image[i - 2, j - 1], image[i - 2, j], image[i - 2, j + 1], image[i - 2, j + 2],
|
||||||
def med_filter_5x5(image, height, width):
|
image[i - 1, j - 2], image[i - 1, j - 1], image[i - 1, j], image[i - 1, j + 1], image[i - 1, j + 2],
|
||||||
# 创建空白图像以存储滤波结果
|
image[i, j - 2], image[i, j - 1], image[i, j], image[i, j + 1], image[i, j + 2],
|
||||||
filtered_image = np.zeros((height - 4, width - 4), dtype=np.uint8)
|
image[i + 1, j - 2], image[i + 1, j - 1], image[i + 1, j], image[i + 1, j + 1], image[i + 1, j + 2],
|
||||||
|
image[i + 2, j - 2], image[i + 2, j - 1], image[i + 2, j], image[i + 2, j + 1], image[i + 2, j + 2]
|
||||||
# 执行5x5中值滤波
|
]
|
||||||
for i in range(2, height - 2):
|
# 计算中值
|
||||||
for j in range(2, width - 2):
|
filtered_image[i - 2, j - 2] = np.median(neighbors)
|
||||||
# 取5x5邻域的所有值
|
|
||||||
neighbors = [
|
return filtered_image
|
||||||
image[i - 2, j - 2], image[i - 2, j - 1], image[i - 2, j], image[i - 2, j + 1], image[i - 2, j + 2],
|
|
||||||
image[i - 1, j - 2], image[i - 1, j - 1], image[i - 1, j], image[i - 1, j + 1], image[i - 1, j + 2],
|
# 读取灰度图像
|
||||||
image[i, j - 2], image[i, j - 1], image[i, j], image[i, j + 1], image[i, j + 2],
|
im = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
||||||
image[i + 1, j - 2], image[i + 1, j - 1], image[i + 1, j], image[i + 1, j + 1], image[i + 1, j + 2],
|
|
||||||
image[i + 2, j - 2], image[i + 2, j - 1], image[i + 2, j], image[i + 2, j + 1], image[i + 2, j + 2]
|
# 获取图像尺寸
|
||||||
]
|
height, width = im.shape
|
||||||
# 计算中值
|
|
||||||
filtered_image[i - 2, j - 2] = np.median(neighbors)
|
# 邻域平均
|
||||||
|
mean = mean_filter(im, height, width)
|
||||||
return filtered_image
|
# 中值滤波3x3
|
||||||
|
median = median_filter(im, height, width)
|
||||||
# 读取灰度图像
|
# 中值滤波5x5
|
||||||
im = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
|
med = med_filter_5x5(im, height, width)
|
||||||
|
|
||||||
# 获取图像尺寸
|
min_height = min(mean.shape[0], median.shape[0], med.shape[0])
|
||||||
height, width = im.shape
|
min_width = min(mean.shape[1], median.shape[1], med.shape[1])
|
||||||
|
|
||||||
# 邻域平均
|
mean_cropped = mean[:min_height, :min_width]
|
||||||
mean = mean_filter(im, height, width)
|
median_cropped = median[:min_height, :min_width]
|
||||||
# 中值滤波3x3
|
med_cropped = med[:min_height, :min_width]
|
||||||
median = median_filter(im, height, width)
|
|
||||||
# 中值滤波5x5
|
combined = np.hstack((mean_cropped, median_cropped, med_cropped))
|
||||||
med = med_filter_5x5(im, height, width)
|
# 更新 edge 变量
|
||||||
|
edge = Image.fromarray(combined)
|
||||||
min_height = min(mean.shape[0], median.shape[0], med.shape[0])
|
|
||||||
min_width = min(mean.shape[1], median.shape[1], med.shape[1])
|
# 创建Toplevel窗口
|
||||||
|
try:
|
||||||
mean_cropped = mean[:min_height, :min_width]
|
AirsmoWin.destroy()
|
||||||
median_cropped = median[:min_height, :min_width]
|
except Exception as e:
|
||||||
med_cropped = med[:min_height, :min_width]
|
print("NVM")
|
||||||
|
finally:
|
||||||
combined = np.hstack((mean_cropped, median_cropped, med_cropped))
|
AirsmoWin = Toplevel()
|
||||||
# 更新 edge 变量
|
AirsmoWin.attributes('-topmost', True)
|
||||||
edge = Image.fromarray(combined)
|
AirsmoWin.geometry("720x300")
|
||||||
|
AirsmoWin.resizable(True, True) # 可缩放
|
||||||
# 创建Toplevel窗口
|
AirsmoWin.title("空域平滑结果")
|
||||||
try:
|
|
||||||
AirsmoWin.destroy()
|
# 显示图像
|
||||||
except Exception as e:
|
LabelPic = tk.Label(AirsmoWin, text="IMG", width=720, height=240)
|
||||||
print("NVM")
|
image = ImageTk.PhotoImage(Image.fromarray(combined))
|
||||||
finally:
|
LabelPic.image = image
|
||||||
AirsmoWin = Toplevel()
|
LabelPic['image'] = image
|
||||||
AirsmoWin.attributes('-topmost', True)
|
|
||||||
AirsmoWin.geometry("720x300")
|
LabelPic.bind('<Configure>', lambda event: changeSize(event, combined, LabelPic))
|
||||||
AirsmoWin.resizable(True, True) # 可缩放
|
LabelPic.pack(fill=tk.BOTH, expand=tk.YES)
|
||||||
AirsmoWin.title("空域平滑结果")
|
|
||||||
|
# 添加保存按钮
|
||||||
# 显示图像
|
btn_save = tk.Button(AirsmoWin, text="保存", bg='#add8e6', fg='black', font=('Helvetica', 14), width=20,
|
||||||
LabelPic = tk.Label(AirsmoWin, text="IMG", width=720, height=240)
|
command=savefile)
|
||||||
image = ImageTk.PhotoImage(Image.fromarray(combined))
|
btn_save.pack(pady=10)
|
||||||
LabelPic.image = image
|
|
||||||
LabelPic['image'] = image
|
|
||||||
|
|
||||||
LabelPic.bind('<Configure>', lambda event: changeSize(event, combined, LabelPic))
|
|
||||||
LabelPic.pack(fill=tk.BOTH, expand=tk.YES)
|
|
||||||
|
|
||||||
# 添加保存按钮
|
|
||||||
btn_save = tk.Button(AirsmoWin, text="保存", bg='#add8e6', fg='black', font=('Helvetica', 14), width=20,
|
|
||||||
command=savefile)
|
|
||||||
btn_save.pack(pady=10)
|
|
||||||
|
|
||||||
return
|
return
|
Loading…
Reference in new issue