用python设计一个gui界面,点击打开图片再点击相关按钮可以实现彩色转灰度图、调暗、调亮、直方图均衡化,图像平移、旋转、仿射、插值缩放、透视,加噪、去噪、图像分割、图像特征点匹配
时间: 2023-07-10 21:13:56 浏览: 82
好的,这是一个比较复杂的需求,需要使用Python的图像处理库和GUI库。我建议使用OpenCV和PyQt5。下面是一个简单的代码示例,可以实现部分功能,你可以根据需要进行修改和完善。
```python
import cv2
import numpy as np
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QMenu, QAction, QFileDialog, QSlider, QGridLayout, \
QHBoxLayout, QVBoxLayout, QWidget, QPushButton
class ImageViewer(QMainWindow):
def __init__(self):
super().__init__()
self.image = None
self.processed_image = None
self.filename = None
self.image_label = QLabel(self)
self.image_label.setAlignment(Qt.AlignCenter)
self.scroll_area = None
self.slider = None
self.slider_value = None
self.slider_label = None
self.slider_layout = None
self.init_ui()
def init_ui(self):
self.setWindowTitle('Image Viewer')
self.setGeometry(100, 100, 800, 600)
# menu bar
menubar = self.menuBar()
file_menu = menubar.addMenu('File')
open_action = QAction('Open', self)
open_action.triggered.connect(self.open_file)
file_menu.addAction(open_action)
# toolbar
toolbar = self.addToolBar('Toolbar')
gray_action = QAction('Gray', self)
gray_action.triggered.connect(self.gray)
toolbar.addAction(gray_action)
# sliders
self.slider = QSlider(Qt.Horizontal, self)
self.slider.setMinimum(-255)
self.slider.setMaximum(255)
self.slider_value = QLabel('0')
self.slider_label = QLabel('Brightness')
self.slider_layout = QHBoxLayout()
self.slider_layout.addWidget(self.slider_label)
self.slider_layout.addWidget(self.slider_value)
self.slider_layout.addWidget(self.slider)
self.slider.valueChanged.connect(self.brightness)
self.slider_layout.setAlignment(Qt.AlignCenter)
# buttons
button_layout = QHBoxLayout()
button_layout.setAlignment(Qt.AlignCenter)
dark_button = QPushButton('Dark', self)
dark_button.clicked.connect(self.dark)
light_button = QPushButton('Light', self)
light_button.clicked.connect(self.light)
button_layout.addWidget(dark_button)
button_layout.addWidget(light_button)
# layout
layout = QVBoxLayout()
layout.addWidget(self.image_label)
layout.addLayout(self.slider_layout)
layout.addLayout(button_layout)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
def open_file(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
self.filename, _ = QFileDialog.getOpenFileName(self, 'Open File', '', 'Images (*.png *.xpm *.jpg *.bmp *.gif *.webp)',
options=options)
if self.filename:
self.image = cv2.imread(self.filename)
self.processed_image = self.image.copy()
self.update_image()
def update_image(self):
if self.processed_image is not None:
height, width, channel = self.processed_image.shape
bytes_per_line = channel * width
q_image = QImage(self.processed_image.data, width, height, bytes_per_line, QImage.Format_RGB888)
pixmap = QPixmap.fromImage(q_image)
self.image_label.setPixmap(pixmap)
self.image_label.resize(pixmap.width(), pixmap.height())
self.image_label.adjustSize()
self.update()
def gray(self):
self.processed_image = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
self.update_image()
def dark(self):
self.processed_image = cv2.addWeighted(self.image, 1, np.zeros(self.image.shape, dtype=self.image.dtype), 0, -50)
self.update_image()
def light(self):
self.processed_image = cv2.addWeighted(self.image, 1, np.zeros(self.image.shape, dtype=self.image.dtype), 0, 50)
self.update_image()
def brightness(self):
value = self.slider.value()
self.slider_value.setText(str(value))
self.processed_image = cv2.addWeighted(self.image, 1, np.zeros(self.image.shape, dtype=self.image.dtype), 0, value)
self.update_image()
if __name__ == '__main__':
app = QApplication([])
window = ImageViewer()
window.show()
app.exec_()
```
这个代码示例可以打开图片,显示图片,并实现了灰度化、调暗、调亮、亮度调整的功能。你可以在这个基础上进行扩展,添加其他的功能。
阅读全文