博客
关于我
cv更换图片背景色
阅读量:236 次
发布时间:2019-02-28

本文共 4814 字,大约阅读时间需要 16 分钟。

import cv2import numpy as npfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QFileDialogclass 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, 0, 251, 331))        self.pushButton.setObjectName("pushButton")        self.pushButton.clicked.connect(self.openFile)        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)        self.pushButton_2.setGeometry(QtCore.QRect(140, 390, 101, 51))        self.pushButton_2.setObjectName("pushButton_2")        self.pushButton_2.clicked.connect(lambda: self.setimg_bg("blue"))        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)        self.pushButton_3.setGeometry(QtCore.QRect(280, 390, 101, 51))        self.pushButton_3.setObjectName("pushButton_3")        self.pushButton_3.clicked.connect(lambda: self.setimg_bg("green"))        self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)        self.pushButton_4.setGeometry(QtCore.QRect(430, 390, 101, 51))        self.pushButton_4.setObjectName("pushButton_4")        self.pushButton_4.clicked.connect(lambda: self.setimg_bg("red"))        self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget)        self.pushButton_5.setGeometry(QtCore.QRect(570, 390, 101, 51))        self.pushButton_5.setObjectName("pushButton_5")        self.pushButton_5.clicked.connect(lambda: self.setimg_bg("white"))        MainWindow.setCentralWidget(self.centralwidget)        self.menubar = QtWidgets.QMenuBar(MainWindow)        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))        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 openFile(self):        global img_path        get_filename_path, ok = QFileDialog.getOpenFileName()        if ok:            img_path = str(get_filename_path)            self.pushButton.setStyleSheet("QPushButton{border-image: url(%s)}" % str(get_filename_path))    def setimg_bg(self, color):        global img_path, new_path        if img_path != '':            change_bg_color(img_path, color)            self.pushButton.setStyleSheet("QPushButton{border-image: url(%s)}" % str(new_path))    def retranslateUi(self, MainWindow):        _translate = QtCore.QCoreApplication.translate        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))        self.pushButton.setText(_translate("MainWindow", ""))        self.pushButton_2.setText(_translate("MainWindow", "蓝色"))        self.pushButton_3.setText(_translate("MainWindow", "绿色"))        self.pushButton_4.setText(_translate("MainWindow", "红色"))        self.pushButton_5.setText(_translate("MainWindow", "白色"))def change_bg_color(path, color):    color_dict = {        'red': [0, 0, 255],        'green': [0, 255, 0],        'blue': [255, 0, 0],        'white': [255, 255, 255]    }    color_list = color_dict[color]    img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), 1)    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)    if img[0][0][0] > 200 and img[0][0][1] < 230 and img[0][0][2] < 230:        hsv_min = np.array([47, 79, 79])        hsv_max = np.array([102, 255, 255])    elif img[0][0][1] > 200 and img[0][0][0] < 230 and img[0][0][2] < 230:        hsv_min = np.array([41, 40, 41])        hsv_max = np.array([90, 255, 255])    elif img[0][0][2] > 200 and img[0][0][0] < 230 and img[0][0][1] < 230:        hsv_min = np.array([0, 200, 40])        hsv_max = np.array([10, 255, 255])    else:        hsv_min = np.array([0, 0, 221])        hsv_max = np.array([180, 30, 255])    mask = cv2.inRange(hsv, hsv_min, hsv_max)    erode = cv2.erode(mask, None, iterations=1)    dilate = cv2.dilate(erode, None, iterations=1)    rows, cols, channels = img.shape    for i in range(rows):        for j in range(cols):            if dilate[i, j] == 255:                img[i, j] = (color_list[0], color_list[1], color_list[2])    new_path = f'%s_%s.jpg' % (str(int(time.time())), color)    path = path.replace(path.split('/')[-1], new_path)    cv2.imencode('.jpg', img)[1].tofile(path)if __name__ == '__main__':    global img_path, new_path    img_path = 'G:/34.jpg'    app = QApplication(sys.argv)    MainWindow = QMainWindow()    ui = Ui_MainWindow()    ui.setupUi(MainWindow)    MainWindow.show()    sys.exit(app.exec_())

转载地址:http://mwvp.baihongyu.com/

你可能感兴趣的文章
Netpas:不一样的SD-WAN+ 保障网络通讯品质
查看>>
Netty WebSocket客户端
查看>>
Netty工作笔记0011---Channel应用案例2
查看>>
Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
查看>>
netty底层源码探究:启动流程;EventLoop中的selector、线程、任务队列;监听处理accept、read事件流程;
查看>>
Netty核心模块组件
查看>>
Netty框架的服务端开发中创建EventLoopGroup对象时线程数量源码解析
查看>>
Netty源码—2.Reactor线程模型一
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码解读
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty相关
查看>>
Network Dissection:Quantifying Interpretability of Deep Visual Representations(深层视觉表征的量化解释)
查看>>
Network Sniffer and Connection Analyzer
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>