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.

127 lines
4.2 KiB

6 months ago
import os
import sys
import cv2 as cv
import numpy as np
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):
super().__init__()
self.setupUi(self)
self.camera = cv.VideoCapture(0)
self.is_camera_opened = False
self._timer = QtCore.QTimer(self)
self._timer.timeout.connect(self._queryFrame)
6 months ago
self._timer.setInterval(10)
6 months ago
def btnOpenCamera_Clicked(self):
self.is_camera_opened = ~self.is_camera_opened
if self.is_camera_opened:
self.btnOpenCamera.setText("关闭摄像头")
self._timer.start()
else:
self.btnOpenCamera.setText("打开摄像头")
self._timer.stop()
def btnCapture_Clicked(self):
if not self.is_camera_opened:
return
self.captured = self.frame
rows, cols, channels = self.captured.shape
bytesPerLine = channels * cols
QImg = QImage(self.captured.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
self.labelCapture.setPixmap(QPixmap.fromImage(QImg).scaled(
self.labelCapture.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
def btnReadImage_Clicked(self):
filename, _ = QFileDialog.getOpenFileName(self, '打开图片')
if filename:
self.captured = cv.imread(str(filename))
self.captured = cv.cvtColor(self.captured, cv.COLOR_BGR2RGB)
rows, cols, channels = self.captured.shape
bytesPerLine = channels * cols
QImg = QImage(self.captured.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
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):
ret, self.frame = self.camera.read()
img_rows, img_cols, channels = self.frame.shape
bytesPerLine = channels * img_cols
cv.cvtColor(self.frame, cv.COLOR_BGR2RGB, self.frame)
QImg = QImage(self.frame.data, img_cols, img_rows, bytesPerLine, QImage.Format_RGB888)
self.labelCamera.setPixmap(QPixmap.fromImage(QImg).scaled(
self.labelCamera.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))
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
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = PyQtMainEntry()
window.show()
sys.exit(app.exec_())