目录
彩色图显示色调不正常——opencv(BGR)QT(RGB)需要进行转换
1、GUI界面(QT designer设计)
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'ImshowImgWithMatGUI.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(280, 460, 201, 23))
self.pushButton.setObjectName("pushButton")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(210, 90, 351, 291))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "通过数组显示图片"))
self.label.setText(_translate("MainWindow", "TextLabel"))
2、逻辑函数(回调等)
import cv2 as cv
from PyQt5.QtGui import QImage,QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow
from ImshowImgWithMatGUI import Ui_MainWindow
import sys
class My_UI(QMainWindow, Ui_MainWindow):
def __init__(self):
QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.setWindowTitle('窗口标题')
def pushbuttonthing(self):
# 读取彩色图片
img_src = cv.imread(r"X:\4.jpg",)
img_src = cv.cvtColor(img_src,cv.COLOR_BGR2RGB)
# 读取label宽高
label_width = self.label.width()
label_height = self.label.height()
# 将图片转换为QImage
temp_imgSrc = QImage(img_src, img_src.shape[1], img_src.shape[0],img_src.shape[1]*3, QImage.Format_RGB888)
# 将图片转换为QPixmap方便显示
pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)
# 使用label进行显示
self.label.setPixmap(pixmap_imgSrc)
def run(self):
self.pushButton.clicked.connect(self.pushbuttonthing)
if __name__ == '__main__':
app = QApplication(sys.argv)
# 显示窗口
win = My_UI()
win.show()
win.run()
sys.exit(app.exec_())
3、显示图片在label上
0)直接利用QPixmap显示图像
from PyQt5.QtGui import QPixmap
label.setScaledContents(True) # 设置图像自适应控件大小 label.setPixmap(QPixmap("***.jpg"))
1)显示彩色图
img_src是通过imread读取的彩色图像
QImage.Format_RGB888
# 将图片转换为QImage
temp_imgSrc = QImage(img_src, img_src.shape[1], img_src.shape[0],img_src.shape[1]*3, QImage.Format_RGB888)
# 将图片转换为QPixmap方便显示
pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)
# 使用label进行显示
self.label.setPixmap(pixmap_imgSrc)
彩色图显示色调不正常——opencv(BGR)QT(RGB)需要进行转换
img_src = cv.cvtColor(img_src,cv.COLOR_BGR2RGB)
2)显示灰度图
显示灰度图像(其中灰度图像是由彩色直接通过)
QImage.Format_Format_Grayscale8
# 将图片转换为QImage
temp_imgSrc = QImage(img_src, img_src.shape[1], img_src.shape[0], QImage.Format_Format_Grayscale8)
# 将图片转换为QPixmap方便显示
pixmap_imgSrc = QPixmap.fromImage(temp_imgSrc).scaled(label_width, label_height)
# 使用label进行显示
self.label.setPixmap(pixmap_imgSrc)
4、显示图像在graphic views上
img = cv2.imread("data\\1.jpg") # 读取图像
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 转换图像通道
x = img.shape[1] # 获取图像大小
y = img.shape[0]
self.zoomscale = 1 # 图片放缩尺度
frame = QImage(img, x, y,x*3, QImage.Format_RGB888)
pix = QPixmap.fromImage(frame)
self.item = QGraphicsPixmapItem(pix) # 创建像素图元
# self.item.setScale(self.zoomscale)
self.scene = QGraphicsScene() # 创建场景
self.scene.addItem(self.item)
self.picshow.setScene(self.scene) # 将场景添加至视图
实例
GUI界面代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2021/11/15 21:01
# @Author : @linlianqin
# @Site :
# @File : test2.py
# @Software: PyCharm
# @description:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QGraphicsPixmapItem
from PyQt5.QtGui import QImage, QPixmap
import cv2
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralWidget = QtWidgets.QWidget(MainWindow)
self.centralWidget.setObjectName("centralWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralWidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.picshow = QtWidgets.QGraphicsView(self.centralWidget)
self.picshow.setObjectName("picshow")
self.gridLayout.addWidget(self.picshow, 0, 1, 3, 1)
self.zoomout = QtWidgets.QPushButton(self.centralWidget)
self.zoomout.setObjectName("zoomout")
self.gridLayout.addWidget(self.zoomout, 0, 0, 1, 1)
self.zoomin = QtWidgets.QPushButton(self.centralWidget)
self.zoomin.setObjectName("zoomin")
self.gridLayout.addWidget(self.zoomin, 1, 0, 1, 1)
self.horizontalLayout.addLayout(self.gridLayout)
MainWindow.setCentralWidget(self.centralWidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.zoomout.setText(_translate("MainWindow", "放大"))
self.zoomin.setText(_translate("MainWindow", "缩小"))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
app.exec_()
逻辑业务代码
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QApplication, QGraphicsScene, QGraphicsPixmapItem
from PyQt5.QtGui import QImage, QPixmap
import cv2
from test2 import Ui_MainWindow
class picturezoom(QMainWindow, Ui_MainWindow):
"""
Class documentation goes here.
"""
def __init__(self, parent=None):
"""
Constructor
@param parent reference to the parent widget
@type QWidget
"""
super(picturezoom, self).__init__(parent)
self.setupUi(self)
img = cv2.imread("data\\1.jpg") # 读取图像
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 转换图像通道
x = img.shape[1] # 获取图像大小
y = img.shape[0]
self.zoomscale = 1 # 图片放缩尺度
frame = QImage(img, x, y,x*3, QImage.Format_RGB888)
pix = QPixmap.fromImage(frame)
self.item = QGraphicsPixmapItem(pix) # 创建像素图元
# self.item.setScale(self.zoomscale)
self.scene = QGraphicsScene() # 创建场景
self.scene.addItem(self.item)
self.picshow.setScene(self.scene) # 将场景添加至视图
@pyqtSlot()
def on_zoomin_clicked(self):
"""
点击缩小图像
"""
# TODO: not implemented yet
self.zoomscale = self.zoomscale - 0.05
if self.zoomscale <= 0:
self.zoomscale = 0.2
self.item.setScale(self.zoomscale) # 缩小图像
@pyqtSlot()
def on_zoomout_clicked(self):
"""
点击方法图像
"""
# TODO: not implemented yet
self.zoomscale = self.zoomscale + 0.05
if self.zoomscale >= 1.2:
self.zoomscale = 1.2
self.item.setScale(self.zoomscale) # 放大图像
def main():
import sys
app = QApplication(sys.argv)
piczoom = picturezoom()
piczoom.show()
app.exec_()
if __name__ == '__main__':
main()
# 图像自适应显示
self.graphicsView.fitInView(self.item)
# 缩放图像(self.zoomscale<1时缩小,self.zoomscale>1时放大)
self.item.setScale(self.zoomscale)
参考:
http://www.baidu.com/link?url=a8hCYgwP-bV878QR6HYFf936xX8oy2PMykU9JvlZ5Nkfluzl046xMqJ6Q6pvatydTqIP_fhO8g-OBxVhQsgVUkcqLNMgIkiUeg3_Fq_FXl3&wd=&eqid=835fd08a0002ad020000000661925f6bhttp://www.baidu.com/link?url=a8hCYgwP-bV878QR6HYFf936xX8oy2PMykU9JvlZ5Nkfluzl046xMqJ6Q6pvatydTqIP_fhO8g-OBxVhQsgVUkcqLNMgIkiUeg3_Fq_FXl3&wd=&eqid=835fd08a0002ad020000000661925f6bpyqt5 graphics view简单使用 - 菜芽caiya - 博客园https://www.cnblogs.com/caiya/p/10328010.html