好的,我可以幫你將這段程式碼改寫成一個帶有視覺化界面的軟體,讓你可以通過圖形介面來調整參數,並即時查看圖像處理的效果。
以下我將使用 Python 的 Tkinter 庫來創建一個簡單的圖形介面應用程式。同時,繼續使用 OpenCV 進行圖像處理。
完整程式碼:
import cv2
import numpy as np
import tkinter as tk
from tkinter import filedialog
from tkinter import HORIZONTAL
from PIL import Image, ImageTk
import os
def adjust_sharpness(image, alpha=1.9):
"""調整圖像的清晰度"""
blurred = cv2.GaussianBlur(image, (0, 0), 3)
sharpened = cv2.addWeighted(image, alpha, blurred, 1 - alpha, 0)
return sharpened
def adjust_contrast(image, beta=90):
"""調整圖像的對比度"""
image = image.astype(np.float32)
image = image * (1 + beta / 100.0)
image = np.clip(image, 0, 255).astype(np.uint8)
return image
def adjust_brightness(image, gamma=1.0):
"""調整圖像的亮度"""
if gamma == 0:
gamma = 0.01 # 防止除以零
inv_gamma = 1.0 / gamma
table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
def apply_blur(image, ksize=5):
"""應用模糊"""
if ksize % 2 == 0:
ksize += 1 # 確保 ksize 是奇數
return cv2.GaussianBlur(image, (ksize, ksize), 0)
def process_image():
global processed_image, display_image
# 獲取當前參數
alpha = sharpness_scale.get()
beta = contrast_scale.get()
gamma = brightness_scale.get()
ksize = blur_scale.get()
# 應用圖像處理
processed_image = adjust_sharpness(original_image, alpha=alpha)
processed_image = adjust_contrast(processed_image, beta=beta)
processed_image = adjust_brightness(processed_image, gamma=gamma)
processed_image = apply_blur(processed_image, ksize=ksize)
# 將圖像從 BGR 轉換為 RGB,並轉換為 PIL 格式
img_rgb = cv2.cvtColor(processed_image, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(img_rgb)
# 將圖像調整到合適的顯示尺寸
im_display = im_pil.resize((400, 300))
imgtk = ImageTk.PhotoImage(image=im_display)
# 更新顯示的圖像
display_image.configure(image=imgtk)
display_image.image = imgtk
def load_image():
global original_image
file_path = filedialog.askopenfilename()
if file_path:
image = cv2.imread(file_path)
if image is None:
tk.messagebox.showerror("錯誤", "無法加載圖像")
return
original_image = image
process_image()
def save_image():
if processed_image is None:
tk.messagebox.showerror("錯誤", "沒有可保存的圖像")
return
file_path = filedialog.asksaveasfilename(defaultextension=".png")
if file_path:
cv2.imwrite(file_path, processed_image)
tk.messagebox.showinfo("保存成功", f"圖像已保存到 {file_path}")
# 初始化Tkinter窗口
root = tk.Tk()
root.title("圖像調整工具")
root.geometry("450x600")
# 初始化全局變數
original_image = None
processed_image = None
# 加載圖像按鈕
load_button = tk.Button(root, text="加載圖像", command=load_image)
load_button.pack(pady=10)
# 清晰度調節
sharpness_label = tk.Label(root, text="清晰度 (alpha)")
sharpness_label.pack()
sharpness_scale = tk.Scale(root, from_=0.0, to=5.0, resolution=0.1, orient=HORIZONTAL, command=lambda x: process_image() if original_image is not None else None)
sharpness_scale.set(1.9)
sharpness_scale.pack()
# 對比度調節
contrast_label = tk.Label(root, text="對比度 (beta)")
contrast_label.pack()
contrast_scale = tk.Scale(root, from_=-100, to=100, resolution=1, orient=HORIZONTAL, command=lambda x: process_image() if original_image is not None else None)
contrast_scale.set(0)
contrast_scale.pack()
# 亮度調節
brightness_label = tk.Label(root, text="亮度 (gamma)")
brightness_label.pack()
brightness_scale = tk.Scale(root, from_=0.1, to=5.0, resolution=0.1, orient=HORIZONTAL, command=lambda x: process_image() if original_image is not None else None)
brightness_scale.set(1.0)
brightness_scale.pack()
# 模糊程度調節
blur_label = tk.Label(root, text="模糊程度 (ksize)")
blur_label.pack()
blur_scale = tk.Scale(root, from_=1, to=31, resolution=2, orient=HORIZONTAL, command=lambda x: process_image() if original_image is not None else None)
blur_scale.set(1)
blur_scale.pack()
# 保存圖像按鈕
save_button = tk.Button(root, text="保存圖像", command=save_image)
save_button.pack(pady=10)
# 圖像顯示區域
display_image = tk.Label(root)
display_image.pack()
# 運行主循環
root.mainloop()
功能說明:
- 加載圖像:點擊「加載圖像」按鈕,選擇需要處理的圖像文件。
- 調節參數:通過滑動條調整清晰度(alpha)、對比度(beta)、亮度(gamma)和模糊程度(ksize)。
- 即時顯示:每當調節參數時,圖像將自動更新,實時顯示處理效果。
- 保存圖像:點擊「保存圖像」按鈕,將當前處理後的圖像保存到指定位置。
注意事項:
- ksize:模糊核的大小必須為奇數,程式中已確保這一點。
- gamma:亮度調節時,gamma 不能為零,已在程式中避免。
- 圖像尺寸:為了適應介面顯示,圖像被調整為 400x300 的尺寸。
所需外部庫:
- OpenCV(處理圖像):
pip install opencv-python
- Pillow(圖像格式轉換):
pip install Pillow
- Tkinter(圖形介面):大多數 Python 發行版已內置,如果沒有,請安裝相應版本
執行程式:
- 確保已安裝所有所需的庫。
- 將上述程式碼保存為一個
.py 文件,例如 image_adjust_tool.py。
- 在命令行中運行程式:
python image_adjust_tool.py
- 介面將彈出,按照提示進行操作。
希望這個圖形介面軟體能滿足你的需求,讓你方便地調節圖像參數並實時查看效果。如果有任何問題,請隨時告訴我!