Opencv学习笔记 简单铅笔效果/卡通效果
程序员文章站
2022-05-20 22:31:27
...
铅笔画滤镜
铅笔画滤镜主要是提取出图片中梯度比较高的部分,也就是边缘检测
卡通画特效
卡通画特效主要由三部分组成
1、提取图像中轮廓 => edges
2、使用bilateral 滤波在保留图像的边缘特征的前提下对图像进行模糊操作 => color
3、使用轮廓的图像作为mask对模糊的操作进行and操作 bitwise_and(color, color, cartoon, edges)
//铅笔效果
Mat image = Cv2.ImRead(@"C:\Users\Desktop\timg.jpg");
Mat pencilSketchImage = new Mat();
int kernelSize = 3;
Mat imageGray = new Mat();
Cv2.CvtColor(image, imageGray, ColorConversionCodes.BGR2GRAY);
Mat bluredImage = new Mat();
Cv2.GaussianBlur(imageGray, bluredImage, new OpenCvSharp.Size(kernelSize, kernelSize), 0);
Mat laplacian = new Mat();
Cv2.Laplacian(bluredImage, laplacian, MatType.CV_32F, kernelSize);
Cv2.Normalize(laplacian, laplacian, 0, 1, NormTypes.MinMax);
laplacian = laplacian * 255;
laplacian.ConvertTo(laplacian, MatType.CV_8U);
Cv2.Threshold(laplacian, pencilSketchImage, 145, 255, ThresholdTypes.Binary);
Cv2.ImShow("11", pencilSketchImage);
//卡通画
Mat image = Cv2.ImRead(@"C:\Users\Desktop\timg.jpg");
Mat cartoonImage = new Mat();
Mat imageGray = new Mat();
Cv2.CvtColor(image, imageGray, ColorConversionCodes.BGR2GRAY);
// 获取图像的边缘
Mat bluredImage=new Mat(), edges = new Mat();
Cv2.GaussianBlur(imageGray, bluredImage, new OpenCvSharp.Size(3, 3), 0);
Cv2.AdaptiveThreshold(bluredImage, edges, 255, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 9, 9);
// 使用双边滤波处理图片测颜色
Mat color = new Mat();
Cv2.BilateralFilter(image, color, 0, 5, 5);
// 卡通化
Cv2.BitwiseAnd(color, color, cartoonImage, edges);
Cv2.ImShow("123", cartoonImage);
上一篇: PHP的可变变量名使用方法详解