import sys import cv2 from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QFileDialog, QLineEdit, QHBoxLayout, QVBoxLayout

class MainWindow(QWidget): def init(self): super().init()

    # 创建窗口控件
    self.image_label = QLabel(self)
    self.video_label = QLabel(self)
    self.open_image_button = QPushButton('打开图像', self)
    self.open_video_button = QPushButton('打开视频', self)
    self.canny_image_button = QPushButton('Canny检测图像', self)
    self.canny_video_button = QPushButton('Canny检测视频', self)
    self.image_threshold_layout = QHBoxLayout()
    self.video_threshold_layout = QHBoxLayout()
    self.image_threshold_lower_edit = QLineEdit()
    self.image_threshold_upper_edit = QLineEdit()
    self.video_threshold_lower_edit = QLineEdit()
    self.video_threshold_upper_edit = QLineEdit()
    self.image_threshold_layout.addWidget(QLabel('图像阈值下限:'))
    self.image_threshold_layout.addWidget(self.image_threshold_lower_edit)
    self.image_threshold_layout.addWidget(QLabel('图像阈值上限:'))
    self.image_threshold_layout.addWidget(self.image_threshold_upper_edit)
    self.video_threshold_layout.addWidget(QLabel('视频阈值下限:'))
    self.video_threshold_layout.addWidget(self.video_threshold_lower_edit)
    self.video_threshold_layout.addWidget(QLabel('视频阈值上限:'))
    self.video_threshold_layout.addWidget(self.video_threshold_upper_edit)

    # 设置控件位置和大小
    self.image_label.setGeometry(10, 10, 400, 400)
    self.video_label.setGeometry(420, 10, 400, 400)
    self.open_image_button.setGeometry(10, 420, 100, 30)
    self.open_video_button.setGeometry(120, 420, 100, 30)
    self.canny_image_button.setGeometry(10, 460, 100, 30)
    self.canny_video_button.setGeometry(120, 460, 100, 30)

    # 绑定按钮点击事件
    self.open_image_button.clicked.connect(self.open_image)
    self.open_video_button.clicked.connect(self.open_video)
    self.canny_image_button.clicked.connect(self.canny_image)
    self.canny_video_button.clicked.connect(self.canny_video)

    # 初始化图像和视频
    self.image = None
    self.cap = None

    # 初始化阈值
    self.image_threshold_lower = 100
    self.image_threshold_upper = 200
    self.video_threshold_lower = 100
    self.video_threshold_upper = 200

    # 绑定阈值输入框事件
    self.image_threshold_lower_edit.textChanged.connect(self.set_image_threshold_lower)
    self.image_threshold_upper_edit.textChanged.connect(self.set_image_threshold_upper)
    self.video_threshold_lower_edit.textChanged.connect(self.set_video_threshold_lower)
    self.video_threshold_upper_edit.textChanged.connect(self.set_video_threshold_upper)

    # 将阈值输入框添加到界面上
    main_layout = QVBoxLayout()
    main_layout.addLayout(self.image_threshold_layout)
    main_layout.addLayout(self.video_threshold_layout)
    main_layout.addWidget(self.image_label)
    main_layout.addWidget(self.video_label)
    main_layout.addWidget(self.open_image_button)
    main_layout.addWidget(self.open_video_button)
    main_layout.addWidget(self.canny_image_button)
    main_layout.addWidget(self.canny_video_button)
    self.setLayout(main_layout)

def open_image(self):
    # 打开图像文件
    file_name, _ = QFileDialog.getOpenFileName(self, '打开图像', '', 'Image Files(*.jpg *.png *.bmp)')

    if file_name:
        # 读取图像
        self.image = cv2.imread(file_name)

        # 将图像转换为QImage格式
        qimage = QImage(self.image.data, self.image.shape[1], self.image.shape[0], QImage.Format_RGB888).rgbSwapped()

        # 将QImage显示在界面上
        pixmap = QPixmap.fromImage(qimage)
        self.image_label.setPixmap(pixmap)

def open_video(self):
    # 打开视频文件
    file_name, _ = QFileDialog.getOpenFileName(self, '打开视频', '', 'Video Files(*.mp4 *.avi)')

    if file_name:
        # 打开视频
        self.cap = cv2.VideoCapture(file_name)

        # 显示第一帧图像
        ret, frame = self.cap.read()
        if ret:
            # 将图像转换为QImage格式
            qimage = QImage(frame.data, frame.shape[1], frame.shape[0], QImage.Format_RGB888).rgbSwapped()

            # 将QImage显示在界面上
            pixmap = QPixmap.fromImage(qimage)
            self.video_label.setPixmap(pixmap)

def canny_image(self):
    if self.image is not None:
        # Canny边缘检测
        edges = cv2.Canny(self.image, self.image_threshold_lower, self.image_threshold_upper)

        # 将边缘图像转换为QImage格式
        qimage = QImage(edges.data, edges.shape[1], edges.shape[0], QImage.Format_Grayscale8)

        # 将QImage显示在界面上
        pixmap = QPixmap.fromImage(qimage)
        self.image_label.setPixmap(pixmap)

def canny_video(self):
    if self.cap is not None:
        while True:
            # 读取视频帧
            ret, frame = self.cap.read()
            if not ret:
                break

            # Canny边缘检测
            edges = cv2.Canny(frame, self.video_threshold_lower, self.video_threshold_upper)

            # 将边缘图像转换为QImage格式
            qimage = QImage(edges.data, edges.shape[1], edges.shape[0], QImage.Format_Grayscale8)

            # 将QImage显示在界面上
            pixmap = QPixmap.fromImage(qimage)
            self.video_label.setPixmap(pixmap)

            # 等待100毫秒,等待界面更新
            cv2.waitKey(100)

def set_image_threshold_lower(self, text):
    try:
        self.image_threshold_lower = int(text)
    except ValueError:
        pass

def set_image_threshold_upper(self, text):
    try:
        self.image_threshold_upper = int(text)
    except ValueError:
        pass

def set_video_threshold_lower(self, text):
    try:
        self.video_threshold_lower = int(text)
    except ValueError:
        pass

def set_video_threshold_upper(self, text):
    try:
        self.video_threshold_upper = int(text)
    except ValueError:
        pass

if name == 'main': app = QApplication(sys.argv) main_window = MainWindow() main_window.show() sys.exit(app.exec_()

import sysimport cv2from PyQt5QtGui import QImage QPixmapfrom PyQt5QtWidgets import QApplication QWidget QLabel QPushButton QFileDialogclass MainWindowQWidget def __init__self super__init__

原文地址: http://www.cveoy.top/t/topic/ckG6 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录