欢迎来到入门教程网!

C语言

当前位置:主页 > 软件编程 > C语言 >

OpenCV实现彩色照片转换成素描卡通片

来源:本站原创|时间:2020-01-10|栏目:C语言|点击:

本文实例为大家分享了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;
}

原图、素描图、卡通图效果依次为:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

上一篇:C++小知识:用合适的工具来分析你的代码

栏    目:C语言

下一篇:C++小知识:C/C++中不要按值传递数组

本文标题:OpenCV实现彩色照片转换成素描卡通片

本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/523.html

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 脚本教程网 版权所有