OpenCV實(shí)現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片
更新時間:2020年08月19日 15:08:34 作者:Nani_xiao
這篇文章主要為大家詳細(xì)介紹了OpenCV實(shí)現(xiàn)彩色照片轉(zhuǎn)換成素描卡通片,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了OpenCV實(shí)現(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++小利器之std::bind參數(shù)綁定包裝器的使用詳解
從 C++11 開始,標(biāo)準(zhǔn)庫提供了 std::bind 用于綁定函數(shù) f 和調(diào)用參數(shù),返回一個新可調(diào)用函數(shù)對象 fn,下面就跟隨小編一起深入了解一下std::bind的具體使用吧2023-12-12
C++實(shí)現(xiàn)刪除txt文件中指定內(nèi)容的示例代碼
這篇文章主要介紹了C++實(shí)現(xiàn)刪除txt文件中指定內(nèi)容的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12

