'''
@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
# global start 全局变量定义初始化开始
#全局变量参数
carmela_hight = 200
carmela_width = 200
#Tkinter元素句柄参数
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__))
#global end 全局变量定义初始化结束
#创建窗口对象
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('尚未选择文件')
# resizeImage 此段代码抄袭
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
#窗口使用变量初始化结束
#图片获取通过图片路径显示标签,返回值为cv2.imread 缩放后
def ImageGet():
#读取图片参数,通过opencv
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
#RGB图像转换并且显示
#图片格式转化………………不理解
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):#检测是否是第一次输入图片,是的话创建RGB图片显示Label
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:#不是的话更改Label中的图片
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图片显示Label
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:#不是的话更改Label中的图片
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):#检测是否是第一次输入图片,是的话创建GRAY图片显示Label
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:#不是的话更改Label中的图片
Canny_Img_Label.configure(image=Temp_Img)
Canny_Img_Label.image = Temp_Img
#图片选择按键绑定函数
def AskPicture():
#图片路径获取
Picture_Path = filedialog.askopenfilename()
#显示图片路径,在Img_Path_Text中
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)
#设置窗口是否可变长、宽,True:可变,False:不可变
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()
Python学习随笔02 OpenCV对图片高斯平滑处理,并进行边缘检测
最新推荐文章于 2020-12-10 21:25:39 发布