OpenCV – 开源计算机视觉。它是计算机视觉和图像处理任务中使用最广泛的工具之一。它被
用于各种应用,如面部检测、视频捕捉、跟踪移动物体、对象公开。如今应用在 Covid 中,如
口罩检测、社交距离等等。
今天让我们从零开始学习OpenCV的入门小知识吧!
所有文章完整的素材+源码都在
粉丝白嫖源码福利,请移步至CSDN社区或文末公众hao即可免费。
基于opencv和tkinter的图像处理系统1.0,是用python语言进行编写,亲测有效。
制作该系统其主要目的是给初学图像处理的学生提供一些直观的印象。
当然系统存在不足:
1.无法保存处理后生成的图片;
2.由于对窗口进行了限定,输入的图像最好为250*250,这样刚好显示。
希望有感兴趣的同学,下载交流学习,小编编程技术有限,代码可读性一般,也希望多批评指
正,多多交流。当然运行该代码,一些必要的安装包是必不可少的,如果无法运行,也可以联
系我哦~
形态学操作就是改变物体的形状,如腐蚀使物体"变瘦",膨胀使物体"变胖" 先腐蚀后膨胀会
分离物体,所以叫开运算,常用来去除小区域物体 先膨胀后腐蚀会消除物体内的小洞,所以叫
闭运算 :
img_path = asksaveasfilename(initialdir = file_path,
filetypes=[("jpg格式","jpg"), ("png格式","png"), ("bmp格式","bmp")],
parent = self.root,
title = '保存图片')
常见噪声有椒盐噪声和高斯噪声,椒盐噪声可以理解为斑点,随机出现在图像中的黑点或白
点; 高斯噪声可以理解为拍摄图片时由于光照等原因造成的噪声;这样解释并不准确,只要能
简单分辨即可。
环境安装:python 3.8: 解释器、pycharm: 代码编辑器、tkinter、numpy、opencv、
pillow等部 分自带的模块 直接安装Python就可以使用了。
相对应的安装包/安装教程/激活码/使用教程/学 习资料/工具插件 可以直接找我厚台获取 。
第三方库的安装方式如下:
一般安装:pip install +模块名
镜像源安装:pip install -i https://pypi.douban.com/simple/+模块名
(还有很多国内镜像源,这里是豆瓣的用习惯 了,其他镜像源可以去看下之前文章都有的)
模块安装问题可以详细的找我给大家讲一下的哈,之前其实也有的文章写了几个点的。
(可自选,还可以找其他的图片哈~输入的图像最好为250*250)
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 = file_path + os.sep + '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)
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 = file_path + os.sep + '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(sef.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()
如果看到这里,说明你喜欢这篇文章,请评论、转发、点赞三连哦!
每日更新一篇高质量博文,期待你们的关注啦,今天的内容到此结束,下期我们再见~
项目1.1 动漫化人物
【突破次元壁】谁说二次元离我们遥远?Python特效火遍全网,关键技术原来是它。
项目1.2 颜值打分系统
Python小测试 2021最新男女颜值打分小系统标准出炉,看哭无数人...
项目3.2 Opencv合集3款神仙代码
【OpenCV案例合集】竟还有如此的神仙代码,爱了爱了......真有趣(附3款源码)
项目3.3 图片处理加/去水印
【一篇解决】Python图片处理: 去水印/加水印—这几个方法你一定要学会,太神奇了~(建议保留)
项目3.0 Opencv换背景图
【Opencv实战】AI换背景:朋友结婚没有蓝天白云怎么办?幸亏我急中生智。
项目3.1 抠图神器
【爆赞】这款Python小程序自动抠图只需5秒,秒杀PS手动抠图?
汇总合集 Python—2022 |已有文章汇总 | 持续更新,直接看这篇就够了
(更多内容+源码都在✨文章汇总哦!!欢迎阅读喜欢的文章~