Update image_sharp.py

main
pos97em56 5 months ago
parent 87a6541480
commit 6c1bb98f70

@ -1,372 +1,333 @@
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
FreqsharWin = 0 FreqsharWin = 0
AirsharWin = 0 AirsharWin = 0
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 select_image(root): def changeSize(event, img, LabelPic):
global img_path, src, img_label img_aspect = img.shape[1] / img.shape[0]
new_aspect = event.width / event.height
img_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.png;*.jpeg;*.bmp")])
if img_path: if new_aspect > img_aspect:
# 确保路径中的反斜杠正确处理,并使用 UTF-8 编码处理中文路径 new_width = int(event.height * img_aspect)
img_path_fixed = os.path.normpath(img_path) new_height = event.height
else:
# 图像输入 new_width = event.width
src_temp = cv2.imdecode(np.fromfile(img_path_fixed, dtype=np.uint8), cv2.IMREAD_UNCHANGED) new_height = int(event.width / img_aspect)
if src_temp is None:
messagebox.showerror("错误", "无法读取图片,请选择有效的图片路径") resized_image = cv2.resize(img, (new_width, new_height))
return image1 = ImageTk.PhotoImage(Image.fromarray(resized_image))
src = cv2.cvtColor(src_temp, cv2.COLOR_BGR2RGB) LabelPic.image = image1
LabelPic['image'] = image1
# 检查 img_label 是否存在且有效
if img_label is None or not img_label.winfo_exists(): def savefile():
img_label = tk.Label(root) global edge
img_label.pack(side=tk.TOP, pady=10)
filename = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg"), ("PNG files", "*.png"), ("BMP files", "*.bmp")])
img = Image.open(img_path) if not filename:
img.thumbnail((160, 160)) return
img_tk = ImageTk.PhotoImage(img) # 确保 edge 变量已定义
img_label.configure(image=img_tk) if edge is not None:
img_label.image = img_tk try:
src = cv2.cvtColor(src, cv2.COLOR_BGR2RGB) edge.save(filename)
else: messagebox.showinfo("保存成功", "图片保存成功!")
messagebox.showerror("错误", "没有选择图片路径") except Exception as e:
messagebox.showerror("保存失败", f"无法保存图片: {e}")
def show_selected_image(root): else:
global img_label messagebox.showerror("保存失败", "没有图像可保存")
img_label = tk.Label(root)
img_label.pack(side=tk.TOP, pady=10) #频域锐化
img = Image.open(img_path) def freq_shar(root):
img.thumbnail((200, 200)) global src, FreqsharWin, edge
img_tk = ImageTk.PhotoImage(img)
img_label.configure(image=img_tk) # 判断是否已经选取图片
img_label.image = img_tk if src is None:
messagebox.showerror("错误", "没有选择图片!")
def changeSize(event, img, LabelPic): return
img_aspect = img.shape[1] / img.shape[0]
new_aspect = event.width / event.height # 理想高通滤波
def Ideal_HighPassFilter(rows, cols, crow, ccol, D0=40):
if new_aspect > img_aspect: # 创建空白图像以存储滤波结果
new_width = int(event.height * img_aspect) Ideal_HighPass = np.zeros((rows, cols), np.uint8)
new_height = event.height # 计算理想高通滤波器
else: for i in range(rows):
new_width = event.width for j in range(cols):
new_height = int(event.width / img_aspect) D = np.sqrt((i - crow) ** 2 + (j - ccol) ** 2)
if D >= D0:
resized_image = cv2.resize(img, (new_width, new_height)) Ideal_HighPass[i, j] = 255
image1 = ImageTk.PhotoImage(Image.fromarray(resized_image)) # 应用滤波器到频域表示
LabelPic.image = image1 mask = Ideal_HighPass[:, :, np.newaxis]
LabelPic['image'] = image1 fshift = dft_shift * mask
# 逆傅里叶变换以获得处理后的图像
def savefile(): f_ishift = np.fft.ifftshift(fshift)
global edge img_back = cv2.idft(f_ishift)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
filename = filedialog.asksaveasfilename(defaultextension=".jpg", filetypes=[("JPEG files", "*.jpg"), ("PNG files", "*.png"), ("BMP files", "*.bmp")]) # 归一化图像到0-255
if not filename: cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
return img_back = np.uint8(img_back)
# 确保 edge 变量已定义
if edge is not None: return img_back
try:
edge.save(filename) #Butterworth高通滤波器
messagebox.showinfo("保存成功", "图片保存成功!") def ButterWorth_HighPassFilter(rows, cols, crow, ccol, D0=40, n=2):
except Exception as e: # 创建空白图像以存储滤波结果
messagebox.showerror("保存失败", f"无法保存图片: {e}") ButterWorth_HighPass = np.zeros((rows, cols), np.uint8)
else:
messagebox.showerror("保存失败", "没有图像可保存") # 计算 Butterworth 高通滤波器
for i in range(rows):
#频域锐化 for j in range(cols):
def freq_shar(root): D = np.sqrt((i - crow) ** 2 + (j - ccol) ** 2)
global src, FreqsharWin, edge if D == 0:
ButterWorth_HighPass[i, j] = 0 # 如果 D = 0直接赋值为 0避免除以零错误
# 判断是否已经选取图片 else:
if src is None: ButterWorth_HighPass[i, j] = 255 / (1 + (D0 / D) ** (2 * n))
messagebox.showerror("错误", "没有选择图片!")
return # 应用滤波器到频域表示
mask = ButterWorth_HighPass[:, :, np.newaxis]
# 理想高通滤波 fshift = dft_shift * mask
def Ideal_HighPassFilter(rows, cols, crow, ccol, D0=40):
# 创建空白图像以存储滤波结果 # 逆傅里叶变换以获得处理后的图像
Ideal_HighPass = np.zeros((rows, cols), np.uint8) f_ishift = np.fft.ifftshift(fshift)
# 计算理想高通滤波器 img_back = cv2.idft(f_ishift)
for i in range(rows): img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
for j in range(cols):
D = np.sqrt((i - crow) ** 2 + (j - ccol) ** 2) # 归一化图像到0-255
if D >= D0: cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
Ideal_HighPass[i, j] = 255 img_back = np.uint8(img_back)
# 应用滤波器到频域表示
mask = Ideal_HighPass[:, :, np.newaxis] return img_back
fshift = dft_shift * mask
# 逆傅里叶变换以获得处理后的图像 #Gauss高通滤波器
f_ishift = np.fft.ifftshift(fshift) def Gauss_HighPassFilter(rows, cols, crow, ccol, D0=40):
img_back = cv2.idft(f_ishift) # 创建空白图像以存储滤波结果
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]) Gauss_HighPass = np.zeros((rows, cols), np.uint8)
# 归一化图像到0-255
cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX) # 计算 Gauss 高通滤波器
img_back = np.uint8(img_back) for i in range(rows):
for j in range(cols):
return img_back D = np.sqrt((i - crow) ** 2 + (j - ccol) ** 2)
Gauss_HighPass[i, j] = 255 * (1 - np.exp(-0.5 * (D ** 2) / (D0 ** 2)))
#Butterworth高通滤波器
def ButterWorth_HighPassFilter(rows, cols, crow, ccol, D0=40, n=2): # 应用滤波器到频域表示
# 创建空白图像以存储滤波结果 mask = Gauss_HighPass[:, :, np.newaxis]
ButterWorth_HighPass = np.zeros((rows, cols), np.uint8) fshift = dft_shift * mask
# 计算 Butterworth 高通滤波器 # 逆傅里叶变换以获得平滑后的图像
for i in range(rows): f_ishift = np.fft.ifftshift(fshift)
for j in range(cols): img_back = cv2.idft(f_ishift)
D = np.sqrt((i - crow) ** 2 + (j - ccol) ** 2) img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1])
if D == 0:
ButterWorth_HighPass[i, j] = 0 # 如果 D = 0直接赋值为 0避免除以零错误 # 归一化图像到0-255
else: cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
ButterWorth_HighPass[i, j] = 255 / (1 + (D0 / D) ** (2 * n)) img_back = np.uint8(img_back)
# 应用滤波器到频域表示 return img_back
mask = ButterWorth_HighPass[:, :, np.newaxis]
fshift = dft_shift * mask # 读取灰度图像
im = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
# 逆傅里叶变换以获得处理后的图像
f_ishift = np.fft.ifftshift(fshift) # 获取图像尺寸
img_back = cv2.idft(f_ishift) rows, cols = im.shape
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]) crow, ccol = rows // 2, cols // 2 # 中心位置
# 归一化图像到0-255 # 获取图像的频域表示
cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX) dft = cv2.dft(np.float32(im), flags=cv2.DFT_COMPLEX_OUTPUT)
img_back = np.uint8(img_back) dft_shift = np.fft.fftshift(dft)
return img_back # 理想高通滤波器
Ideal_HighPass = Ideal_HighPassFilter(rows, cols, crow, ccol)
#Gauss高通滤波器 # 巴特沃斯高通滤波器
def Gauss_HighPassFilter(rows, cols, crow, ccol, D0=40): ButterWorth_HighPass = ButterWorth_HighPassFilter(rows, cols, crow, ccol)
# 创建空白图像以存储滤波结果 # 高斯高通滤波器
Gauss_HighPass = np.zeros((rows, cols), np.uint8) Gauss_HighPass = Gauss_HighPassFilter(rows, cols, crow, ccol)
# 计算 Gauss 高通滤波器 combined = np.hstack((Ideal_HighPass, ButterWorth_HighPass, Gauss_HighPass))
for i in range(rows):
for j in range(cols): # 更新 edge 变量
D = np.sqrt((i - crow) ** 2 + (j - ccol) ** 2) edge = Image.fromarray(combined)
Gauss_HighPass[i, j] = 255 * (1 - np.exp(-0.5 * (D ** 2) / (D0 ** 2)))
# 创建Toplevel窗口
# 应用滤波器到频域表示 try:
mask = Gauss_HighPass[:, :, np.newaxis] FreqsharWin.destroy()
fshift = dft_shift * mask except Exception as e:
print("NVM")
# 逆傅里叶变换以获得平滑后的图像 finally:
f_ishift = np.fft.ifftshift(fshift) FreqsharWin = Toplevel()
img_back = cv2.idft(f_ishift) FreqsharWin.attributes('-topmost', True)
img_back = cv2.magnitude(img_back[:, :, 0], img_back[:, :, 1]) FreqsharWin.geometry("720x300")
FreqsharWin.resizable(True, True) # 可缩放
# 归一化图像到0-255 FreqsharWin.title("频域锐化结果")
cv2.normalize(img_back, img_back, 0, 255, cv2.NORM_MINMAX)
img_back = np.uint8(img_back) # 显示图像
LabelPic = tk.Label(FreqsharWin, text="IMG", width=720, height=240)
return img_back image = ImageTk.PhotoImage(Image.fromarray(combined))
LabelPic.image = image
# 读取灰度图像 LabelPic['image'] = image
im = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
LabelPic.bind('<Configure>', lambda event: changeSize(event, combined, LabelPic))
# 获取图像尺寸 LabelPic.pack(fill=tk.BOTH, expand=tk.YES)
rows, cols = im.shape
crow, ccol = rows // 2, cols // 2 # 中心位置 # 添加保存按钮
btn_save = tk.Button(FreqsharWin, text="保存", bg='#add8e6', fg='black', font=('Helvetica', 14), width=20,
# 获取图像的频域表示 command=savefile)
dft = cv2.dft(np.float32(im), flags=cv2.DFT_COMPLEX_OUTPUT) btn_save.pack(pady=10)
dft_shift = np.fft.fftshift(dft)
return
# 理想高通滤波器
Ideal_HighPass = Ideal_HighPassFilter(rows, cols, crow, ccol) #空域锐化
# 巴特沃斯高通滤波器 def air_shar(root):
ButterWorth_HighPass = ButterWorth_HighPassFilter(rows, cols, crow, ccol) global src, AirsharWin, edge
# 高斯高通滤波器
Gauss_HighPass = Gauss_HighPassFilter(rows, cols, crow, ccol) # 判断是否已经选取图片
if src is None:
combined = np.hstack((Ideal_HighPass, ButterWorth_HighPass, Gauss_HighPass)) messagebox.showerror("错误", "没有选择图片!")
return
# 更新 edge 变量
edge = Image.fromarray(combined) # 定义Roberts边缘检测算子
def Roberts(Image_In, height, width):
# 创建Toplevel窗口 # 创建输出图像
try: Roberts = np.zeros((height - 1, width - 1), dtype=np.uint8)
FreqsharWin.destroy()
except Exception as e: # 进行Roberts边缘检测
print("NVM") for i in range(height - 1):
finally: for j in range(width - 1):
FreqsharWin = Toplevel() # 计算Roberts响应
FreqsharWin.attributes('-topmost', True) tmp = abs(int(Image_In[i + 1, j + 1]) - int(Image_In[i, j])) + abs(
FreqsharWin.geometry("720x300") int(Image_In[i + 1, j]) - int(Image_In[i, j + 1]))
FreqsharWin.resizable(True, True) # 可缩放 tmp = max(0, min(255, tmp)) # 确保结果在0到255之间
FreqsharWin.title("频域锐化结果") Roberts[i, j] = tmp
# 显示图像 return Roberts
LabelPic = tk.Label(FreqsharWin, text="IMG", width=720, height=240)
image = ImageTk.PhotoImage(Image.fromarray(combined)) # 定义Sobel边缘检测算子
LabelPic.image = image def Sobel(Image_In, height, width):
LabelPic['image'] = image # 创建输出图像
Image_Sobel = np.zeros((height - 2, width - 2), dtype=np.uint8)
LabelPic.bind('<Configure>', lambda event: changeSize(event, combined, LabelPic))
LabelPic.pack(fill=tk.BOTH, expand=tk.YES) # 进行Sobel边缘检测
for i in range(1, height - 1):
# 添加保存按钮 for j in range(1, width - 1):
btn_save = tk.Button(FreqsharWin, text="保存", bg='#add8e6', fg='black', font=('Helvetica', 14), width=20, # 使用Sobel算子计算水平和垂直方向的梯度
command=savefile) tmp1 = abs(-int(Image_In[i - 1, j - 1]) - 2 * int(Image_In[i - 1, j]) - int(Image_In[i - 1, j + 1]) +
btn_save.pack(pady=10) int(Image_In[i + 1, j - 1]) + 2 * int(Image_In[i + 1, j]) + int(Image_In[i + 1, j + 1]))
tmp2 = abs(-int(Image_In[i - 1, j - 1]) - 2 * int(Image_In[i, j - 1]) - int(Image_In[i + 1, j - 1]) +
return int(Image_In[i - 1, j + 1]) + 2 * int(Image_In[i, j + 1]) + int(Image_In[i + 1, j + 1]))
tmp = tmp1 + tmp2
#空域锐化 tmp = max(0, min(255, tmp)) # 确保结果在0到255之间
def air_shar(root): Image_Sobel[i - 1, j - 1] = tmp
global src, AirsharWin, edge
return Image_Sobel
# 判断是否已经选取图片
if src is None: # 定义Prewitt边缘检测算子
messagebox.showerror("错误", "没有选择图片!") def Prewitt(Image_In, height, width):
return # 创建输出图像
Image_Prewitt = np.zeros((height - 2, width - 2), dtype=np.uint8)
# 定义Roberts边缘检测算子
def Roberts(Image_In, height, width): # 进行Prewitt边缘检测
# 创建输出图像 for i in range(1, height - 1):
Roberts = np.zeros((height - 1, width - 1), dtype=np.uint8) for j in range(1, width - 1):
# 使用Prewitt算子计算水平和垂直方向的梯度
# 进行Roberts边缘检测 tmp1 = abs(-int(Image_In[i - 1, j - 1]) - int(Image_In[i - 1, j]) - int(Image_In[i - 1, j + 1]) +
for i in range(height - 1): int(Image_In[i + 1, j - 1]) + int(Image_In[i + 1, j]) + int(Image_In[i + 1, j + 1]))
for j in range(width - 1): tmp2 = abs(-int(Image_In[i - 1, j - 1]) - int(Image_In[i, j - 1]) - int(Image_In[i + 1, j - 1]) +
# 计算Roberts响应 int(Image_In[i - 1, j + 1]) + int(Image_In[i, j + 1]) + int(Image_In[i + 1, j + 1]))
tmp = abs(int(Image_In[i + 1, j + 1]) - int(Image_In[i, j])) + abs( tmp = tmp1 + tmp2
int(Image_In[i + 1, j]) - int(Image_In[i, j + 1])) tmp = max(0, min(255, tmp)) # 确保结果在0到255之间
tmp = max(0, min(255, tmp)) # 确保结果在0到255之间 Image_Prewitt[i - 1, j - 1] = tmp
Roberts[i, j] = tmp
return Image_Prewitt
return Roberts
# 将彩色图像转换为灰度图像
# 定义Sobel边缘检测算子 im = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
def Sobel(Image_In, height, width):
# 创建输出图像 # 获取图像的尺寸
Image_Sobel = np.zeros((height - 2, width - 2), dtype=np.uint8) height, width = im.shape
# 进行Sobel边缘检测 # 使用各种算子进行边缘检测
for i in range(1, height - 1): Rob = Roberts(im, height, width) # Roberts算子
for j in range(1, width - 1): Sob = Sobel(im, height, width) # Sobel算子
# 使用Sobel算子计算水平和垂直方向的梯度 Pre = Prewitt(im, height, width) # Prewitt算子
tmp1 = abs(-int(Image_In[i - 1, j - 1]) - 2 * int(Image_In[i - 1, j]) - int(Image_In[i - 1, j + 1]) +
int(Image_In[i + 1, j - 1]) + 2 * int(Image_In[i + 1, j]) + int(Image_In[i + 1, j + 1])) # 找出最小的尺寸,以便进行裁剪使得结果图像尺寸一致
tmp2 = abs(-int(Image_In[i - 1, j - 1]) - 2 * int(Image_In[i, j - 1]) - int(Image_In[i + 1, j - 1]) + min_height = min(Rob.shape[0], Sob.shape[0], Pre.shape[0])
int(Image_In[i - 1, j + 1]) + 2 * int(Image_In[i, j + 1]) + int(Image_In[i + 1, j + 1])) min_width = min(Rob.shape[1], Sob.shape[1], Pre.shape[1])
tmp = tmp1 + tmp2
tmp = max(0, min(255, tmp)) # 确保结果在0到255之间 # 对所有结果进行裁剪,使它们的尺寸一致
Image_Sobel[i - 1, j - 1] = tmp Rob = Rob[:min_height, :min_width]
Sob = Sob[:min_height, :min_width]
return Image_Sobel Pre = Pre[:min_height, :min_width]
# 定义Prewitt边缘检测算子 # 将三种边缘检测结果水平拼接成一张图像
def Prewitt(Image_In, height, width): combined = np.hstack((Rob, Sob, Pre))
# 创建输出图像
Image_Prewitt = np.zeros((height - 2, width - 2), dtype=np.uint8) # 更新 edge 变量,这里假设 edge 是用来存储结果图像的变量
# 进行Prewitt边缘检测 # 创建Toplevel窗口用于显示结果
for i in range(1, height - 1): try:
for j in range(1, width - 1): AirsharWin.destroy()
# 使用Prewitt算子计算水平和垂直方向的梯度 except Exception as e:
tmp1 = abs(-int(Image_In[i - 1, j - 1]) - int(Image_In[i - 1, j]) - int(Image_In[i - 1, j + 1]) + print("NVM")
int(Image_In[i + 1, j - 1]) + int(Image_In[i + 1, j]) + int(Image_In[i + 1, j + 1])) finally:
tmp2 = abs(-int(Image_In[i - 1, j - 1]) - int(Image_In[i, j - 1]) - int(Image_In[i + 1, j - 1]) + AirsharWin = Toplevel()
int(Image_In[i - 1, j + 1]) + int(Image_In[i, j + 1]) + int(Image_In[i + 1, j + 1])) AirsharWin.attributes('-topmost', True)
tmp = tmp1 + tmp2 AirsharWin.geometry("720x300")
tmp = max(0, min(255, tmp)) # 确保结果在0到255之间 AirsharWin.resizable(True, True) # 可缩放
Image_Prewitt[i - 1, j - 1] = tmp AirsharWin.title("空域锐化结果")
return Image_Prewitt # 显示图像
LabelPic = tk.Label(AirsharWin, text="IMG", width=720, height=240)
# 将彩色图像转换为灰度图像 image = ImageTk.PhotoImage(Image.fromarray(combined))
im = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) LabelPic.image = image
LabelPic['image'] = image
# 获取图像的尺寸
height, width = im.shape # 配置LabelPic以自适应窗口大小
LabelPic.bind('<Configure>', lambda event: changeSize(event, combined, LabelPic))
# 使用各种算子进行边缘检测 LabelPic.pack(fill=tk.BOTH, expand=tk.YES)
Rob = Roberts(im, height, width) # Roberts算子
Sob = Sobel(im, height, width) # Sobel算子 # 添加保存按钮
Pre = Prewitt(im, height, width) # Prewitt算子 btn_save = tk.Button(AirsharWin, text="保存", bg='#add8e6', fg='black', font=('Helvetica', 14), width=20,
command=savefile)
# 找出最小的尺寸,以便进行裁剪使得结果图像尺寸一致 btn_save.pack(pady=10)
min_height = min(Rob.shape[0], Sob.shape[0], Pre.shape[0])
min_width = min(Rob.shape[1], Sob.shape[1], Pre.shape[1]) return
# 对所有结果进行裁剪,使它们的尺寸一致
Rob = Rob[:min_height, :min_width]
Sob = Sob[:min_height, :min_width]
Pre = Pre[:min_height, :min_width]
# 将三种边缘检测结果水平拼接成一张图像
combined = np.hstack((Rob, Sob, Pre))
# 更新 edge 变量,这里假设 edge 是用来存储结果图像的变量
# 创建Toplevel窗口用于显示结果
try:
AirsharWin.destroy()
except Exception as e:
print("NVM")
finally:
AirsharWin = Toplevel()
AirsharWin.attributes('-topmost', True)
AirsharWin.geometry("720x300")
AirsharWin.resizable(True, True) # 可缩放
AirsharWin.title("空域锐化结果")
# 显示图像
LabelPic = tk.Label(AirsharWin, text="IMG", width=720, height=240)
image = ImageTk.PhotoImage(Image.fromarray(combined))
LabelPic.image = image
LabelPic['image'] = image
# 配置LabelPic以自适应窗口大小
LabelPic.bind('<Configure>', lambda event: changeSize(event, combined, LabelPic))
LabelPic.pack(fill=tk.BOTH, expand=tk.YES)
# 添加保存按钮
btn_save = tk.Button(AirsharWin, text="保存", bg='#add8e6', fg='black', font=('Helvetica', 14), width=20,
command=savefile)
btn_save.pack(pady=10)
return

Loading…
Cancel
Save