OpenCV實現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片
更新時間:2020年08月19日 15:08:34 作者:Nani_xiao
這篇文章主要為大家詳細(xì)介紹了OpenCV實現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了OpenCV實現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片的具體代碼,供大家參考,具體內(nèi)容如下
#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;
}
原圖、素描圖、卡通圖效果依次為:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
老生常談C語言動態(tài)函數(shù)庫的制作和使用(推薦)
下面小編就為大家?guī)硪黄仙U凜語言動態(tài)函數(shù)庫的制作和使用(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
C++可調(diào)用對象callable object深入分析
所謂的callable object,表示可以被某種方式調(diào)用其某些函數(shù)的對象。它可以是:一個函數(shù)、一個指向成員函數(shù)的指針、一個函數(shù)對象,該對象擁有operator()、一個lambda表達(dá)式,嚴(yán)格的說它是一種函數(shù)對象2022-08-08
C語言?深入理解動態(tài)規(guī)劃之計數(shù)類DP
動態(tài)規(guī)劃可謂是大名鼎鼎,筆試面試中的高頻考點,也是重點難點,動態(tài)規(guī)劃類型題目靈活多變,難度系數(shù)也相對較高,往往我們做不好動態(tài)規(guī)劃的題目就會與心儀的offer失之交臂,本篇文章我們就一起來研究一下動態(tài)規(guī)劃的計數(shù)類DP2022-04-04

