最近发现Opencv里面一个好玩的图像处理函数PencilSketch,包含几种不错的滤镜,所以尝试了一下~
代码如下:
import os
import cv2
## 读取图片
img = cv2.imread("view.jpg")
## 分别进行 铅笔画、风格化、细节增强、边缘保持等处理
dst1_gray, dst1_color = cv2.pencilSketch(img, sigma_s = 50, sigma_r = 0.15, shade_factor = 0.04)
dst2 = cv2.stylization(img, sigma_s = 50, sigma_r = 0.15)
dst3 = cv2.detailEnhance(img, sigma_s = 50, sigma_r = 0.15)
dst4 = cv2.edgePreservingFilter(img, flags=1, sigma_s = 50, sigma_r = 0.15)
## 显示
cv2.imshow("source", img)
cv2.imshow("pencilSketchG", dst1_gray)
cv2.imshow("pencilSketchC", dst1_color)
cv2.imshow("stylization", dst2)
cv2.imshow("detailEnhance", dst3)
cv2.imshow("edgePreserving", dst4)
## 保存
File_Path = os.getcwd()[:-4] +'pic\\' #获取到当前文件的目录,并检查是否有pic文件夹,如果不存在则自动新建pic文件
if not(os.path.exists(File_Path)):
os.makedirs(File_Path)
cv2.imwrite("pic/1.jpg",dst1_gray)
cv2.imwrite("pic/2.jpg",dst1_color)
cv2.imwrite("pic/3.jpg",dst2)
cv2.imwrite("pic/4.jpg",dst3)
cv2.imwrite("pic/5.jpg",dst4)
cv2.imwrite("pic/0.jpg",img)
cv2.waitKey()
cv2.destroyAllWindows()
下面是几种图片处理的效果:
0_原图 1_pencilSketch_grey 2_pencilSketch_color 3_stylization 4_detailEnhance 5_edgePreserving
完毕~
网友评论