问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
import tkinter
import cv2
from PIL import Image, ImageTk
topwin = tkinter.Tk() #创建顶层窗口
topwin.geometry('1000x600') #初始化窗口大小
topwin.title('实验一') #设置窗口标题
img_open = Image.open('computer.jpg')
# img = ImageTk.PhotoImage(img_open)
img = ImageTk.PhotoImage(img_open.resize((250,300),Image.ANTIALIAS))
def SoucerImg():
label = tkinter.Label(topwin,
text='Rice原图',
image=img,
font=('黑体', 20),
fg='red', # 字体颜色
compound=tkinter.BOTTOM # 设置文本和图像的混合模式
)
label.pack(side=tkinter.LEFT)
def Laplacianshow():
label = tkinter.Label(topwin,
text='Laplacian算子',
image=imgout_1,
font=('黑体', 20),
fg='red', # 字体颜色
compound=tkinter.BOTTOM # 设置文本和图像的混合模式
)
label.pack(side=tkinter.LEFT)
def Laplacian():
# 灰度化处理图像
grayImage = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(grayImage, (3, 3), 0)
dst = cv2.Laplacian(grayImage, cv2.CV_16S, ksize=3)
Laplacian = cv2.convertScaleAbs(dst)
cv2.imwrite('Laplacianimg.png',Laplacian)
def LoG():
# 灰度化处理图像
grayImage = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(grayImage, (3, 3), 0)
# LoG算法
dst1 = cv2.Laplacian(blurred, cv2.CV_16S, ksize=3)
LoG = cv2.convertScaleAbs(dst1)
cv2.imwrite('LoGimg.png',LoG)
def LoGshow():
label = tkinter.Label(topwin,
text='LoG算子',
image=imgout_2,
font=('黑体', 20),
fg='red', # 字体颜色
compound=tkinter.BOTTOM # 设置文本和图像的混合模式
)
label.pack(side=tkinter.LEFT)
def Canny():
# 灰度化处理图像
grayImage = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(grayImage, (3, 3), 0)
# LoG算法
dst2 = cv2.Canny(blurred, 20, 40)
Canny = cv2.convertScaleAbs(dst2)
cv2.imwrite('Cannyimg.png',Canny)
def Cannyshow():
label = tkinter.Label(topwin,
text='Canny算子',
image=imgout_3,
font=('黑体', 20),
fg='red', # 字体颜色
compound=tkinter.BOTTOM # 设置文本和图像的混合模式
)
label.pack(side=tkinter.LEFT)
# 读取图像
img1 = cv2.imread('computer.jpg')
lenna_img = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)# 灰度化处理图像
Laplacian()
imgout1=Image.open("Laplacianimg.png")
imgout_1 = ImageTk.PhotoImage(imgout1.resize((250,300),Image.ANTIALIAS))
LoG()
imgout2=Image.open("LoGimg.png")
imgout_2 = ImageTk.PhotoImage(imgout2.resize((250,300),Image.ANTIALIAS))
Canny()
imgout3=Image.open("LoGimg.png")
imgout_3 = ImageTk.PhotoImage(imgout3.resize((250,300),Image.ANTIALIAS))
button1 = tkinter.Button(topwin, text ="原图",command = SoucerImg,width=10)
button1.place(x=0,y=0)
button2 = tkinter.Button(topwin, text ="Laplacian算子",command = Laplacianshow,width=10)
button2.place(x=80,y=0)
button3 = tkinter.Button(topwin, text ="LoG算子",command = LoGshow,width=10)
button3.place(x=160,y=0)
button4 = tkinter.Button(topwin, text ="Canny算子",command = Cannyshow,width=10)
button4.place(x=240,y=0)
button5=tkinter.Button(topwin,text='退出',command=topwin.quit,width=10)
button5.place(x=320,y=0)
topwin.mainloop()#显示窗口
运行结果及报错内容
我想要达到的结果
按任意按钮只显示当前按钮下插入的图片