You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.3 KiB

6 months ago
import os
import sys
import cv2 as cv
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QFileDialog, QMainWindow
from surface import (Ui_MainWindow)
os.chdir(sys.path[0])
class PyQtMainEntry(QMainWindow, Ui_MainWindow):
def __init__(self):
6 months ago
super().__init__() # 调用父类的初始化函数
self.setupUi(self) # 设置用户界面
# 初始化摄像头并尝试打开默认尝试打开摄像头索引为0
6 months ago
self.camera = cv.VideoCapture(0)
6 months ago
self.is_camera_opened = False # 标记摄像头是否成功打开
6 months ago
6 months ago
# 创建一个定时器并设置其触发间隔为10毫秒
6 months ago
self._timer = QtCore.QTimer(self)
6 months ago
self._timer.timeout.connect(self._queryFrame) # 定时器触发时调用_queryFrame函数
self._timer.setInterval(10) # 设置定时器触发间隔
6 months ago
6 months ago
def btnOpenCamera_Clicked(self):
6 months ago
6 months ago
# 切换摄像头的打开状态
6 months ago
self.is_camera_opened = ~self.is_camera_opened
if self.is_camera_opened:
6 months ago
# 当摄像头打开时,更改按钮文本为关闭摄像头,并启动定时器
6 months ago
self.btnOpenCamera.setText("关闭摄像头")
self._timer.start()
else:
6 months ago
# 当摄像头关闭时,更改按钮文本为打开摄像头,并停止定时器
6 months ago
self.btnOpenCamera.setText("打开摄像头")
self._timer.stop()
def btnCapture_Clicked(self):
if not self.is_camera_opened:
6 months ago
# 如果相机未打开,则直接返回,不做任何操作
6 months ago
return
6 months ago
# 捕获图像
6 months ago
self.captured = self.frame
6 months ago
# 获取捕获图像的行数、列数和通道数
6 months ago
rows, cols, channels = self.captured.shape
6 months ago
# 计算每行字节数
6 months ago
bytesPerLine = channels * cols
6 months ago
# 使用捕获的图像数据创建QImage对象
6 months ago
QImg = QImage(self.captured.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
6 months ago
# 将QImage对象转换为QPixmap并按比例缩放以适应标签的大小然后设置到标签上
6 months ago
self.labelCapture.setPixmap(QPixmap.fromImage(QImg).scaled(
self.labelCapture.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
def btnReadImage_Clicked(self):
6 months ago
# 使用QFileDialog获取选择的图像文件名
# ,_是占位符用来忽略返回值比如这里面会返回文件名和文件类型使用占位符可以忽略文件类型
6 months ago
filename, _ = QFileDialog.getOpenFileName(self, '打开图片')
if filename:
6 months ago
# 通过文件名读取图像并转换为RGB格式
6 months ago
self.captured = cv.imread(str(filename))
self.captured = cv.cvtColor(self.captured, cv.COLOR_BGR2RGB)
6 months ago
# 获取图像的尺寸信息用于创建QImage
6 months ago
rows, cols, channels = self.captured.shape
bytesPerLine = channels * cols
6 months ago
# 将OpenCV图像转换为QImage格式
6 months ago
QImg = QImage(self.captured.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
6 months ago
# 将QImage设置为标签的图片并保持原始比例进行缩放
6 months ago
self.labelCapture.setPixmap(QPixmap.fromImage(QImg).scaled(
self.labelCapture.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
def btnGray_Clicked(self):
6 months ago
if not hasattr(self, "captured"):
return
self.cpatured = cv.cvtColor(self.captured, cv.COLOR_RGB2GRAY)
rows, columns = self.cpatured.shape
bytesPerLine = columns
QImg = QImage(self.cpatured.data, columns, rows, bytesPerLine, QImage.Format_Indexed8)
self.labelResult.setPixmap(QPixmap.fromImage(QImg).scaled(
self.labelResult.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
6 months ago
def btnThreshold_Clicked(self):
if not hasattr(self, "captured"):
return
6 months ago
_, self.cpatured = cv.threshold(
self.cpatured, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)
rows, columns = self.cpatured.shape
bytesPerLine = columns
QImg = QImage(self.cpatured.data, columns, rows, bytesPerLine, QImage.Format_Indexed8)
self.labelResult.setPixmap(QPixmap.fromImage(QImg).scaled(
self.labelResult.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
6 months ago
@QtCore.pyqtSlot()
def _queryFrame(self):
6 months ago
"""
从相机读取一帧图像并将其显示在标签上
"""
# 从相机读取图像
6 months ago
ret, self.frame = self.camera.read()
6 months ago
# 获取图像的尺寸和每行字节数
6 months ago
img_rows, img_cols, channels = self.frame.shape
bytesPerLine = channels * img_cols
6 months ago
# 将图像从BGR转换为RGB格式
6 months ago
cv.cvtColor(self.frame, cv.COLOR_BGR2RGB, self.frame)
6 months ago
# 创建QImage对象
6 months ago
QImg = QImage(self.frame.data, img_cols, img_rows, bytesPerLine, QImage.Format_RGB888)
6 months ago
# 将QImage设置为标签的图片并保持宽高比
6 months ago
self.labelCamera.setPixmap(QPixmap.fromImage(QImg).scaled(
self.labelCamera.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
6 months ago
6 months ago
def btnDenoising_Clicked(self):
if not hasattr(self, "captured"):
return
6 months ago
self.cpatured = cv.fastNlMeansDenoising(self.cpatured, None, 10, 7, 21)
rows, columns = self.cpatured.shape
bytesPerLine = columns
6 months ago
6 months ago
QImg = QImage(self.cpatured.data, columns, rows, bytesPerLine, QImage.Format_Indexed8)
self.labelResult.setPixmap(QPixmap.fromImage(QImg).scaled(
self.labelResult.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
6 months ago
6 months ago
def btnSaveImage_Clicked(self):
if not hasattr(self, "captured"): # 检查是否已捕获图像
return
filename, _ = QFileDialog.getSaveFileName(self, '保存图片') # 弹出保存文件对话框
if filename: # 如果用户选择了保存路径
cv.imwrite(str(filename), self.captured) # 将捕获的图像保存到选择的路径
6 months ago
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = PyQtMainEntry()
window.show()
sys.exit(app.exec_())