在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python GUI开发 → 基于opencv和tkinter的图像处理GUI-源代码python

基于opencv和tkinter的图像处理GUI-源代码python

Python GUI开发

下载此实例
  • 开发语言:Python
  • 实例大小:0.12M
  • 下载次数:6
  • 浏览次数:78
  • 发布时间:2019-06-15
  • 实例类别:Python GUI开发
  • 发 布 人:crazycode
  • 所需积分:2
 相关标签: UI c python 图像 源代码

实例介绍

【实例简介】基于opencv和tkinter的图像处理系统1.0,是用python语言进行编写,亲测有效。主体思想来源于:李立宗老师的opencv编成案例详解。制作该系统其主要目的是给初学图像处理的学生提供一些直观的印象。当然系统存在不足:1.无法保存处理后生成的图片;2.由于对窗口进行了限定,输入的图像最好为250*250,这样刚好显示

【实例截图】

from clipboard


from clipboard

from clipboard


from clipboard



from clipboard


【核心代码】

# -*- coding: utf-8 -*-
"""
Created on Sat Oct 20 10:30:53 2018

@author: 531995918@qq.com
形态学操作就是改变物体的形状,如腐蚀使物体"变瘦",膨胀使物体"变胖"
先腐蚀后膨胀会分离物体,所以叫开运算,常用来去除小区域物体
先膨胀后腐蚀会消除物体内的小洞,所以叫闭运算
img_path = asksaveasfilename(initialdir = file_path, 
                      filetypes=[("jpg格式","jpg"), ("png格式","png"), ("bmp格式","bmp")],
                      parent = self.root,
                      title = '保存图片')
"""

