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

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

# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'untitled.ui'## Created by: PyQt5 UI code generator 5.13.0## WARNING! All changes made in this file will be lost!import sysfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QInputDialog, QFileDialogimport cv2, time, sysimport numpy as npdef change_bg_color(path, color):    global new_path    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.imread(path)    # 导入图片,可以有中文路径    img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), 1)    # 图片缩放    # img=cv2.resize(img,None,fx=0.5,fy=0.5)    # 转换hsv,提取颜色    # cv2.cvtColor是颜色空间转换函数,img是需要转换的图片,第二个是转换成何种格式。    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:        #         print('原图蓝色底')        # 提取颜色区域,不在范围的设为0,在范围的设为255        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:        #         print('原图绿色底')        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:        #         print('原图红色底')        hsv_min = np.array([0, 200, 40])        hsv_max = np.array([10, 255, 255])    else:        #         print('原图白色底')        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)    #     cv2.imshow('res',dilate)    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])  # 此处替换颜色,为BGR通道    new_path = '%s_%s.jpg' % (str(int(time.time())), color)    path = path.replace(path.split('/')[-1], new_path)    new_path = path    # 防止中文路径    cv2.imencode('.jpg', img)[1].tofile(path)#     cv2.imwrite(path,img)#     cv2.imshow('res',img)#     cv2.waitKey(0)#     cv2.destroyAllWindows()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, 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", "白色"))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/

你可能感兴趣的文章
MySQL Workbench 数据库建模详解:从设计到实践
查看>>
MySQL Workbench 数据建模全解析:从基础到实践
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
MySQL —— 视图
查看>>
mysql 不区分大小写
查看>>
mysql 两列互转
查看>>
MySQL 中开启二进制日志(Binlog)
查看>>
MySQL 中文问题
查看>>
MySQL 中日志的面试题总结
查看>>
mysql 中的all,5分钟了解MySQL5.7中union all用法的黑科技
查看>>
MySQL 中的外键检查设置:SET FOREIGN_KEY_CHECKS = 1
查看>>
Mysql 中的日期时间字符串查询
查看>>
mysql 中索引的问题
查看>>
MySQL 中锁的面试题总结
查看>>