'''
@Author: your name
@Date: 2020-02-13 13:30:07
@LastEditTime : 2020-02-13 17:02:32
@LastEditors : Please set LastEditors
@Description: 高斯平滑展示,边缘检测展示,
能够通过按键时时控制高斯平滑,高斯选择改变后改变高斯图和边缘检测图
边缘检测通过右侧两个滑条更改检测阈值
下一步加入圆形检测
'''
import tkinter
import os
import cv2
from PIL import Image,ImageTk
from tkinter import filedialog
carmela_hight = 200
carmela_width = 200
Source_Img_Label = None
Gray_Img_Label = None
Canny_Img_Label = None
Gaussian_Button = None
Threshold_Min_Scale = None
Threshold_Max_Scale = None
Gaussian_Enable = False
py_path=os.path.abspath(os.path.dirname(__file__))
root = tkinter.Tk()
screen_Width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
width = screen_Width//2
height = screen_height//2
root_win_par = '%dx%d+%d+%d'% (width, height, (screen_Width-width)/2, (screen_height-height)/2)
String_var = tkinter.StringVar()
String_var.set('尚未选择文件')
def resizeImage(image, width, height, inter=cv2.INTER_AREA):
'''
图像缩放
:param image: 输入图像
:param width: 输出图像宽限制
:param height: 输出图像高限制
:param inter: 插值方法
CV_INTER_NN - 最近-邻居插补
CV_INTER_LINEAR - 双线性插值(默认方法)
CV_INTER_AREA - 像素面积相关重采样。当缩小图像时,该方法可以避免波纹的出现。当放大图像时,类似于方法CV_INTER_NN
CV_INTER_CUBIC - 双三次插值
:return:
'''
newsize = (width, height)
(h, w) = image.shape[:2]
if w <= width and h <= height:
return image
if w > width:
n = height / float(h)
newsize = (int(n * w), height)
else:
n = width / float(w)
newsize = (width, int(h * n))
newimage = cv2.resize(image, newsize, interpolation=inter)
return newimage
def ImageGet():
Source_Img = cv2.imread(String_var.get())
if (Source_Img is None):
return None
else:
Source_Img = resizeImage(image=Source_Img,width=carmela_width,height=carmela_hight)
return Source_Img
def SourceDisplay(Source_Img):
global Source_Img_Label
Temp_Img = cv2.cvtColor(Source_Img,cv2.COLOR_BGR2RGB)
Temp_Img = Image.fromarray(Temp_Img)
Temp_Img = ImageTk.PhotoImage(Temp_Img)
if(Source_Img_Label is None):
Source_Img_Label = tkinter.Label(root,bg='red',image=Temp_Img,width=carmela_width,height=carmela_hight)
Source_Img_Label.image = Temp_Img
Source_Img_Label.place(x=10*1+carmela_width*0,y=40+10*0+carmela_hight*0)
else:
Source_Img_Label.configure(image=Temp_Img)
Source_Img_Label.image = Temp_Img
def GrayDisply(Source_Img):
global Gray_Img_Label
global Gaussian_Enable
Temp_Img = cv2.cvtColor(Source_Img,cv2.COLOR_BGR2GRAY)
if Gaussian_Enable:
Temp_Img = cv2.GaussianBlur(Temp_Img, (5,5), 0, 0, cv2.BORDER_DEFAULT)
Temp_Img = Image.fromarray(Temp_Img)
Temp_Img = ImageTk.PhotoImage(Temp_Img)
if (Gray_Img_Label is None):
Gray_Img_Label = tkinter.Label(root,bg='red',image=Temp_Img,width=carmela_width,height=carmela_hight)
Gray_Img_Label.image = Temp_Img
Gray_Img_Label.place(x=10*2+carmela_width*1,y=40+10*0+carmela_hight*0)
else:
Gray_Img_Label.configure(image=Temp_Img)
Gray_Img_Label.image = Temp_Img
def CannyDisply(Source_Img):
global Canny_Img_Label,Threshold_Min_Scale,Threshold_Max_Scale
global Gaussian_Enable
Temp_Img = cv2.cvtColor(Source_Img,cv2.COLOR_BGR2GRAY)
if Gaussian_Enable:
Temp_Img = cv2.GaussianBlur(Temp_Img, (5,5), 0, 0, cv2.BORDER_DEFAULT)
Temp_Img = cv2.Canny(Temp_Img, Threshold_Min_Scale.get(), Threshold_Max_Scale.get())
Temp_Img = Image.fromarray(Temp_Img)
Temp_Img = ImageTk.PhotoImage(Temp_Img)
if (Canny_Img_Label is None):
Canny_Img_Label = tkinter.Label(root,bg='red',image=Temp_Img,width=carmela_width,height=carmela_hight)
Canny_Img_Label.image = Temp_Img
Canny_Img_Label.place(x=10*3+carmela_width*2,y=40+10*0+carmela_hight*0)
else:
Canny_Img_Label.configure(image=Temp_Img)
Canny_Img_Label.image = Temp_Img
def AskPicture():
Picture_Path = filedialog.askopenfilename()
String_var.set(Picture_Path)
Source_Img = ImageGet()
if (Source_Img is None):
String_var.set('文件选择错误')
return
else:
SourceDisplay(Source_Img)
GrayDisply(Source_Img)
CannyDisply(Source_Img)
def GaussianChoice():
global Gaussian_Enable,Gaussian_Button
if Gaussian_Enable:
Gaussian_Enable = False
Gaussian_Button['text']='高斯关'
else:
Gaussian_Enable = True
Gaussian_Button['text']='高斯开'
Source_Img = ImageGet()
if (Source_Img is None):
return
else:
GrayDisply(Source_Img)
CannyDisply(Source_Img)
def ThresholdChange(text):
global Source_Img_Label,Gray_Img_Label,Canny_Img_Label,Threshold_Min_Scale,Threshold_Max_Scale
Source_Img = ImageGet()
if (Source_Img is None):
return
else:
CannyDisply(Source_Img)
root.geometry(root_win_par)
root.resizable(width=False, height=False)
root.title('Hello')
root.iconbitmap(py_path+'\\ico.ico')
Img_Path_Text = tkinter.Entry(root,textvariable=String_var,borderwidth=1,state=tkinter.DISABLED)
Img_Path_Text.place(x=10,y=10,width=width-20-40,height=20)
Img_Path_Button = tkinter.Button(root,text='选择',command=AskPicture)
Img_Path_Button.place(x=width-45,y=10,width=40,height=20)
Gaussian_Button = tkinter.Button(root,text='高斯关',command=GaussianChoice)
Gaussian_Button.place(x=width-45,y=40,width=40,height=20)
Threshold_Min_Scale = tkinter.Scale(root,from_=0,to=500,orient=tkinter.VERTICAL,length=height-100,command=ThresholdChange)
Threshold_Min_Scale.place(x=width-85,y=90)
Threshold_Min_Scale.set(100)
Threshold_Max_Scale = tkinter.Scale(root,from_=0,to=500,orient=tkinter.VERTICAL,length=height-100,command=ThresholdChange)
Threshold_Max_Scale.place(x=width-45,y=90)
Threshold_Max_Scale.set(200)
root.mainloop()