OpenCV教程02:图像处理系统1.0(翻转+形态学+滤波+缩放+旋转)

-------------OpenCV教程集合-------------

Python教程99:一起来初识OpenCV(一个跨平台的计算机视觉库)

OpenCV教程01:图像的操作(读取+显示+保存+属性+获取和修改像素值)

OpenCV教程02:图像处理系统1.0(翻转+形态学+滤波+缩放+旋转)
OpenCV教程03:绘制图形(线段、矩形、圆形、椭圆、多边形、箭头线)+水印文本

1.准备一张要输入的图像素材,没有的话也可以拿下面的图片练习一下,大小为250250或300300。基于OpenCV+tkinter的图像处理系统1.0,主要功能,翻转+形态学+滤波+缩放+旋转的示例用法,代码已跑亲测有效。感兴趣的朋友,自己学习一下。当然运行该代码,一些必要的第三方安装包是必不可少的,自己看着源码,逐一模块安装一下。

2.py源码不足的地方:
a.仅支持png和jpe的图片格式,如果你要添加其他的图片格式,自己在源码里面的通用对话框增加一些格式,默认是显示png格式。
b.没有办法,保存生成的新图片,当然你可以参考,历史pillow教程博文中,tk是如何显示和保存图片的。
Pillow教程10:设计博文的文字背景封面图,再也不担心找不到素材了
c.显示图片的窗口大小是固定的,输入的图像最好为250*250,这样刚好显示。如果你要显示更大的,自己调一下tk窗口组件的大小。
在这里插入图片描述
运行后的效果
在这里插入图片描述

# -*- coding: utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
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 npfile_path = os.path.dirname(__file__)
test_file_path = file_path + os.sep + 'example.png'WIN_WIDTH = 700
WIN_HEIGHT = 400class Image_sys():def __init__(self):self.root = tk.Tk()self.root.geometry('700x400+80+80')self.root.title('图像处理系统1.0——微信公众号:WdPython')  # 设置窗口标题# 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.rootself.frame_scr = ttk.LabelFrame(self.root, text="Scour image:")# padx  pady   该容器外围需要留出的空余空间self.frame_scr.place(x=80, y=30, width=250, height=250)# 创建一个容器,其父容器为self.rootself.frame_des = ttk.LabelFrame(self.root, text="Destination image:")# padx  pady   该容器外围需要留出的空余空间self.frame_des.place(x=370, y=30, width=250, height=250)# 创建两个labellabel_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 = Noneself.label_des_image = Noneself.path = ''self.root.mainloop()def open_test_file(self):self.path = test_file_pathimage = 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 = Noneif (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=[("png格式", "png"), ("jpg格式", "jpg"), ("bmp格式", "bmp")],parent=self.root,title='打开自定义图片')if (open_img_path == ''):returnelse:if (self.label_des_image != None):self.label_des_image.pack_forget()  # 隐藏控件self.label_des_image = Noneself.path = open_img_pathimage = 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 == ''):returnimage = Image.open(self.path)tk_image = ImageTk.PhotoImage(image)if (self.label_des_image == None):returnself.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 = Noneself.path = ''if (self.label_des_image != None):self.label_des_image.pack_forget()  # 隐藏控件self.label_des_image = Noneself.path = ''def exit_sys(self):quit_root = messagebox.askokcancel('提示', '真的要退出么!~')if (quit_root == True):self.root.destroy()returndef flip_horizontal(self):if (self.path == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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轴移动50img_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 == ''):returnif (self.label_scr_image == None):returnimage = 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轴移动50img_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 == ''):returnif (self.label_scr_image == None):returnimage = 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 == ''):returnif (self.label_scr_image == None):returnimage = 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, 缩放scaleimg_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 == ''):returnif (self.label_scr_image == None):returnimage = 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, 缩放scaleimg_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 == ''):returnif (self.label_scr_image == None):returnimage = 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, 缩放scaleimg_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 == ''):returnif (self.label_scr_image == None):returnimage = 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, 缩放scaleimg_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='微信公众号:WdPython!~')def help_about(self):tk.messagebox.showinfo(title='关于', message='微信公众号:WdPython!~!~')if __name__ == '__main__':Image_sys()

