飞道的博客

Python+QT美颜工具源码

369人阅读  评论(0)

 程序示例精选

Python+QT美颜工具源码

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!

前言

这篇博客针对《Python+QT美颜》编写代码,功能包括了亮度,磨皮,风格化,铅笔化等多种特征修饰,代码整洁,规则,易读。 学习与应用推荐首选。


文章目录

        一、所需工具软件

        二、使用步骤

                1. 引入库

                2. 显示图片

                3. 美颜特征函数定义

                4. 运行结果

         三在线协助


一、所需工具软件

          1. Python3.6以上

          2. Pycharm代码编辑器

          3. PyQt, OpenCV库

二、使用步骤

1.引入库

代码如下(示例):


  
  1. from PyQt5 import Qt
  2. from PyQt5 import QtCore,QtWidgets,QtGui
  3. import sys
  4. import PyQt5
  5. from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QFileDialog, QGraphicsRectItem, QGraphicsScene
  6. from PyQt5.QtCore import QSize
  7. import cv2
  8. import numpy as np
  9. from matplotlib import pyplot as plt

2.显示图片

代码如下(示例):


  
  1. # 显示图片
  2. def show_image (self):
  3. img_cv = cv2.cvtColor(self.current_img, cv2.COLOR_RGB2BGR)
  4. img_width, img_height, a = img_cv.shape
  5. ratio_img = img_width/img_height
  6. ratio_scene = self.ui.graphicsView.width()/self.ui.graphicsView.height()
  7. if ratio_img > ratio_scene:
  8. width = int(self.ui.graphicsView.width())
  9. height = int(self.ui.graphicsView.width() / ratio_img)
  10. else:
  11. width = int(self.ui.graphicsView.height() * ratio_img)
  12. height = int(self.ui.graphicsView.height())
  13. img_resize = cv2.resize(img_cv, (height -5, width -5), interpolation=cv2.INTER_AREA)
  14. h, w, c = img_resize.shape
  15. bytesPerLine = w * 3
  16. qimg = QImage(img_resize.data, w, h, bytesPerLine, QImage.Format_RGB888)
  17. self.scene = QGraphicsScene()
  18. pix = QPixmap(qimg)
  19. self.scene.addPixmap(pix)
  20. self.ui.graphicsView.setScene(self.scene)

3.美颜特征函数定义:

