麻瓜大憨批
2022-06-22 10:57
采纳率: 0%
浏览 13

textBrowser控件如何显示图片处理后的数字结果

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图

import sys
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import QFileDialog, QMainWindow
from demo import Ui_MainWindow

class PyQtMainEntry(QMainWindow, Ui_MainWindow):
def init(self):

    super().__init__()
    self.setupUi(self)

    self.camera = cv2.VideoCapture(0)
    self.is_camera_opened = False  # 摄像头有没有打开标记

    # 定时器:30ms捕获一帧
    self._timer = QtCore.QTimer(self)
    self._timer.timeout.connect(self._queryFrame)
    self._timer.setInterval(30)

def Button_OpenCamera_Clicked(self):   #打开和关闭摄像头
    self.is_camera_opened = ~self.is_camera_opened
    if self.is_camera_opened:
        self.Button_OpenCamera.setText("关闭摄像头")
        self._timer.start()
    else:
        self.Button_OpenCamera.setText("打开摄像头")
        self._timer.stop()

def Button_Capture_Clicked(self):#        捕获图片

    # 摄像头未打开,不执行任何操作
    if not self.is_camera_opened:
        return

    self.captured = self.frame
    # 后面这几行代码几乎都一样,可以尝试封装成一个函数
    rows, cols, channels = self.captured.shape
    bytesPerLine = channels * cols
    # Qt显示图片时,需要先转换成QImgage类型
    QImg = QImage(self.captured.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
    self.label_Capture.setPixmap(QPixmap.fromImage(QImg).scaled(self.label_Capture.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))

def Button_ReadImage_Clicked(self):#从本地读取图片 文件路径不能有中文
    # 打开文件选取对话框
    filename, _ = QFileDialog.getOpenFileName(self, '打开图片')
    if filename:
        self.captured = cv2.imread(str(filename))
        # OpenCV图像以BGR通道存储,显示时需要从BGR转到RGB
        self.captured = cv2.cvtColor(self.captured, cv2.COLOR_BGR2RGB)

        rows, cols, channels = self.captured.shape
        bytesPerLine = channels * cols
        QImg = QImage(self.captured.data, cols, rows, bytesPerLine, QImage.Format_RGB888)
        self.label_Capture.setPixmap(QPixmap.fromImage(QImg).scaled(
            self.label_Capture.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))

def Button_Gray_Clicked(self):# 灰度化
    # 如果没有捕获图片,则不执行操作
    if not hasattr(self, "captured"):
        return
    self.cpatured = cv2.cvtColor(self.captured, cv2.COLOR_RGB2GRAY)
    rows, columns = self.cpatured.shape
    bytesPerLine = columns
    # 灰度图是单通道,所以需要用Format_Indexed8
    QImg = QImage(self.cpatured.data, columns, rows, bytesPerLine, QImage.Format_Indexed8)
    self.label_Result.setPixmap(QPixmap.fromImage(QImg).scaled(
        self.label_Result.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))

def Button_Threshold_Clicked(self):# Otsu自动阈值分割
    if not hasattr(self, "captured"):
        return

    _, self.cpatured = cv2.threshold(self.cpatured, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    rows, columns = self.cpatured.shape
    bytesPerLine = columns
    # 阈值分割图也是单通道,也需要用Format_Indexed8
    QImg = QImage(self.cpatured.data, columns, rows, bytesPerLine, QImage.Format_Indexed8)
    self.label_Result.setPixmap(QPixmap.fromImage(QImg).scaled(
        self.label_Result.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))

def Button_jisaun_Clicked(self):
    if not hasattr(self, "captured"):
        return
    rows,columns = self.cpatured.shape
    balack=0
    white = 0
    for i in range(rows):
        for j in range(columns):
            if self.cpatured[i, j] == 0:
                balack += 1
            else:
                white += 1
    rate = white/(rows*columns)
    self.textBrowser.setText(rate,"占空比: round(rate*100,2)'%'" )

@QtCore.pyqtSlot()
def _queryFrame(self):   #循环捕获图片
    ret, self.frame = self.camera.read()
    img_rows, img_cols, channels = self.frame.shape
    bytesPerLine = channels * img_cols

    cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB, self.frame)
    QImg = QImage(self.frame.data, img_cols, img_rows, bytesPerLine, QImage.Format_RGB888)
    self.label_Camera.setPixmap(QPixmap.fromImage(QImg).scaled(self.label_Camera.size(), Qt.KeepAspectRatio, Qt.SmoothTransformation))

if name == "main":
app = QtWidgets.QApplication(sys.argv)
window = PyQtMainEntry()
window.show()
sys.exit(app.exec_())

运行结果及报错内容

img

我的解答思路和尝试过的方法
我想要达到的结果

在textBrowsert上显示计算结果
计算函数如下
def Button_jisaun_Clicked(self):
if not hasattr(self, "captured"):
return
rows,columns = self.cpatured.shape
balack=0
white = 0
for i in range(rows):
for j in range(columns):
if self.cpatured[i, j] == 0:
balack += 1
else:
white += 1
rate = white/(rowscolumns)
self.textBrowser.setText(rate,"占空比: round(rate
100,2)'%'" )

1条回答 默认 最新

相关推荐 更多相似问题