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.
106 lines
4.7 KiB
106 lines
4.7 KiB
5 months ago
|
import sys
|
||
|
import cv2
|
||
|
import numpy as np
|
||
|
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QGraphicsView, QGraphicsScene, QGraphicsPixmapItem, QFileDialog, QMenuBar, QStatusBar
|
||
|
from PyQt5.QtCore import QRect, QCoreApplication, QMetaObject
|
||
|
from PyQt5.QtGui import QPixmap, QImage
|
||
|
|
||
|
class Ui_MainWindow:
|
||
|
def setupUi(self, MainWindow):
|
||
|
if not MainWindow.objectName():
|
||
|
MainWindow.setObjectName("MainWindow")
|
||
|
MainWindow.resize(950, 750)
|
||
|
self.centralwidget = QWidget(MainWindow)
|
||
|
self.centralwidget.setObjectName("centralwidget")
|
||
|
|
||
|
self.pushButton_1 = QPushButton(self.centralwidget)
|
||
|
self.pushButton_1.setObjectName("pushButton_1")
|
||
|
self.pushButton_1.setGeometry(QRect(100, 550, 240, 71))
|
||
|
|
||
|
self.pushButton_2 = QPushButton(self.centralwidget)
|
||
|
self.pushButton_2.setObjectName("pushButton_2")
|
||
|
self.pushButton_2.setGeometry(QRect(370, 550, 220, 71))
|
||
|
|
||
|
self.pushButton_3 = QPushButton(self.centralwidget)
|
||
|
self.pushButton_3.setObjectName("pushButton_3")
|
||
|
self.pushButton_3.setGeometry(QRect(620, 550, 230, 71))
|
||
|
|
||
|
self.graphicsView = QGraphicsView(self.centralwidget)
|
||
|
self.graphicsView.setObjectName("graphicsView")
|
||
|
self.graphicsView.setGeometry(QRect(70, 60, 400, 450))
|
||
|
|
||
|
self.graphicsView_2 = QGraphicsView(self.centralwidget)
|
||
|
self.graphicsView_2.setObjectName("graphicsView_2")
|
||
|
self.graphicsView_2.setGeometry(QRect(500, 60, 400, 450))
|
||
|
|
||
|
MainWindow.setCentralWidget(self.centralwidget)
|
||
|
self.menubar = QMenuBar(MainWindow)
|
||
|
self.menubar.setObjectName("menubar")
|
||
|
self.menubar.setGeometry(QRect(0, 0, 800, 23))
|
||
|
MainWindow.setMenuBar(self.menubar)
|
||
|
self.statusbar = QStatusBar(MainWindow)
|
||
|
self.statusbar.setObjectName("statusbar")
|
||
|
MainWindow.setStatusBar(self.statusbar)
|
||
|
|
||
|
self.retranslateUi(MainWindow)
|
||
|
QMetaObject.connectSlotsByName(MainWindow)
|
||
|
|
||
|
def retranslateUi(self, MainWindow):
|
||
|
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", "素描风格转换系统", None))
|
||
|
self.pushButton_2.setText(QCoreApplication.translate("MainWindow", "转换至素描风格", None))
|
||
|
self.pushButton_1.setText(QCoreApplication.translate("MainWindow", "选择图片", None))
|
||
|
self.pushButton_3.setText(QCoreApplication.translate("MainWindow", "保存图片", None))
|
||
|
|
||
|
class MainWindow(QMainWindow, Ui_MainWindow):
|
||
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
self.setupUi(self)
|
||
|
self.pushButton_1.clicked.connect(self.select_image)
|
||
|
self.pushButton_2.clicked.connect(self.convert_to_sketch)
|
||
|
self.pushButton_3.clicked.connect(self.save_image)
|
||
|
self.src_image = None
|
||
|
self.dst_image = None
|
||
|
|
||
|
def select_image(self):
|
||
|
options = QFileDialog.Options()
|
||
|
file_name, _ = QFileDialog.getOpenFileName(self, "选择图片", "", "Images (*.png *.xpm *.jpg *.bmp *.gif)", options=options)
|
||
|
if file_name:
|
||
|
self.src_image = cv2.imread(file_name)
|
||
|
self.display_image(self.src_image, self.graphicsView)
|
||
|
|
||
|
def convert_to_sketch(self):
|
||
|
if self.src_image is not None:
|
||
|
#1.将图片转为灰度图
|
||
|
gray = cv2.cvtColor(self.src_image, cv2.COLOR_BGR2GRAY)
|
||
|
#2.将灰度图取反
|
||
|
gray_inverse = cv2.bitwise_not(gray)
|
||
|
#3. 对取反后的图像进行高斯模糊操作,指定了一个15x15的高斯内核
|
||
|
gray_inverse = cv2.GaussianBlur(gray_inverse, (15, 15), 50)
|
||
|
#4. 颜色减淡混合
|
||
|
self.dst_image = cv2.divide(gray, 255 - gray_inverse, scale=256)
|
||
|
self.display_image(self.dst_image, self.graphicsView_2)
|
||
|
|
||
|
def save_image(self):
|
||
|
if self.dst_image is not None:
|
||
|
options = QFileDialog.Options()
|
||
|
file_name, _ = QFileDialog.getSaveFileName(self, "保存图片", "", "Images (*.png *.xpm *.jpg *.bmp *.gif)", options=options)
|
||
|
if file_name:
|
||
|
cv2.imwrite(file_name, self.dst_image)
|
||
|
|
||
|
def display_image(self, image, view):
|
||
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
||
|
h, w, ch = image.shape
|
||
|
bytes_per_line = ch * w
|
||
|
q_image = QImage(image.data, w, h, bytes_per_line, QImage.Format_RGB888)
|
||
|
pixmap = QPixmap.fromImage(q_image)
|
||
|
scene = QGraphicsScene()
|
||
|
scene.addItem(QGraphicsPixmapItem(pixmap))
|
||
|
view.setScene(scene)
|
||
|
view.fitInView(scene.itemsBoundingRect(), mode=1)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
app = QApplication(sys.argv)
|
||
|
window = MainWindow()
|
||
|
window.show()
|
||
|
sys.exit(app.exec_())
|