闲着没事在网上找美颜的例子,还真叫我找到了,然后自己就写了很丑很简单的一个小界面,可以实现人脸的磨皮和美白,效果还算不错。美颜部分代码是借鉴的大神的。
meiyan.py
import cv2
import numpy as np
import sys
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QFileDialog, QMessageBox, QDockWidget, QListWidget,QMainWindow,QApplication,QPushButton
from PyQt5.QtGui import *
from untitled import Ui_MainWindow
class camshow(QMainWindow,Ui_MainWindow):
def __init__(self, parent=None):
super(camshow, self).__init__(parent)
self.setupUi(self)
self.button.clicked.connect(self.kunkun)
self.label.setScaledContents(True)
self.label_2.setScaledContents(True)
def kunkun(self):
img = cv2.imread('D://zopencv//xiajie.jpg')
dst = np.zeros_like(img)
# int value1 = 3, value2 = 1; 磨皮程度与细节程度的确定
v1 = 3
v2 = 1
dx = v1 * 5 # 双边滤波参数之一
fc = v1 * 12.5 # 双边滤波参数之一
p = 0.1
temp4 = np.zeros_like(img)
temp1 = cv2.bilateralFilter(img, dx, fc, fc)
temp2 = cv2.subtract(temp1, img)
temp2 = cv2.add(temp2, (10, 10, 10, 128))
temp3 = cv2.GaussianBlur(temp2, (2 * v2 - 1, 2 * v2 - 1), 0)
temp4 = cv2.add(img, temp3)
dst = cv2.addWeighted(img, p, temp4, 1 - p, 0.0)
dst = cv2.add(dst, (10, 10, 10, 255))
Im = dst
image_height, image_width, image_depth = Im.shape # 获取图像的高,宽以及深度。
QIm = cv2.cvtColor(Im, cv2.COLOR_BGR2RGB) # opencv读图片是BGR,qt显示要RGB,所以需要转换一下
QIm = QImage(QIm.data, image_width, image_height, # 创建QImage格式的图像,并读入图像信息
image_width * image_depth,
QImage.Format_RGB888)
Im2 = img
image_height, image_width, image_depth = Im2.shape # 获取图像的高,宽以及深度。
QIm2 = cv2.cvtColor(Im2, cv2.COLOR_BGR2RGB) # opencv读图片是BGR,qt显示要RGB,所以需要转换一下
QIm2 = QImage(QIm2.data, image_width, image_height, # 创建QImage格式的图像,并读入图像信息
image_width * image_depth,
QImage.Format_RGB888)
self.label.setPixmap(QPixmap.fromImage(QIm2))
self.label_2.setPixmap(QPixmap.fromImage(QIm)) # 将QI
if __name__ == '__main__':
app=QApplication(sys.argv)
c=camshow()
c.show()
sys.exit(app.exec_())
untitled.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1022, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(19, 19, 500, 461))
self.frame.setFrameShape(QtWidgets.QFrame.Box)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(40, 0, 421, 461))
self.label.setObjectName("label")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(529, 20, 481, 461))
self.frame_2.setFrameShape(QtWidgets.QFrame.Box)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.label_2 = QtWidgets.QLabel(self.frame_2)
self.label_2.setGeometry(QtCore.QRect(50, 0, 391, 451))
self.label_2.setObjectName("label_2")
self.button = QtWidgets.QPushButton(self.centralwidget)
self.button.setGeometry(QtCore.QRect(20, 480, 991, 61))
self.button.setObjectName("button")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 1022, 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 retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "原图"))
self.label_2.setText(_translate("MainWindow", "整容后。。。"))
self.button.setText(_translate("MainWindow", "一键变帅,奥利给!"))
其实这个还可以加比如涂口红之类的,等我研究研究在更新代码。