OpenCV小例程_把彩色照片转换成素描卡通片

本文介绍了一种使用OpenCV库实现的图像卡通化效果处理方法。该方法通过多次双侧滤波和拉普拉斯边缘检测,结合阈值处理和掩膜操作,最终实现了从普通图像到卡通风格图像的转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

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



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值