OpenCV实现彩色照片转换成素描卡通片
2019/7/10 22:30:31
本文主要是介绍OpenCV实现彩色照片转换成素描卡通片,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例为大家分享了OpenCV实现彩色照片转换成素描卡通片的具体代码,供大家参考,具体内容如下
#include"stdafx.h" //#include<cv.h> //#include<highgui.h> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/core/core.hpp> using namespace cv; using namespace std; int main() { Mat src,smallImg,tmp,bigImg,gray,edges,masks,dst; const int MEDIAN_BLUR_FILTER_SIZE = 7; const int LAPLACIAN_FILTER_SIZE = 5; const int EDGES_THRESHOLD = 80; int repetitions = 7; // Repetitions for strong cartoon effect. src = imread("pic.jpg"); Size size = src.size(); Size smallSize; smallSize.width = size.width/2; smallSize.height = size.height/2; smallImg = Mat(smallSize, CV_8UC3); tmp = Mat(smallSize, CV_8UC3); dst= Mat(size,CV_8UC3); cvtColor(src,gray,CV_BGR2GRAY); medianBlur(gray,gray,MEDIAN_BLUR_FILTER_SIZE); Laplacian(gray, edges, CV_8U,LAPLACIAN_FILTER_SIZE); threshold(edges, masks,EDGES_THRESHOLD,255, THRESH_BINARY_INV); imshow("sketch:)", masks); waitKey(10); resize(src, smallImg, smallSize, 0,0, INTER_LINEAR); for (int i=0; i<repetitions; i++) { int ksize = 9; // Filter size. Has a large effect on speed. double sigmaColor = 9; // Filter color strength. double sigmaSpace = 7; // Spatial strength. Affects speed. bilateralFilter(smallImg, tmp, ksize, sigmaColor, sigmaSpace); bilateralFilter(tmp, smallImg, ksize, sigmaColor, sigmaSpace); } resize(smallImg, bigImg, size, 0,0, INTER_LINEAR); bigImg.copyTo(dst,masks); imshow("cartoon :)", dst); waitKey(0); return 0; }
原图、素描图、卡通图效果依次为:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于OpenCV实现彩色照片转换成素描卡通片的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-07-03微信支付提示下单账户与支付账户不一致-icode9专业技术文章分享
- 2024-07-03微信支付提示订单号重复-icode9专业技术文章分享
- 2024-07-02微服务启动nacos注册上去了,但是一直没有收到请求-icode9专业技术文章分享
- 2024-07-02如何检查文件的编码格式-icode9专业技术文章分享
- 2024-07-02sublime 更改编码格式-icode9专业技术文章分享
- 2024-06-30uniAPP 实现全屏左右滚动滚动的效果-icode9专业技术文章分享
- 2024-06-30如何在本地使用授权或插件-icode9专业技术文章分享
- 2024-06-30伪静态规则配置方法汇总-icode9专业技术文章分享
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享