完毕!!感谢您的收看

----------★★历史博文集合★★----------

我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具 NumPy Pygame

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/40018.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WIN32核心编程 - 数据类型 错误处理 字符处理

公开视频 -> 链接点击跳转公开课程博客首页 -> 链接点击跳转博客主页 目录 数据类型 基本数据类型 Win32基本数据类型 错误处理 C语言中的错误处理 C中的错误处理 Win32中的错误处理 字符处理 C/C WIN32 字符处理 数据类型 基本数据类型 C/C语言定义了一系列…

Rocky Linux 9 系统OpenSSH CVE-2024-6387 漏洞修复

Rocky Linux 9系统 OpenSSH CVE-2024-6387 漏洞修复 1、漏洞修复2、修复思路3、修复方案3.1、方案一3.2、方案二 4、总结5、参考 1、漏洞修复 CVE-2024-6387:regreSSHion:OpenSSH 服务器中的远程代码执行(RCE),至少在…

【目标检测】DINO

一、引言 论文: DINO: DETR with Improved DeNoising Anchor Boxes for End-to-End Object Detection 作者: IDEA 代码: DINO 注意: 该算法是在Deformable DETR、DAB-DETR、DN-DETR基础上的改进,在学习该算法前&#…

《IT 领域准新生暑期预习指南:开启未来科技之旅》

IT专业入门,高考假期预习指南 高考的落幕,只是人生长途中的一个逗号,对于心怀 IT 梦想的少年们,新的征程已然在脚下铺展。这个七月,当分数尘埃落定,你们即将迈向新的知识殿堂,而这个假期&#…

Git使用[推送大于100M的文件后解救办法]

推送大于100M的文件后解救办法 本文摘录于:https://blog.csdn.net/u012150602/article/details/122687435只是做学习备份之用,绝无抄袭之意,有疑惑请联系本人! 当有文件大于100M的时候在提交的时候没有问题,但是在push的似乎就不行…

番外篇 | 手把手教你如何去更换YOLOv5的检测头为ASFF_Detect

前言:Hello大家好,我是小哥谈。自适应空间特征融合(ASFF)的主要原理旨在解决单次检测器中不同尺度特征的不一致性问题。具体来说,ASFF通过动态调整来自不同尺度特征金字塔层的特征贡献,确保每个检测对象的特征表示是一致且最优的。本文所做出的改进是将YOLOv5的检测头更换…

程序算法设计分析

动态规划和分治、贪心相比有什么区别?各自的优缺点? 分治算法特征: 1)规模如果很小,则很容易解决。//一般问题都能满足 2)大问题可以分为若干规模小的相同问题。//前提 3)利用子问题的解&#x…

O2OA(翱途)开发平台 V9.1 即将发布,更安全、更高效、更开放

尊敬的O2OA(翱途)平台合作伙伴、用户以及亲爱的开发小伙伴们,O2OA(翱途)平台 V9.1将于7月3日正式发布,届时欢迎大家到O2OA官网部署下载及体验最新版本。新版本我们在如下方面做了更大的努力: 1.扩展数据库兼容性和功能范围:在O2OA…

基于Web技术的教育辅助系统设计与实现(SpringBoot MySQL)+文档

💗博主介绍💗:✌在职Java研发工程师、专注于程序设计、源码分享、技术交流、专注于Java技术领域和毕业设计✌ 温馨提示:文末有 CSDN 平台官方提供的老师 Wechat / QQ 名片 :) Java精品实战案例《700套》 2025最新毕业设计选题推荐…

汽车电子工程师入门系列——CAN 规范系列通读

