好的,我可以帮助您将每个步骤合并,并将其可视化成一个应用软件。我们将使用 Python 和 OpenCV 以及 PyQt5 来创建一个图形界面,整合您的所有图像处理步骤,方便您调整参数并实时查看效果。
以下是详细的实现步骤:
- 整合图像处理步骤:将您提供的各个图像处理函数合并到一个类中,以便管理和调用。
- 创建图形用户界面(GUI):使用 PyQt5 创建一个应用程序界面,包括图像加载、参数调整、实时预览和保存功能。
- 实现交互功能:添加滑块和按钮,允许用户调整参数并实时查看处理结果。
步骤 1:整合图像处理步骤
我们将所有的图像处理函数封装到一个 ImageProcessor 类中,方便在 GUI 中调用。
import cv2
import numpy as np
from PIL import Image
class ImageProcessor:
def __init__(self, image_path=None):
self.original_image = None
self.processed_image = None
if image_path:
self.load_image(image_path)
def load_image(self, image_path):
self.original_image = cv2.imread(image_path)
if self.original_image is None:
raise ValueError(f"无法加载图像: {image_path}")
self.processed_image = self.original_image.copy()
def adjust_sharpness(self, alpha=1.0):
"""调整图像的清晰度"""
blurred = cv2.GaussianBlur(self.processed_image, (0, 0), 3)
self.processed_image = cv2.addWeighted(self.processed_image, alpha, blurred, 1 - alpha, 0)
def adjust_contrast(self, beta=0):
"""调整图像的对比度"""
image = self.processed_image.astype(np.float32)
image = image * (1 + beta / 100.0)
self.processed_image = np.clip(image, 0, 255).astype(np.uint8)
def adjust_brightness(self, gamma=1.0):
"""调整图像的亮度"""
inv_gamma = 1.0 / gamma
table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
self.processed_image = cv2.LUT(self.processed_image, table)
def apply_blur(self, ksize=5):
"""应用模糊"""
self.processed_image = cv2.GaussianBlur(self.processed_image, (ksize, ksize), 0)
def convert_to_binary(self, threshold=128):
"""转换为二值图像"""
gray_image = cv2.cvtColor(self.processed_image, cv2.COLOR_BGR2GRAY)
_, self.processed_image = cv2.threshold(gray_image, threshold, 255, cv2.THRESH_BINARY)
# 转换为 BGR 格式以便显示
self.processed_image = cv2.cvtColor(self.processed_image, cv2.COLOR_GRAY2BGR)
def remove_noise(self):
"""消除噪点"""
gray_image = cv2.cvtColor(self.processed_image, cv2.COLOR_BGR2GRAY)
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(gray_image, cv2.MORPH_OPEN, kernel, iterations=1)
self.processed_image = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel, iterations=2)
# 转换为 BGR 格式以便显示
self.processed_image = cv2.cvtColor(self.processed_image, cv2.COLOR_GRAY2BGR)
def canny_edge_detection(self, threshold1=100, threshold2=200):
"""使用 Canny 算法进行边缘检测"""
gray_image = cv2.cvtColor(self.processed_image, cv2.COLOR_BGR2GRAY)
blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 1.4)
edges = cv2.Canny(blurred_image, threshold1, threshold2)
# 转换为 BGR 格式以便显示
self.processed_image = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
def reset(self):
"""重置为原始图像"""
self.processed_image = self.original_image.copy()
def save_image(self, output_path):
"""保存处理后的图像"""
cv2.imwrite(output_path, self.processed_image)
步骤 2:创建图形用户界面(GUI)
我们将使用 PyQt5 创建一个 GUI 应用程序界面。在界面中,我们将添加图像显示区域、参数调整控件和功能按钮,方便用户交互。
安装 PyQt5
在开始之前,请确保已安装 PyQt5:
pip install PyQt5
GUI 代码
以下是完整的 GUI 应用程序代码:
import sys
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QLabel, QSlider, QPushButton, QFileDialog,
QVBoxLayout, QHBoxLayout, QWidget, QGroupBox, QMessageBox
)
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QImage
import cv2
class ImageProcessingApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('图像处理应用程序')
self.processor = ImageProcessor()
self.init_ui()
def init_ui(self):
# 创建主部件和布局
main_widget = QWidget()
main_layout = QVBoxLayout()
# 图像显示标签
self.image_label = QLabel()
self.image_label.setAlignment(Qt.AlignCenter)
self.image_label.setFixedSize(800, 600)
main_layout.addWidget(self.image_label)
# 控件区域
controls_layout = QHBoxLayout()
# 左侧控件(加载、保存、重置)
left_controls = QVBoxLayout()
load_button = QPushButton('加载图像')
load_button.clicked.connect(self.load_image)
save_button = QPushButton('保存图像')
save_button.clicked.connect(self.save_image)
reset_button = QPushButton('重置图像')
reset_button.clicked.connect(self.reset_image)
left_controls.addWidget(load_button)
left_controls.addWidget(save_button)
left_controls.addWidget(reset_button)
# 右侧控件(处理参数调整)
right_controls = QVBoxLayout()
# 锐度调整
sharpness_group = QGroupBox('锐度调整')
sharpness_layout = QVBoxLayout()
self.sharpness_slider = QSlider(Qt.Horizontal)
self.sharpness_slider.setMinimum(0)
self.sharpness_slider.setMaximum(500)
self.sharpness_slider.setValue(100)
self.sharpness_slider.valueChanged.connect(self.update_image)
sharpness_layout.addWidget(self.sharpness_slider)
sharpness_group.setLayout(sharpness_layout)
right_controls.addWidget(sharpness_group)
# 对比度调整
contrast_group = QGroupBox('对比度调整')
contrast_layout = QVBoxLayout()
self.contrast_slider = QSlider(Qt.Horizontal)
self.contrast_slider.setMinimum(-100)
self.contrast_slider.setMaximum(100)
self.contrast_slider.setValue(0)
self.contrast_slider.valueChanged.connect(self.update_image)
contrast_layout.addWidget(self.contrast_slider)
contrast_group.setLayout(contrast_layout)
right_controls.addWidget(contrast_group)
# 亮度调整
brightness_group = QGroupBox('亮度调整')
brightness_layout = QVBoxLayout()
self.brightness_slider = QSlider(Qt.Horizontal)
self.brightness_slider.setMinimum(1)
self.brightness_slider.setMaximum(200)
self.brightness_slider.setValue(100)
self.brightness_slider.valueChanged.connect(self.update_image)
brightness_layout.addWidget(self.brightness_slider)
brightness_group.setLayout(brightness_layout)
right_controls.addWidget(brightness_group)
# 二值化阈值调整
binary_group = QGroupBox('二值化阈值')
binary_layout = QVBoxLayout()
self.binary_slider = QSlider(Qt.Horizontal)
self.binary_slider.setMinimum(0)
self.binary_slider.setMaximum(255)
self.binary_slider.setValue(128)
self.binary_slider.valueChanged.connect(self.update_image)
binary_layout.addWidget(self.binary_slider)
binary_group.setLayout(binary_layout)
right_controls.addWidget(binary_group)
# Canny 边缘检测参数调整
canny_group = QGroupBox('Canny 边缘检测')
canny_layout = QVBoxLayout()
self.canny_low_slider = QSlider(Qt.Horizontal)
self.canny_low_slider.setMinimum(0)
self.canny_low_slider.setMaximum(500)
self.canny_low_slider.setValue(100)
self.canny_low_slider.valueChanged.connect(self.update_image)
self.canny_high_slider = QSlider(Qt.Horizontal)
self.canny_high_slider.setMinimum(0)
self.canny_high_slider.setMaximum(500)
self.canny_high_slider.setValue(200)
self.canny_high_slider.valueChanged.connect(self.update_image)
canny_layout.addWidget(QLabel('阈值 1'))
canny_layout.addWidget(self.canny_low_slider)
canny_layout.addWidget(QLabel('阈值 2'))
canny_layout.addWidget(self.canny_high_slider)
canny_group.setLayout(canny_layout)
right_controls.addWidget(canny_group)
# 布局设置
controls_layout.addLayout(left_controls)
controls_layout.addLayout(right_controls)
main_layout.addLayout(controls_layout)
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
def load_image(self):
options = QFileDialog.Options()
file_name, _ = QFileDialog.getOpenFileName(self, "选择图像", "", "图像文件 (*.png;*.jpg;*.bmp)", options=options)
if file_name:
self.processor.load_image(file_name)
self.display_image(self.processor.original_image)
def save_image(self):
if self.processor.processed_image is not None:
options = QFileDialog.Options()
file_name, _ = QFileDialog.getSaveFileName(self, "保存图像", "", "图像文件 (*.png;*.jpg;*.bmp)", options=options)
if file_name:
self.processor.save_image(file_name)
QMessageBox.information(self, "保存成功", f"图像已保存到 {file_name}")
else:
QMessageBox.warning(self, "错误", "没有可保存的图像。")
def reset_image(self):
if self.processor.original_image is not None:
self.processor.reset()
self.display_image(self.processor.original_image)
self.reset_sliders()
else:
QMessageBox.warning(self, "错误", "请先加载图像。")
def update_image(self):
if self.processor.original_image is not None:
self.processor.reset()
# 获取参数值
alpha = self.sharpness_slider.value() / 100.0 # 锐度参数
beta = self.contrast_slider.value() # 对比度参数
gamma = self.brightness_slider.value() / 100.0 # 亮度参数
binary_threshold = self.binary_slider.value() # 二值化阈值
threshold1 = self.canny_low_slider.value() # Canny 阈值 1
threshold2 = self.canny_high_slider.value() # Canny 阈值 2
# 应用图像处理
self.processor.adjust_sharpness(alpha=alpha)
self.processor.adjust_contrast(beta=beta)
self.processor.adjust_brightness(gamma=gamma)
self.processor.convert_to_binary(threshold=binary_threshold)
self.processor.remove_noise()
self.processor.canny_edge_detection(threshold1=threshold1, threshold2=threshold2)
# 显示处理后的图像
self.display_image(self.processor.processed_image)
else:
QMessageBox.warning(self, "错误", "请先加载图像。")
def display_image(self, img):
"""在 QLabel 上显示图像"""
height, width = img.shape[:2]
bytes_per_line = 3 * width
cv2.cvtColor(img, cv2.COLOR_BGR2RGB, img)
q_img = QImage(img.data, width, height, bytes_per_line, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(q_img)
self.image_label.setPixmap(pixmap.scaled(
self.image_label.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
def reset_sliders(self):
"""重置滑块到默认值"""
self.sharpness_slider.setValue(100)
self.contrast_slider.setValue(0)
self.brightness_slider.setValue(100)
self.binary_slider.setValue(128)
self.canny_low_slider.setValue(100)
self.canny_high_slider.setValue(200)
def main():
app = QApplication(sys.argv)
window = ImageProcessingApp()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
代码说明
- 图像显示区域:使用
QLabel 来显示图像,支持图片的缩放和调整。
- 参数调整控件:使用滑块
QSlider 来调整锐度、对比度、亮度、二值化阈值和 Canny 边缘检测的参数。
- 功能按钮:包括加载图像、保存图像和重置图像的按钮。
- 事件连接:滑块的值改变时,调用
update_image 方法,实时更新处理后的图像。
步骤 3:运行应用程序
安装必要的库
请确保已安装以下 Python 库:
pip install opencv-python numpy pillow PyQt5
运行程序
将上述代码保存为 image_processing_app.py,然后在命令行中运行:
python image_processing_app.py
使用说明
- 加载图像:点击“加载图像”按钮,选择要处理的图像文件。
- 调整参数:使用右侧的滑块调整各个图像处理参数。滑块移动后,图像会自动更新。
- 保存图像:处理完成后,点击“保存图像”按钮,将结果保存到指定的文件。
- 重置图像:如果需要恢复到原始图像,点击“重置图像”按钮。
进一步改进
-
手动细微调整:如果需要在图像上进行手动绘制,可以在 GUI 中添加绘图功能,实现鼠标事件捕获,允许用户在图像上绘制。这需要对 QLabel 进行扩展,重新实现鼠标事件方法。
-
实时预览每个步骤:可以添加一个下拉菜单,允许用户选择查看某个特定处理步骤的结果,或者在界面上同时显示原始图像与处理后的图像。
-
参数配置保存:添加功能,允许用户保存当前参数设置,以便下次处理类似图像时直接加载参数配置。
总结
我们通过整合您的图像处理步骤,创建了一个带有图形用户界面的应用程序。这个应用程序允许您调整图像处理参数,并实时查看结果。您可以根据需要进一步定制和扩展此应用程序,以满足特定的需求。
如有任何问题或需要进一步的帮助,请随时告诉我!