import tkinter.messagebox as messagebox
import tkinter as tk
from tkinter import ttk
from PIL import Image, ImageTk
import os
from tkinter.filedialog import askopenfilename, asksaveasfilename  
import cv2
import numpy as np
file_path = os.path.dirname(__file__)
test_file_path = 'D:/qqFiles/图像处理系统1.0/图像处理系统1.0/data/lena250_250.png'
WIN_WIDTH = 700
WIN_HEIGHT = 400
class Image_sys():
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry('700x400 80 80')
        self.root.title('图像处理系统1.0')#设置窗口标题
        self.root.iconbitmap('icon/icon.ico')# 设置窗口图标
        #scnWidth, scnHeight = self.root.maxsize()
        # 屏幕中心居中
        #center = '%dx%d %d %d' % (WIN_WIDTH, WIN_HEIGHT, (scnWidth - WIN_WIDTH) / 2, (scnHeight - WIN_HEIGHT) / 2)
        #print(center)
        # 设置窗口的大小宽x高 偏移量
        #self.root.geometry(center)
        # 调用方法会禁止根窗体改变大小
        self.root.resizable(False, False)
                
        menubar = tk.Menu(self.root)# 创建菜单栏 (Menu)
        self.root.config(menu = menubar)
        
        # 创建文件下拉菜单
        # 文件菜单下 tearoff=0 表示有没有分隔符,默认为有分隔符
        file_menu = tk.Menu(menubar, tearoff = 0)
        #为顶级菜单实例添加菜单,并级联相应的子菜单实例
        menubar.add_cascade(label = "文件", menu = file_menu)
        file_menu.add_command(label = "打开测试文件", command = self.open_test_file)
        file_menu.add_command(label = "打开自定义文件", command = self.open_file)
        file_menu.add_command(label = "复原", command = self.recover)
        file_menu.add_command(label = "清除", command = self.clear)
        file_menu.add_command(label = "退出", command = self.exit_sys)
        
        # 创建翻转下拉菜单
        turn_menu = tk.Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "翻转", menu = turn_menu)
        turn_menu.add_command(label = "水平", command = self.flip_horizontal)
        turn_menu.add_command(label = "垂直", command = self.flip_vertical)
        turn_menu.add_command(label = "水平&垂直", command = self.flip_hor_ver)

        # 形态学
        morph_menu = tk.Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "形态学", menu = morph_menu)
        morph_menu.add_command(label = "腐蚀", command = self.mor_corrosion)
        morph_menu.add_command(label = "膨胀", command = self.mor_expand)
        morph_menu.add_command(label = "开运算", command = self.mor_open_operation)
        morph_menu.add_command(label = "闭运算", command = self.mor_close_operation)
        morph_menu.add_command(label = "Morphological Gradient", command = self.mor_gradient)
        morph_menu.add_command(label = "顶帽", command = self.mor_top_hat)
        morph_menu.add_command(label = "黑帽", command = self.mor_black_hat)

        # 滤波
        filter_menu = tk.Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "滤波", menu = filter_menu)
        filter_menu.add_command(label = "均值", command = self.filter_mean)
        filter_menu.add_command(label = "方框", command = self.filter_box)
        filter_menu.add_command(label = "高斯", command = self.filter_gauss)
        filter_menu.add_command(label = "中值", command = self.filter_mid_value)
        filter_menu.add_command(label = "双边", command = self.filter_bilateral)

        # 缩放
        scale_menu = tk.Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "缩放", menu = scale_menu)
        scale_menu.add_command(label = "放大PyrUp", command = self.scale_pyrup)
        scale_menu.add_command(label = "缩小PyrDown", command = self.scale_pyrdown)
        scale_menu.add_command(label = "放大Resize", command = self.scale_zoom_in)
        scale_menu.add_command(label = "缩小Resize", command = self.scale_zoom_out)

        # 旋转
        rotate_menu = tk.Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "旋转", menu = rotate_menu)
        rotate_menu.add_command(label = "平移", command = self.rotate_offset)
        rotate_menu.add_command(label = "仿射", command = self.rotate_affine)
        rotate_menu.add_command(label = "透射", command = self.rotate_transmission)
        rotate_menu.add_command(label = "顺时针-无缩放", command = self.rotate_clockwise)
        rotate_menu.add_command(label = "顺时针-缩放", command = self.rotate_clockwise_zoom)
        rotate_menu.add_command(label = "逆时针-缩放", command = self.rotate_anti_zoom)
        rotate_menu.add_command(label = "零旋转-缩放", command = self.rotate_zero_zoom)

        # 帮助
        help_menu = tk.Menu(menubar, tearoff = 0)
        menubar.add_cascade(label = "帮助", menu = help_menu)
        help_menu.add_command(label = "版权", command = self.help_copyright)
        help_menu.add_command(label = "关于", command = self.help_about)                
        
 
        # 创建一个容器,其父容器为self.root
        self.frame_scr = ttk.LabelFrame(self.root, text="Scour image:") 
        # padx  pady   该容器外围需要留出的空余空间
        self.frame_scr.place(x = 80, y = 30, width = 250, height  = 250) 
      
        # 创建一个容器,其父容器为self.root
        self.frame_des = ttk.LabelFrame(self.root, text="Destination image:") 
        # padx  pady   该容器外围需要留出的空余空间
        self.frame_des.place(x = 370, y = 30, width = 250, height  = 250)    


        # 创建两个label
        label_scr = ttk.Label(self.root, text = '源图像', font = 25, foreground = 'blue', anchor = 'center')
        label_scr.place(x = 150, y = 280, width = 100, height  = 50)
       
        label_des = ttk.Label(self.root, text = '目标图像', font = 25, foreground = 'blue', anchor = 'center')
        label_des.place(x = 450, y = 280, width = 100, height  = 50)
        
        
        self.label_scr_image = None
        self.label_des_image = None           
        self.path = ''           
        self.root.mainloop()    
            
    def open_test_file(self):
        self.path = test_file_path
        image = Image.open(self.path)
        test_image = ImageTk.PhotoImage(image)
        if(self.label_des_image != None):
            self.label_des_image.pack_forget()# 隐藏控件
            self.label_des_image = None
        if(self.label_scr_image == None):
            self.label_scr_image = tk.Label(self.frame_scr,image = test_image)
        self.label_scr_image.configure(image=test_image)
        self.label_scr_image.pack()
        self.root.mainloop()
  
    def open_file(self):
        # 打开文件对话框
        open_img_path =askopenfilename(initialdir = file_path, 
                                  filetypes=[("jpg格式","jpg"), ("png格式","png"), ("bmp格式","bmp")],
                                  parent = self.root,
                                  title = '打开自定义图片')
        if (open_img_path == ''):
            return
        else:                
            if(self.label_des_image != None):
                self.label_des_image.pack_forget()# 隐藏控件
                self.label_des_image = None
            self.path =  open_img_path
            image = Image.open(self.path)
            tk_image = ImageTk.PhotoImage(image)
            if(self.label_scr_image == None):
                self.label_scr_image = tk.Label(self.frame_scr,image = tk_image)
            self.label_scr_image.configure(image = tk_image)
            self.label_scr_image.pack() # 显示控件
            self.root.mainloop()
    
    def recover(self):
        if(self.path == ''):
            return        
        image = Image.open(self.path)
        tk_image = ImageTk.PhotoImage(image)
        if(self.label_des_image == None):
            return
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()
    def clear(self):
        if(self.label_scr_image != None):
            self.label_scr_image.pack_forget()# 隐藏控件
            self.label_scr_image = None
            self.path = ''
        if(self.label_des_image != None):
            self.label_des_image.pack_forget()# 隐藏控件
            self.label_des_image = None
            self.path = ''
    def exit_sys(self):
        quit_root = messagebox.askokcancel('提示', '真的要退出么!~')
        if (quit_root == True):
            self.root.destroy()
        return

    def flip_horizontal(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return        
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        # Flipped Horizontally 水平翻转
        image_hflip = cv2.flip(image, 1)
        image_pil_hflip = Image.fromarray(image_hflip)
        tk_image = ImageTk.PhotoImage(image_pil_hflip)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()    
    def flip_vertical(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return        
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        # Flipped Horizontally 水平翻转
        image_hflip = cv2.flip(image, 0)# 垂直翻转
        image_pil_hflip = Image.fromarray(image_hflip)
        tk_image = ImageTk.PhotoImage(image_pil_hflip)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop() 
    
    def flip_hor_ver(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return        
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        # Flipped Horizontally 水平翻转
        image_hflip = cv2.flip(image, -1)# 水平垂直翻转
        image_pil_hflip = Image.fromarray(image_hflip)
        tk_image = ImageTk.PhotoImage(image_pil_hflip)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()        

    def mor_corrosion(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        #kernel = np.ones((5, 5), np.uint8)# 指定核大小
        #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))  # 矩形结构
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))  # 椭圆结构
        #kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))  # 十字形结构
        img_erosion = cv2.erode(image, kernel)  # 腐蚀
        image_pil_erosion = Image.fromarray(img_erosion)
        tk_image = ImageTk.PhotoImage(image_pil_erosion)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()                
    # 膨胀    
    def mor_expand(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        #kernel = np.ones((5, 5), np.uint8)# 指定核大小
        #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))  # 矩形结构
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))  # 椭圆结构
        #kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))  # 十字形结构
        img_dilation = cv2.dilate(image, kernel) # 膨胀
        image_pil_dilation = Image.fromarray(img_dilation)
        tk_image = ImageTk.PhotoImage(image_pil_dilation)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop() 
    # 开运算    
    def mor_open_operation(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        #kernel = np.ones((5, 5), np.uint8)# 指定核大小
        #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))  # 矩形结构
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))  # 椭圆结构
        #kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))  # 十字形结构
        img_open_operation = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)  # 开运算
        image_pil_open = Image.fromarray(img_open_operation)
        tk_image = ImageTk.PhotoImage(image_pil_open)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop() 
    # 闭运算    
    def mor_close_operation(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        #kernel = np.ones((5, 5), np.uint8)# 指定核大小
        #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))  # 矩形结构
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))  # 椭圆结构
        #kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))  # 十字形结构
        img_close_operation = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel)  # 闭运算
        image_pil_close = Image.fromarray(img_close_operation)
        tk_image = ImageTk.PhotoImage(image_pil_close)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()
    # 形态学梯度:膨胀图减去腐蚀图,dilation - erosion,这样会得到物体的轮廓:
    def mor_gradient(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        #kernel = np.ones((5, 5), np.uint8)# 指定核大小
        #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))  # 矩形结构
        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))  # 椭圆结构
        #kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))  # 十字形结构
        img_gradient = cv2.morphologyEx(image, cv2.MORPH_GRADIENT, kernel) # 形态学梯度
        image_pil_gradient = Image.fromarray(img_gradient)
        tk_image = ImageTk.PhotoImage(image_pil_gradient)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()
    # 顶帽    
    def mor_top_hat(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        kernel = np.ones((7, 7), np.uint8)# 指定核大小
        #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))  # 矩形结构
        #kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))  # 椭圆结构
        #kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))  # 十字形结构
        img_top_hat = cv2.morphologyEx(image, cv2.MORPH_TOPHAT, kernel) # 顶帽
        image_pil_top_hat = Image.fromarray(img_top_hat)
        tk_image = ImageTk.PhotoImage(image_pil_top_hat)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()
    # 黑帽    
    def mor_black_hat(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        kernel = np.ones((7, 7), np.uint8)# 指定核大小
        #kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))  # 矩形结构
        #kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))  # 椭圆结构
        #kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (5, 5))  # 十字形结构
        img_black_hat = cv2.morphologyEx(image, cv2.MORPH_BLACKHAT, kernel) # 黑帽
        image_pil_black_hat = Image.fromarray(img_black_hat)
        tk_image = ImageTk.PhotoImage(image_pil_black_hat)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()
    '''
    常见噪声有椒盐噪声和高斯噪声,椒盐噪声可以理解为斑点,随机出现在图像中的黑点或白点;
    高斯噪声可以理解为拍摄图片时由于光照等原因造成的噪声;这样解释并不准确,只要能简单分辨即可。
    '''
    # 均值滤波
    def filter_mean(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        img_mean = cv2.blur(image, (3, 3)) # 均值滤波
        image_pil_mean = Image.fromarray(img_mean)
        tk_image = ImageTk.PhotoImage(image_pil_mean)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()        
        
    # 方框滤波方框滤波跟均值滤波很像,当可选参数normalize为True的时候,方框滤波就是均值滤波,
    # 如3×3的核,a就等于1/9;normalize为False的时候,a=1,相当于求区域内的像素和。
    def filter_box(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        img_box = cv2.boxFilter(image, -1, (3, 3), normalize = False) # 方框滤波
        image_pil_box = Image.fromarray(img_box)
        tk_image = ImageTk.PhotoImage(image_pil_box)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()
    
    #高斯滤波与两种滤波方式,卷积核内的每个值都一样,相当于图像区域中每个像素的权重也就一样。
    #高斯滤波的卷积核权重并不相同,中间像素点权重最高,越远离中心的像素权重越小。
    #高斯滤波相比均值滤波效率要慢,但可以有效消除高斯噪声,能保留更多的图像细节,所以经常被称为最有用的滤波器。
    def filter_gauss(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        img_gauss = cv2.GaussianBlur(image, (1, 1), 1) # 方框滤波
        image_pil_gauss = Image.fromarray(img_gauss)
        tk_image = ImageTk.PhotoImage(image_pil_gauss)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()
    #中值滤波,中值又叫中位数,是所有值排序后取中间的值。
    #中值滤波就是用区域内的中值来代替本像素值,所以那种孤立的斑点,
    #如0或255很容易消除掉,适用于去除椒盐噪声和斑点噪声。中值是一种非线性操作,效率相比前面几种线性滤波要慢。
    #斑点噪声图,用中值滤波显然更好:
    def filter_mid_value(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        img_mid_value = cv2.medianBlur(image, 5) # 中值滤波
        image_pil_mid_value = Image.fromarray(img_mid_value)
        tk_image = ImageTk.PhotoImage(image_pil_mid_value)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()
    #双边滤波,模糊操作基本都会损失掉图像细节信息,尤其前面介绍的线性滤波器,图像的边缘信息很难保留下来。
    #然而,边缘edge信息是图像中很重要的一个特征,所以这才有了双边滤波。
    def filter_bilateral(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        img_bilateral = cv2.bilateralFilter(image, 9, 75, 75) # 双边滤波
        image_pil_bilateral = Image.fromarray(img_bilateral)
        tk_image = ImageTk.PhotoImage(image_pil_bilateral)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()
    # 图像金字塔操作的将是图像的像素问题(图像变清晰了还是模糊了)
    # 图像金字塔主要有两类:高斯金字塔和拉普拉斯金字塔。
    def scale_pyrup(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        img_pyrup = cv2.pyrUp(image) # 高斯金字塔
        image_pil_pyrup = Image.fromarray(img_pyrup)
        tk_image = ImageTk.PhotoImage(image_pil_pyrup)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.pack()
        self.root.mainloop()        
        
    def scale_pyrdown(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        img_pyrdown = cv2.pyrDown(image) # 高斯金字塔
        image_pil_pyrdown = Image.fromarray(img_pyrdown)
        tk_image = ImageTk.PhotoImage(image_pil_pyrdown)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        #self.label_des_image.place(relx=0,rely=0)# 放置组件的不同方式
        self.label_des_image.pack()
        self.root.mainloop() 
    # 放大
    def scale_zoom_in(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        size = (2*image.shape[1], 2*image.shape[0])
        img_zoom_in = cv2.resize(image, size) # 放大
        image_pil_zoom_in = Image.fromarray(img_zoom_in)
        tk_image = ImageTk.PhotoImage(image_pil_zoom_in)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        self.label_des_image.place(x=0, y=0)# 放置组件的不同方式与金字塔放大相比对齐方式不同显示不同
        #self.label_des_image.pack()
        self.root.mainloop()
    # 缩小
    def scale_zoom_out(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        size = (int(0.3*image.shape[1]), int(0.3*image.shape[0]))
        img_zoom_out = cv2.resize(image, size) # 放大
        image_pil_zoom_out = Image.fromarray(img_zoom_out)
        tk_image = ImageTk.PhotoImage(image_pil_zoom_out)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        #self.label_des_image.place(x=0, y=0)
        self.label_des_image.pack()
        self.root.mainloop()
    # 平移
    def rotate_offset(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        width, height = image.shape[1], image.shape[0]
        direction = np.float32([[1,0,50],[0,1,50]])# 沿x轴移动50,沿y轴移动50
        img_offset = cv2.warpAffine(image, direction, (width, height))
        image_pil_offset = Image.fromarray(img_offset)
        tk_image = ImageTk.PhotoImage(image_pil_offset)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        #self.label_des_image.place(x=0, y=0)
        self.label_des_image.pack()
        self.root.mainloop()
    # 仿射-需要三个点坐标
    def rotate_affine(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        width, height = image.shape[1], image.shape[0]
        pts1 = np.float32([[50,50],[200,50],[50,200]])
        pts2 = np.float32([[10,100],[200,50],[100,250]])
        rot_mat = cv2.getAffineTransform(pts1,pts2)# 沿x轴移动50,沿y轴移动50
        img_affine = cv2.warpAffine(image, rot_mat, (width, height))
        image_pil_affine = Image.fromarray(img_affine)
        tk_image = ImageTk.PhotoImage(image_pil_affine)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        #self.label_des_image.place(x=0, y=0)
        self.label_des_image.pack()
        self.root.mainloop()
    # 透射 -需要四个点的坐标
    def rotate_transmission(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        width, height = image.shape[1], image.shape[0]
        pts1 = np.float32([[56,65],[238,52],[28,237],[239,240]])
        pts2 = np.float32([[0,0],[250,0],[0,250],[250,250]])
        rot_mat = cv2.getPerspectiveTransform(pts1,pts2)
        img_clockwise = cv2.warpPerspective(image, rot_mat, (250, 250))# 透射与仿射的函数不一样
        image_pil_clockwise = Image.fromarray(img_clockwise)
        tk_image = ImageTk.PhotoImage(image_pil_clockwise)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        #self.label_des_image.place(x=0, y=0)
        self.label_des_image.pack()
        self.root.mainloop()
    
    
    # 顺时针无缩放   
    def rotate_clockwise(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        width, height = image.shape[1], image.shape[0]
        rotate_center = (width//2, height//2)
        rot_mat = cv2.getRotationMatrix2D(rotate_center, angle = -45, scale = 1) # 旋转中心rotate_center,角度degree, 缩放scale
        img_clockwise = cv2.warpAffine(image, rot_mat, (width, height))
        image_pil_clockwise = Image.fromarray(img_clockwise)
        tk_image = ImageTk.PhotoImage(image_pil_clockwise)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        #self.label_des_image.place(x=0, y=0)
        self.label_des_image.pack()
        self.root.mainloop()
    # 顺时针-缩放
    def rotate_clockwise_zoom(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        width, height = image.shape[1], image.shape[0]
        rotate_center = (width//2, height//2)
        rot_mat = cv2.getRotationMatrix2D(rotate_center, angle = -45, scale = 0.6) # 旋转中心rotate_center,角度degree, 缩放scale
        img_clockwise_zoom = cv2.warpAffine(image, rot_mat, (width, height))
        image_pil_clockwise_zoom = Image.fromarray(img_clockwise_zoom)
        tk_image = ImageTk.PhotoImage(image_pil_clockwise_zoom)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        #self.label_des_image.place(x=0, y=0)
        self.label_des_image.pack()
        self.root.mainloop()
    # 逆时针-缩放
    def rotate_anti_zoom(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        width, height = image.shape[1], image.shape[0]
        rotate_center = (width//2, height//2)
        rot_mat = cv2.getRotationMatrix2D(rotate_center, angle = 45, scale = 0.6) # 旋转中心rotate_center,角度degree, 缩放scale
        img_clockwise_zoom = cv2.warpAffine(image, rot_mat, (width, height))
        image_pil_clockwise_zoom = Image.fromarray(img_clockwise_zoom)
        tk_image = ImageTk.PhotoImage(image_pil_clockwise_zoom)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        #self.label_des_image.place(x=0, y=0)
        self.label_des_image.pack()
        self.root.mainloop()
    # 零旋转-缩放
    def rotate_zero_zoom(self):
        if(self.path == ''):
            return
        if(self.label_scr_image == None):
            return 
        image = cv2.imdecode(np.fromfile(self.path, dtype=np.uint8), 1)# 读取图片
        b, g, r = cv2.split(image)# 三通道分离
        image = cv2.merge([r,g,b])# 三通道合并
        width, height = image.shape[1], image.shape[0]
        rotate_center = (width//2, height//2)
        rot_mat = cv2.getRotationMatrix2D(rotate_center, angle = 0, scale = 0.6) # 旋转中心rotate_center,角度degree, 缩放scale
        img_zero_zoom = cv2.warpAffine(image, rot_mat, (width, height))
        image_pil_zero_zoom = Image.fromarray(img_zero_zoom)
        tk_image = ImageTk.PhotoImage(image_pil_zero_zoom)
        if (self.label_des_image == None):
            self.label_des_image = tk.Label(self.frame_des,image = tk_image)
        self.label_des_image.configure(image = tk_image)
        #self.label_des_image.place(x=0, y=0)
        self.label_des_image.pack()
        self.root.mainloop()
        
    def help_copyright(self):
        tk.messagebox.showinfo(title='版权', message='没什么版权!~')
    def help_about(self):
        tk.messagebox.showinfo(title='关于', message='图像处理系统1.0!~')
        
if __name__ == '__main__':        
    Image_sys()
    
'''
tkinter.messagebox中有如下函数:
askokcancel(title=None, message=None, **options)
Ask if operation should proceed; return true if the answer is ok
askquestion(title=None, message=None, **options)
Ask a question
askretrycancel(title=None, message=None, **options)
Ask if operation should be retried; return true if the answer is yes
askyesno(title=None, message=None, **options)
Ask a question; return true if the answer is yes
askyesnocancel(title=None, message=None, **options)
Ask a question; return true if the answer is yes, None if cancelled.
showerror(title=None, message=None, **options)
Show an error message
showinfo(title=None, message=None, **options)
Show an info message
showwarning(title=None, message=None, **options)
Show a warning message
'''    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

标签: UI c python 图像 源代码

实例下载地址

基于opencv和tkinter的图像处理GUI-源代码python

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
;
报警
var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_4847253'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s13.cnzz.com/stat.php%3Fid%3D4847253' type='text/javascript'%3E%3C/script%3E"));