import os.path
from PyQt5 import QtWidgets
from PyQt5 import QtCore, QtGui
import sys
import cv2
class ButtonPanel(QtWidgets.QWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
select_btn = QtWidgets.QPushButton("图像选择")
self.path_label = QtWidgets.QLabel()
self.path_label.setText("当前未显示图像路径")
self.path_label.setAlignment(QtCore.Qt.AlignCenter) # label上居中显示
self.path_label.setMaximumHeight(50) # label最大高度设置
self.path_label.setStyleSheet("background-color:pink;color:green") # 背景颜色设置
font = QtGui.QFont()
font.setBold(True)
font.setPointSizeF(10)
self.path_label.setFont(font)
self.image_label = QtWidgets.QLabel()
src = cv2.imread("./image/img1.png") # BGR
image = cv2.cvtColor(src, cv2.COLOR_BGR2RGB) # 将BGR转为RGB
h, w, c = image.shape
img = QtGui.QImage(image.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap(img)
pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio) # 自动保持比例放缩方式
self.image_label.setPixmap(pix) # 设置图像显示
self.image_label.setAlignment(QtCore.Qt.AlignCenter) # label上的内容居中显示
self.image_label.setStyleSheet("background-color:blue;color:green") # 背景颜色设置
self.gray_rbtn = QtWidgets.QRadioButton("灰度")
self.gauss_rbtn = QtWidgets.QRadioButton("高斯模糊")
self.laps_rbtn = QtWidgets.QRadioButton("拉普拉斯")
self.origin_rbtn = QtWidgets.QRadioButton("原图")
rbtn_panel = QtWidgets.QGroupBox("图像文件选择")
hbox = QtWidgets.QHBoxLayout()
hbox.addWidget(self.gray_rbtn)
hbox.addWidget(self.gauss_rbtn)
hbox.addWidget(self.laps_rbtn)
hbox.addWidget(self.origin_rbtn)
hbox.addStretch(1)
rbtn_panel.setLayout(hbox)
btn_panel = QtWidgets.QGroupBox("图像文件选择")
hboxlayout = QtWidgets.QHBoxLayout()
hboxlayout.addWidget(self.path_label)
hboxlayout.addWidget(select_btn)
hboxlayout.addStretch(1)
btn_panel.setLayout(hboxlayout)
self.image_label.setAlignment(QtCore.Qt.AlignCenter) # label上的内容居中显示
self.image_label.setStyleSheet("background-color:pink;color:green") # 背景颜色设置
vboxlayout = QtWidgets.QVBoxLayout()
vboxlayout.addWidget(self.image_label)
vboxlayout.addWidget(btn_panel)
vboxlayout.addWidget(rbtn_panel)
vboxlayout.addStretch(1)
self.setLayout(vboxlayout)
# 绑定点击
select_btn.clicked.connect(self.on_select_image)
self.gray_rbtn.toggled.connect(self.on_gray_image)
self.gauss_rbtn.toggled.connect(self.on_blur_image)
self.laps_rbtn.toggled.connect(self.on_laps_image)
self.origin_rbtn.toggled.connect(self.on_origin_image)
def on_gray_image(self):
file_path = self.path_label.text()
print(file_path)
# file_path = 'D:/Work/ProjectItems/OpenCV+PyQT开发项目实例/image/img2.png'
# file_path.split('/')[-1]
file_path = os.path.join('.\\image', file_path.split('/')[-1])
src = cv2.imread(file_path) # BGR
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) # 将BGR转为gray
rbg = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB) # 将gray转为RGB
h, w, c = src.shape
img = QtGui.QImage(rbg.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap(img)
pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio) # 自动保持比例放缩方式
self.image_label.setPixmap(pix) # 设置图像显示
# self.image_label.setAlignment(QtCore.Qt.AlignCenter) # label上的内容居中显示
# self.image_label.setStyleSheet("background-color:blue;color:green") # 背景颜色设置
def on_blur_image(self):
file_path = self.path_label.text()
file_path = os.path.join('.\\image', file_path.split('/')[-1])
src = cv2.imread(file_path) # BGR
blur = cv2.GaussianBlur(src, (0, 0), 15) # 将BGR转为gray
rbg = cv2.cvtColor(blur, cv2.COLOR_BGR2RGB) # 将BGR转为RGB
h, w, c = src.shape
img = QtGui.QImage(rbg.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap(img)
pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio) # 自动保持比例放缩方式
self.image_label.setPixmap(pix) # 设置图像显示
# self.image_label.setAlignment(QtCore.Qt.AlignCenter) # label上的内容居中显示
# self.image_label.setStyleSheet("background-color:blue;color:green") # 背景颜色设置
def on_laps_image(self):
file_path = self.path_label.text()
file_path = os.path.join('.\\image', file_path.split('/')[-1])
src = cv2.imread(file_path) # BGR
laps = cv2.Laplacian(src, -1) # -1表示输入和输出保持一致
rbg = cv2.cvtColor(laps, cv2.COLOR_BGR2RGB) # 将BGR转为RGB
h, w, c = src.shape
img = QtGui.QImage(rbg.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap(img)
pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio) # 自动保持比例放缩方式
self.image_label.setPixmap(pix) # 设置图像显示
# self.image_label.setAlignment(QtCore.Qt.AlignCenter) # label上的内容居中显示
# self.image_label.setStyleSheet("background-color:blue;color:green") # 背景颜色设置
def on_origin_image(self):
file_path = self.path_label.text()
file_path = os.path.join('.\\image', file_path.split('/')[-1])
src = cv2.imread(file_path) # BGR
rbg = cv2.cvtColor(src, cv2.COLOR_BGR2RGB) # 将BGR转为RGB
h, w, c = src.shape
img = QtGui.QImage(rbg.data, w, h, 3 * w, QtGui.QImage.Format_RGB888)
pixmap = QtGui.QPixmap(img)
pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio) # 自动保持比例放缩方式
self.image_label.setPixmap(pix) # 设置图像显示
# self.image_label.setAlignment(QtCore.Qt.AlignCenter) # label上的内容居中显示
# self.image_label.setStyleSheet("background-color:blue;color:green") # 背景颜色设置
def on_select_image(self):
fileinfo = QtWidgets.QFileDialog.getOpenFileName(self, "打开图像文件", ".", "图像文件(*.jpg *.png)")
fileName = fileinfo[0]
if fileName != "":
self.path_label.setText(fileName)
pixmap = QtGui.QPixmap(fileName)
pix = pixmap.scaled(QtCore.QSize(640, 640), QtCore.Qt.KeepAspectRatio) # 自动保持比例放缩方式
self.image_label.setPixmap(pix) # 设置图像显示
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
main_win = QtWidgets.QMainWindow()
main_win.setWindowTitle("图像浏览显示")
myPanel = ButtonPanel()
main_win.setCentralWidget(myPanel)
main_win.setMinimumSize(1080, 720)
main_win.show()
app.exec_()