前言
这个Python没学过,写的是真的不方便,有很多问题还没解决,暂时不想写了,感兴趣的同学可以完善一下。设计的思路就是摆几个控件然后将对应的函数实现,这个Python的坐标放置以及控件的大小我没弄懂,算出来不对劲,完全是按照自己的电脑摆的,然后窗口放置是和自己电脑成比例而且居中放置,本来还想把学过的图像处理全部封装到一个程序里面,结果还是卡在了一个地方,这个图片的显示是相对于之前图片的宽高成比例,所以还是看你太大或者太小展示,然后这个窗口焦点获取还没调整好,每次都要把图片窗口关闭才能选择新的图片转换。
灰度变换(python)-CSDN博客https://blog.csdn.net/weixin_64066303/article/details/136698756?spm=1001.2014.3001.5501
代码
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
from tkinter import ttk
import cv2
import numpy as np# 定义界面的框架
def Frame():# 创建窗口window = tk.Tk()window.title('图像处理')# 获取屏幕大小screen_width = window.winfo_screenwidth()screen_height = window.winfo_screenheight()# 计算所需窗口的相对大小relative_width = int(screen_width * 0.7)relative_height = int(screen_height * 0.7)# 打印宽和高print(relative_width, relative_height)# 计算窗口位于屏幕中央的坐标x = (screen_width - relative_width) // 2y = (screen_height - relative_height) // 2# 设置窗口大小(用f{}格式化字符串)window.geometry(f"{relative_width}x{relative_height}+{x}+{y}")# 生成界面需要的框架module(window)# 运行窗口window.mainloop()# 定义界面展示的组件
def module(window):# 声明全局变量img,comboglobal imgglobal combo# 在窗口中添加标签ystp = tk.Label(window, text="原始图片:", font=("楷体", 45), width=20, height=2)# 文本靠左对齐ystp.place(x=0, y=0)# 定义按钮button = tk.Button(window, text="打开文件", command=open_file, font=("楷体", 30), width=15, height=2)button.place(x=650, y=10)# 创建标签,用于显示图片img = tk.Label(window)img.place(x=387, y=150)# 创建下拉框combo = ttk.Combobox(window, font=("楷体", 25))combo["values"] = ("灰度反转", "对数变换", "伽马变换")combo.place(x=100, y=500)# 默认选中第一个选项combo.current(0)# 定义按钮button = tk.Button(window, text="开始转换", command=transition, font=("楷体", 30), width=15, height=2)button.place(x=650, y=450)# 文件打开
def open_file():# 声明全局变量photoglobal photo, file_path# 打开文件对话框,让用户选择图片文件(限定可以打开的文件格式)file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.jpeg;*.png;*.gif")])if file_path:print("选择的文件路径为:", file_path)# 加载并显示图片image = Image.open(file_path)# 调整图片的大小image = image.resize((300, 300))photo = ImageTk.PhotoImage(image)# 创建标签,显示图片img.config(image=photo)else:print("文件未找到或打开失败")# 图片转换
def transition():print("开始转换")# 读取当前下拉框的选项# print(combo.get())# opencv读取图像image = cv2.imread(file_path, 1)# 检查图像是否成功加载if image is not None:# 将图像灰度化灰度化# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)option = combo.get()# 显示图像# 尺寸缩小一半image = cv2.resize(image, None, fx=0.3, fy=0.3)cv2.imshow('Original Image', image)# cv2.imshow('Gray', gray)if (option == "灰度反转"):# 灰度反转inverted_image = gray_inversion(image)# 尺寸缩小一半# inverted_image = cv2.resize(inverted_image, None, fx=0.5, fy=0.5)cv2.imshow('Inverted Image', inverted_image)elif (option == "对数变换"):# 对数变换transformed_image = log_transform(image, 20)# 尺寸缩小一半# transformed_image = cv2.resize(transformed_image, None, fx=0.5, fy=0.5)cv2.imshow('Log Transformed Image', transformed_image)elif (option == "伽马变换"):# 伽马变换corrected_image = gamma_correction(image, 10, 255)# 尺寸缩小一半# corrected_image = cv2.resize(corrected_image, None, fx=0.5, fy=0.5)cv2.imshow('Gamma Corrected Image', corrected_image)# 窗口保持cv2.waitKey(0)else:print("Failed to load image.")# 灰度反转 s=L-1-r
def gray_inversion(image):# 最大图像的灰度值减去原图像inverted_image = 255 - imagereturn inverted_image# 对数变换 s=c*log(1+r)
def log_transform(image, c=1):# 对图像进行对数变换transformed_image = c * np.log1p(image)# 将结果缩放到0~255transformed_image = np.uint8(transformed_image)return transformed_image# 伽马变换 s=c*r^y
def gamma_correction(image, gamma=1.0, c=1.0):# 将图像转换为浮点类型image = image.astype(np.float32) / 255.0# 应用伽马变换corrected_image = c * np.power(image, gamma)# 将结果缩放到0~255之间corrected_image = np.uint8(corrected_image)return corrected_imageif __name__ == '__main__':# 生成初始界面Frame()
目前又增加了直方图的均衡化,但是目前只能输出灰度图。
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
from tkinter import ttk
import cv2
import numpy as np# 定义界面的框架
def Frame():# 创建窗口window = tk.Tk()window.title('图像处理')# 获取屏幕大小screen_width = window.winfo_screenwidth()screen_height = window.winfo_screenheight()# 计算所需窗口的相对大小relative_width = int(screen_width * 0.7)relative_height = int(screen_height * 0.7)# 打印宽和高print(relative_width, relative_height)# 计算窗口位于屏幕中央的坐标x = (screen_width - relative_width) // 2y = (screen_height - relative_height) // 2# 设置窗口大小(用f{}格式化字符串)window.geometry(f"{relative_width}x{relative_height}+{x}+{y}")# 生成界面需要的框架module(window)# 运行窗口window.mainloop()# 定义界面展示的组件
def module(window):# 声明全局变量img,comboglobal combo, img# 在窗口中添加标签ystp = tk.Label(window, text="原始图片:", font=("楷体", 45), width=20, height=2)# 文本靠左对齐ystp.place(x=0, y=0)# 定义按钮button = tk.Button(window, text="打开文件", command=open_file, font=("楷体", 30), width=15, height=2)button.place(x=650, y=10)# 创建标签,用于显示图片img = tk.Label(window)img.place(x=387, y=150)# 创建下拉框combo = ttk.Combobox(window, font=("楷体", 25))combo["values"] = ("灰度反转", "对数变换", "伽马变换", "直方图均衡化")combo.place(x=100, y=500)# 默认选中第一个选项combo.current(0)# 定义按钮button = tk.Button(window, text="开始转换", command=transition, font=("楷体", 30), width=15, height=2)button.place(x=650, y=450)# 文件打开
def open_file():# 声明全局变量photoglobal photo, file_path# 打开文件对话框,让用户选择图片文件(限定可以打开的文件格式)file_path = filedialog.askopenfilename(filetypes=[("Image files", "*.jpg;*.jpeg;*.png;*.gif")])if file_path:print("选择的文件路径为:", file_path)# 加载并显示图片image = Image.open(file_path)# 调整图片的大小image = image.resize((300, 300))photo = ImageTk.PhotoImage(image)# 创建标签,显示图片img.config(image=photo)else:print("文件未找到或打开失败")# 图片转换
def transition():print("开始转换")# 读取当前下拉框的选项# print(combo.get())# opencv读取图像image = cv2.imread(file_path, 0)# 检查图像是否成功加载if image is not None:# 将图像灰度化灰度化# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)option = combo.get()# 显示图像# 尺寸缩小一半image = cv2.resize(image, None, fx=0.3, fy=0.3)cv2.imshow('Original Image', image)# cv2.imshow('Gray', gray)if (option == "灰度反转"):# 灰度反转inverted_image = gray_inversion(image)# 尺寸缩小一半# inverted_image = cv2.resize(inverted_image, None, fx=0.5, fy=0.5)cv2.imshow('Inverted Image', inverted_image)elif (option == "对数变换"):# 对数变换transformed_image = log_transform(image, 20)# 尺寸缩小一半# transformed_image = cv2.resize(transformed_image, None, fx=0.5, fy=0.5)cv2.imshow('Log Transformed Image', transformed_image)elif (option == "伽马变换"):# 伽马变换corrected_image = gamma_correction(image, 10, 255)# 尺寸缩小一半# corrected_image = cv2.resize(corrected_image, None, fx=0.5, fy=0.5)cv2.imshow('Gamma Corrected Image', corrected_image)elif (option == "直方图均衡化"):equal_image = equalizehist(image)cv2.imshow('equalizehist', equal_image)# 窗口保持cv2.waitKey(0)else:print("Failed to load image.")# 灰度反转 s=L-1-r
def gray_inversion(image):# 最大图像的灰度值减去原图像inverted_image = 255 - imagereturn inverted_image# 对数变换 s=c*log(1+r)
def log_transform(image, c=1):# 对图像进行对数变换transformed_image = c * np.log1p(image)# 将结果缩放到0~255transformed_image = np.uint8(transformed_image)return transformed_image# 伽马变换 s=c*r^y
def gamma_correction(image, gamma=1.0, c=1.0):# 将图像转换为浮点类型image = image.astype(np.float32) / 255.0# 应用伽马变换corrected_image = c * np.power(image, gamma)# 将结果缩放到0~255之间corrected_image = np.uint8(corrected_image)return corrected_image# 直方图均衡化
def equalizehist(image, L=256):# 计算直方图均衡前后的直方图hist_color = cv2.equalizeHist(image)return hist_color# 分割RGB图像
# def equalize_hist_color(image):
# # 使用cv2.split()分割RGB图像
# channels = cv2.split(image)
# eq_channels = []
# # 将cv2.equalizeHist()函数应用与每个通道
# for ch in channels:
# eq_channels.append(cv2.equalizeHist(ch))
# # 使用cv2.merge()合并结果通道
# eq_image = cv2.merge(eq_channels)
# return eq_imageif __name__ == '__main__':# 生成初始界面Frame()