代码如下(示例):


  
  1. # 饱和度
  2. def change_saturation( self):
  3. if self.raw_image is None:
  4. return 0
  5. value = self.ui.horizontalSlider.value()
  6. img_hsv = cv2.cvtColor(self.current_img, cv2.COLOR_BGR2HLS)
  7. if value > 2:
  8. img_hsv[:, :, 2] = np.log(img_hsv[:, :, 2] / 255* (value - 1)+ 1) / np.log(value + 1) * 255
  9. if value < 0:
  10. img_hsv[:, :, 2] = np.uint8(img_hsv[:, :, 2] / np.log(- value + np.e))
  11. self.current_img = cv2.cvtColor(img_hsv, cv2.COLOR_HLS2BGR)
  12. # 明度调节
  13. def change_darker( self):
  14. if self.raw_image is None:
  15. return 0
  16. value = self.ui.horizontalSlider_4.value()
  17. img_hsv = cv2.cvtColor(self.current_img, cv2.COLOR_BGR2HLS)
  18. if value > 3:
  19. img_hsv[:, :, 1] = np.log(img_hsv[:, :, 1] / 255* (value - 1)+ 1) / np.log(value + 1) * 255
  20. if value < 0:
  21. img_hsv[:, :, 1] = np.uint8(img_hsv[:, :, 1] / np.log(- value + np.e))
  22. self.last_image = self.current_img
  23. self.current_img = cv2.cvtColor(img_hsv, cv2.COLOR_HLS2BGR)
  24. # 人脸识别
  25. def detect_face( self):
  26. img = self.raw_image
  27. face_cascade = cv2.CascadeClassifier( './haarcascade_frontalface_default.xml')
  28. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  29. faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  30. return faces
  31. # 皮肤识别
  32. def detect_skin( self):
  33. img = self.raw_image
  34. rows, cols, channals = img.shape
  35. for r in range(rows):
  36. for c in range(cols):
  37. B = img.item(r, c, 0)
  38. G = img.item(r, c, 1)
  39. R = img.item(r, c, 2)
  40. if ( abs(R - G) > 15) and (R > G) and (R > B):
  41. if (R > 95) and (G > 40) and (B > 20) and ( max(R, G, B) - min(R, G, B) > 15):
  42. self.imgskin[r, c] = ( 1, 1, 1)
  43. elif (R > 220) and (G > 210) and (B > 170):
  44. self.imgskin[r, c] = ( 1, 1, 1)
  45. # 皮肤磨皮(value1精细度,value2程度)
  46. def dermabrasion( self, value1=3, value2=2):
  47. value1 = self.ui.horizontalSlider_14.value()
  48. value2 = 11 - self.ui.horizontalSlider_11.value()
  49. if value1 == 0 and value2 == 0:
  50. return 0
  51. if value2 == 0:
  52. value2 = 2
  53. if value1 == 0:
  54. value1 = 3
  55. img = self.current_img
  56. dx = value1 * 5
  57. fc = value1 * 12.5
  58. p = 50
  59. temp1 = cv2.bilateralFilter(img, dx, fc, fc)
  60. temp2 = (temp1 - img + 128)
  61. temp3 = cv2.GaussianBlur(temp2, ( 2 * value2 - 1, 2 * value2 - 1), 0, 0)
  62. temp4 = img + 2 * temp3 - 255
  63. dst = np.uint8(img * (( 100 - p) / 100) + temp4 * (p / 100))
  64. imgskin_c = np.uint8(-(self.imgskin - 1))
  65. dst = np.uint8(dst * self.imgskin + img * imgskin_c)
  66. self.current_img = dst
  67. # 美白算法(皮肤识别)
  68. def whitening_skin( self, value=30):
  69. # value = 30
  70. value = self.ui.horizontalSlider_13.value()
  71. img = self.current_img
  72. imgw = np.zeros(img.shape, dtype= 'uint8')
  73. imgw = img.copy()
  74. midtones_add = np.zeros( 256)
  75. for i in range( 256):
  76. midtones_add[i] = 0.667 * ( 1 - ((i - 127.0) / 127) * ((i - 127.0) / 127))
  77. lookup = np.zeros( 256, dtype= "uint8")
  78. for i in range( 256):
  79. red = i
  80. red += np.uint8(value * midtones_add[red])
  81. red = max( 0, min( 0xff, red))
  82. lookup[i] = np.uint8(red)
  83. rows, cols, channals = img.shape
  84. for r in range(rows):
  85. for c in range(cols):
  86. if self.imgskin[r, c, 0] == 1:
  87. imgw[r, c, 0] = lookup[imgw[r, c, 0]]
  88. imgw[r, c, 1] = lookup[imgw[r, c, 1]]
  89. imgw[r, c, 2] = lookup[imgw[r, c, 2]]
  90. self.current_img = imgw
  91. # 美白算法(人脸识别)
  92. def whitening_face( self, value=30):
  93. # value = 30
  94. value = self.ui.horizontalSlider_8.value()
  95. img = self.current_img
  96. imgw = np.zeros(img.shape, dtype= 'uint8')
  97. imgw = img.copy()
  98. midtones_add = np.zeros( 256)
  99. for i in range( 256):
  100. midtones_add[i] = 0.667 * ( 1 - ((i - 127.0) / 127) * ((i - 127.0) / 127))
  101. lookup = np.zeros( 256, dtype= "uint8")
  102. for i in range( 256):
  103. red = i
  104. red += np.uint8(value * midtones_add[red])
  105. red = max( 0, min( 0xff, red))
  106. lookup[i] = np.uint8(red)
  107. # faces可全局变量
  108. faces = self.faces
  109. if faces == ():
  110. rows, cols, channals = img.shape
  111. for r in range(rows):
  112. for c in range(cols):
  113. imgw[r, c, 0] = lookup[imgw[r, c, 0]]
  114. imgw[r, c, 1] = lookup[imgw[r, c, 1]]
  115. imgw[r, c, 2] = lookup[imgw[r, c, 2]]
  116. else:
  117. x, y, w, h = faces[ 0]
  118. rows, cols, channals = img.shape
  119. x = max(x - (w * np.sqrt( 2) - w) / 2, 0)
  120. y = max(y - (h * np.sqrt( 2) - h) / 2, 0)
  121. w = w * np.sqrt( 2)
  122. h = h * np.sqrt( 2)
  123. rows = min(rows, y + h)
  124. cols = min(cols, x + w)
  125. for r in range( int(y), int(rows)):
  126. for c in range( int(x), int(cols)):
  127. imgw[r, c, 0] = lookup[imgw[r, c, 0]]
  128. imgw[r, c, 1] = lookup[imgw[r, c, 1]]
  129. imgw[r, c, 2] = lookup[imgw[r, c, 2]]
  130. processWidth = int( max( min(rows - y, cols - 1) / 8, 2))
  131. for i in range( 1, processWidth):
  132. alpha = (i - 1) / processWidth
  133. for r in range( int(y), int(rows)):
  134. imgw[r, int(x) + i - 1] = np.uint8(
  135. imgw[r, int(x) + i - 1] * alpha + img[r, int(x) + i - 1] * ( 1 - alpha))
  136. imgw[r, int(cols) - i] = np.uint8(
  137. imgw[r, int(cols) - i] * alpha + img[r, int(cols) - i] * ( 1 - alpha))
  138. for c in range( int(x) + processWidth, int(cols) - processWidth):
  139. imgw[ int(y) + i - 1, c] = np.uint8(
  140. imgw[ int(y) + i - 1, c] * alpha + img[ int(y) + i - 1, c] * ( 1 - alpha))
  141. imgw[ int(rows) - i, c] = np.uint8(
  142. imgw[ int(rows) - i, c] * alpha + img[ int(rows) - i, c] * ( 1 - alpha))
  143. self.current_img = imgw
  144. # Gamma矫正
  145. def gamma_trans( self):
  146. gamma = (self.ui.horizontalSlider_5.value() + 10) / 10
  147. img = self.current_img
  148. gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range( 256)]
  149. gamma_table = np. round(np.array(gamma_table)).astype(np.uint8)
  150. self.current_img = cv2.LUT(img, gamma_table)
  151. self.show_image()
  152. self.show_histogram()
  153. # 响应滑动条的变化
  154. def slider_change( self):
  155. if self.raw_image is None:
  156. return 0
  157. self.current_img = self.raw_image
  158. # 伽马变换
  159. if self.ui.horizontalSlider_5.value() != 0:
  160. self.gamma_trans()
  161. # 饱和度
  162. if self.ui.horizontalSlider.value() != 0:
  163. self.change_saturation()
  164. if self.ui.horizontalSlider_2.value() != 0:
  165. pass
  166. # 边缘保持
  167. if self.ui.horizontalSlider_3.value() != 0:
  168. self.edge_preserve()
  169. # 亮度
  170. if self.ui.horizontalSlider_4.value() != 0:
  171. self.change_darker()
  172. # 美白(人脸识别)
  173. if self.ui.horizontalSlider_8.value() != 0:
  174. self.whitening_face()
  175. # 美白(皮肤识别)
  176. if self.ui.horizontalSlider_13.value() != 0:
  177. self.whitening_skin()
  178. # 风格化
  179. if self.ui.horizontalSlider_2.value() != 0:
  180. self.stylize()
  181. # 细节增强
  182. if self.ui.horizontalSlider_6.value() != 0:
  183. self.detail_enhance()
  184. # 铅笔画
  185. if self.ui.horizontalSlider_12.value() != 0:
  186. self.pencil_color()
  187. self.show_image()
  188. # 计算人脸识别和皮肤识别的基本参数
  189. def calculate( self):
  190. if self.raw_image is None:
  191. return 0
  192. if self.calculated is False:
  193. self.faces = self.detect_face()
  194. if self.faces != ():
  195. self.detect_skin()
  196. self.calculated = True
  197. # 怀旧滤镜
  198. def reminiscene( self):
  199. if self.raw_image is None:
  200. return 0
  201. if self.ui.horizontalSlider_10.value() == 0:
  202. self.current_img = self.raw_image
  203. self.show_image()
  204. return 0
  205. img = self.raw_image.copy()
  206. rows, cols, channals = img.shape
  207. for r in range(rows):
  208. for c in range(cols):
  209. B = img.item(r, c, 0)
  210. G = img.item(r, c, 1)
  211. R = img.item(r, c, 2)
  212. img[r, c, 0] = np.uint8( min( max( 0.272 * R + 0.534 * G + 0.131 * B, 0), 255))
  213. img[r, c, 1] = np.uint8( min( max( 0.349 * R + 0.686 * G + 0.168 * B, 0), 255))
  214. img[r, c, 2] = np.uint8( min( max( 0.393 * R + 0.769 * G + 0.189 * B, 0), 255))
  215. self.current_img = img
  216. self.show_image()
  217. # 木刻滤镜
  218. def woodcut( self):
  219. if self.raw_image is None:
  220. return 0
  221. if self.ui.horizontalSlider_9.value() == 0:
  222. # self.current_img = self.raw_image
  223. self.show_image()
  224. return 0
  225. self.gray_image = cv2.cvtColor(self.raw_image, cv2.COLOR_BGR2GRAY)
  226. gray = self.gray_image
  227. value = 70 + self.ui.horizontalSlider_9.value()
  228. rows, cols = gray.shape
  229. for r in range(rows):
  230. for c in range(cols):
  231. if gray[r, c] > value:
  232. gray[r, c] = 255
  233. else:
  234. gray[r, c] = 0
  235. self.gray_image = gray
  236. self.show_grayimage()
  237. # 铅笔画(灰度)
  238. def pencil_gray( self):
  239. if self.raw_image is None:
  240. return 0
  241. if self.ui.horizontalSlider_7.value() == 0:
  242. # self.current_img = self.raw_image
  243. self.show_image()
  244. return 0
  245. value = self.ui.horizontalSlider_7.value() * 0.05
  246. dst1_gray, dst1_color = cv2.pencilSketch(self.current_img, sigma_s= 50, sigma_r=value, shade_factor= 0.04)
  247. self.gray_image = dst1_gray
  248. self.show_grayimage()
  249. # 铅笔画(彩色)
  250. def pencil_color( self):
  251. if self.raw_image is None:
  252. return 0
  253. if self.ui.horizontalSlider_12.value() == 0:
  254. self.current_img = self.raw_image
  255. self.show_image()
  256. return 0
  257. value = self.ui.horizontalSlider_12.value() * 0.05
  258. dst1_gray, dst1_color = cv2.pencilSketch(self.current_img, sigma_s= 50, sigma_r=value, shade_factor= 0.04)
  259. self.current_img = dst1_color
  260. # 风格化
  261. def stylize( self):
  262. if self.raw_image is None:
  263. return 0
  264. if self.ui.horizontalSlider_2.value() == 0:
  265. self.current_img = self.raw_image
  266. self.show_image()
  267. return 0
  268. value = self.ui.horizontalSlider_2.value() * 0.05
  269. self.current_img = cv2.stylization(self.current_img, sigma_s= 50, sigma_r=value)
  270. # 细节增强
  271. def detail_enhance( self):
  272. if self.raw_image is None:
  273. return 0
  274. if self.ui.horizontalSlider_6.value() == 0:
  275. self.current_img = self.raw_image
  276. self.show_image()
  277. return 0
  278. value = self.ui.horizontalSlider_6.value() * 0.05
  279. self.current_img = cv2.detailEnhance(self.current_img, sigma_s= 50, sigma_r=value)
  280. # 边缘保持
  281. def edge_preserve( self):
  282. if self.raw_image is None:
  283. return 0
  284. if self.ui.horizontalSlider_3.value() == 0:
  285. self.current_img = self.raw_image
  286. self.show_image()
  287. return 0
  288. value = self.ui.horizontalSlider_3.value() * 0.05
  289. self.current_img = cv2.edgePreservingFilter(self.current_img, flags= 1, sigma_s= 50, sigma_r=value)
  290. # 显示摄像照片
  291. def show_camera( self):
  292. flag, self.camera_image = self.cap.read()
  293. show = cv2.resize(self.image, ( 640, 480))
  294. show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)
  295. showImage = QtGui.QImage(show.data, show.shape[ 1], show.shape[ 0], QtGui.QImage.Format_RGB888)
  296. self.label_show_camera.setPixmap(QtGui.QPixmap.fromImage(showImage))

4.运行结果如下: 

三、在线协助: 

如需安装运行环境或远程调试,见文章底部微信名片,由专业技术人员远程协助!


转载:https://blog.csdn.net/alicema1111/article/details/128346509
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场