用pyqt5写一个图像对比评价的页面
时间: 2023-12-02 09:06:32 浏览: 93
以下是一个简单的图像对比评价页面的示例代码,使用PyQt5编写:
```python
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QHBoxLayout, QVBoxLayout, QLabel, QPushButton
from PyQt5.QtGui import QPixmap, QImage, QPainter, QColor
from PyQt5.QtCore import Qt
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("图像对比评价页面")
self.setFixedSize(800, 600)
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
hbox = QHBoxLayout()
# 显示左侧图像的标签
self.left_image_label = QLabel()
self.left_image_label.setAlignment(Qt.AlignCenter)
hbox.addWidget(self.left_image_label)
# 显示右侧图像的标签
self.right_image_label = QLabel()
self.right_image_label.setAlignment(Qt.AlignCenter)
hbox.addWidget(self.right_image_label)
central_widget.setLayout(hbox)
# 显示对比结果的标签
self.result_label = QLabel()
self.result_label.setAlignment(Qt.AlignCenter)
self.result_label.setFixedHeight(100)
vbox = QVBoxLayout()
vbox.addWidget(self.result_label)
central_widget.layout().addLayout(vbox)
# 按钮布局
button_hbox = QHBoxLayout()
# 加载左侧图像的按钮
self.left_load_button = QPushButton("加载左侧图像")
self.left_load_button.clicked.connect(self.load_left_image)
button_hbox.addWidget(self.left_load_button)
# 加载右侧图像的按钮
self.right_load_button = QPushButton("加载右侧图像")
self.right_load_button.clicked.connect(self.load_right_image)
button_hbox.addWidget(self.right_load_button)
# 对比图像并显示结果的按钮
self.compare_button = QPushButton("对比图像")
self.compare_button.clicked.connect(self.compare_images)
button_hbox.addWidget(self.compare_button)
vbox.addLayout(button_hbox)
# 初始化图像数据
self.left_image_data = None
self.right_image_data = None
def load_left_image(self):
filename, _ = QFileDialog.getOpenFileName(self, "选择左侧图像", "", "Images (*.png *.xpm *.jpg *.bmp)")
if filename:
self.left_image_data = QImage(filename)
self.left_image_label.setPixmap(QPixmap.fromImage(self.left_image_data))
def load_right_image(self):
filename, _ = QFileDialog.getOpenFileName(self, "选择右侧图像", "", "Images (*.png *.xpm *.jpg *.bmp)")
if filename:
self.right_image_data = QImage(filename)
self.right_image_label.setPixmap(QPixmap.fromImage(self.right_image_data))
def compare_images(self):
if self.left_image_data is None or self.right_image_data is None:
self.result_label.setText("请先加载两张图像")
return
# 比较图像像素值的差异
diff = 0
for i in range(self.left_image_data.width()):
for j in range(self.left_image_data.height()):
left_color = QColor(self.left_image_data.pixel(i, j))
right_color = QColor(self.right_image_data.pixel(i, j))
diff += abs(left_color.red() - right_color.red())
diff += abs(left_color.green() - right_color.green())
diff += abs(left_color.blue() - right_color.blue())
# 显示对比结果
self.result_label.setText("图像差异值为: {}".format(diff))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
运行示例代码,会弹出一个窗口,包含两个用于显示图像的标签和三个按钮,分别用于加载左侧图像、加载右侧图像和对比图像,并在下方显示对比结果。用户可以点击“加载左侧图像”按钮和“加载右侧图像”按钮,选择两张图像进行加载。加载完成后,用户可以点击“对比图像”按钮,程序会计算两张图像的像素值差异,并在下方显示对比结果。
阅读全文