我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 屏蔽力是信息过载时代一个人的特殊竞争力,任何消耗你的人和事,多看一眼都是你的不对。非必要不费力证明自己,无利益不试图说服别人,是精神上的节…

使用ps给gif动图抠图

目录 导入gif图片 打开时间轴 选择图片 魔棒抠图-初步抠图 套索抠图-精准抠图 导入gif图片 打开时间轴 因为gif动图实际上多张图片实现的效果,所以如果要给gif抠图,就得挨个给每个时间线的图片抠图 点击窗口->时间轴 选择图片 在时间轴上选择要…

以 Vue 3 项目为例,多个请求下如何全局封装 Loading 的展示与关闭?其中大有学问!

大家好,我是CodeQi! 项目开发中,Loading 的展示与关闭是非常关键的用户体验设计。 当我们的应用需要发起多个异步请求时,如何有效地管理全局 Loading 状态,保证用户在等待数据加载时能有明确的反馈,这是一个值得深入探讨的问题。 本文将以 Vue 3 项目为例,详细讲解如…

eventloop 事件循环机制 (猜答案)

// eventloop 事件循环机制// console.log(555);setTimeout(() > {console.log(666);})let p new Promise((resolve,reject)>{// 同步执行console.log(111);resolve();});// promise 的回调函数是异步的微任务p.then(v > {console.log(222);}, r > {console.log(r…

STMF4学习笔记(天空星)

前言:本篇笔记参考嘉立创文档,连接放在最后 #RTC相关概念定义 Real-Time Clock 缩写 RTC 翻译 实时时钟,是单片机片内外设的一种,作用于提供准确的时间还有日期,这个外设有独立的电源,当单片机停止供电…

Java同步包装器

通过 Collections.synchronizedList() 方法将一个普通的 ArrayList 包装成了线程安全的 List&#xff1a; import java.util.*;public class SynchronizedWrapperExample {public static void main(String[] args) {// 创建一个非线程安全的 ArrayListList<String> list…

AzureDataFactory Dataverse connector自动处理了分页问题(单次查询上限5000条的限制)

众所周知&#xff0c;在用fetch执行D365的查询时&#xff0c;单次的查询是5000条&#xff0c;如果超过5000条则需要自己处理分页&#xff0c;添加额外的处理逻辑&#xff0c;但在ADF中&#xff0c;Dataverse connector已经自动处理了分页&#xff0c;我们可以很简单的做个POC. …

计算机网络——数据链路层(点对点协议PPP)

点对点协议PPP的概述 对于点对点的链路&#xff0c;目前使用得最广泛的数据链路层协议是点对点协议 PPP (Point-to-Point Protocol)。 它主要应用于两个场景&#xff1a; 用户计算机与ISP之间的链路层协议就是点对点协议 PPP&#xff0c;1999年公布了回以在以太网上运行的PPP协…

【教程】lighttpd配置端口反向代理

转载请注明出处&#xff1a;小锋学长生活大爆炸[xfxuezhagn.cn] 如果本文帮助到了你&#xff0c;欢迎[点赞、收藏、关注]哦~ 1、修改配置文件&#xff1a; sudo vim /etc/lighttpd/lighttpd.conf2、先添加mod_proxy&#xff1a; 3、然后添加端口映射&#xff1a; 4、保存&…

耗材分类功能解析:智慧校园的必备利器

在智慧校园的资产管理架构中&#xff0c;耗材分类功能是确保日常运营物资有效管理的关键组成部分&#xff0c;它致力于提高耗材使用的效率和经济性。此功能通过智能化、精细化的管理手段&#xff0c;对校园内各种易耗品进行科学分类与跟踪。 耗材分类功能首先建立在对校园日常运…

C++ 实现QT信号槽

https://github.com/libsigcplusplus/libsigcplusplus #include <iostream>/* 在sigslot.h的420,将&#xff1a; //typedef sender_set::const_iterator const_iterator; 改为&#xff1a; //typedef typename sender_set::const_iterator const_iterator;#include <…