学习OpenCV:滤镜系列(8)——素描

【原文:http://blog.csdn.net/yangtrees/article/details/9115321

==============================================

版权所有:小熊不去实验室CSDN博客

==============================================


熟悉photoshop的朋友都知道,把彩色图片打造成素描的效果仅仅需要几步操作:

1、去色;

2、复制去色图层,并且反色;反色为Y(i,j)=255-X(i,j)

3、对反色图像进行高斯模糊;

4、模糊后的图像叠加模式选择颜色减淡效果。

减淡公式:C =MIN( A +(A×B)/(255-B),255),其中C为混合结果,A为去色后的像素点,B为高斯模糊后的像素点。


  1. #include <math.h>  
  2. #include <opencv/cv.h>  
  3. #include <opencv/highgui.h>  
  4.   
  5. using namespace cv;  
  6. using namespace std;  
  7.   
  8. int main()  
  9. {  
  10.     Mat src = imread("D:/arrow.jpg",1);  
  11.     int width=src.cols;  
  12.     int heigh=src.rows;  
  13.     Mat gray0,gray1;  
  14.     //去色  
  15.     cvtColor(src,gray0,CV_BGR2GRAY);  
  16.     //反色  
  17.     addWeighted(gray0,-1,NULL,0,255,gray1);  
  18.     //高斯模糊,高斯核的Size与最后的效果有关  
  19.     GaussianBlur(gray1,gray1,Size(11,11),0);  
  20.   
  21.     //融合:颜色减淡  
  22.     Mat img(gray1.size(),CV_8UC1);  
  23.     for (int y=0; y<heigh; y++)  
  24.     {  
  25.   
  26.         uchar* P0  = gray0.ptr<uchar>(y);  
  27.         uchar* P1  = gray1.ptr<uchar>(y);  
  28.         uchar* P  = img.ptr<uchar>(y);  
  29.         for (int x=0; x<width; x++)  
  30.         {  
  31.             int tmp0=P0[x];  
  32.             int tmp1=P1[x];  
  33.             P[x] =(uchar) min((tmp0+(tmp0*tmp1)/(256-tmp1)),255);  
  34.         }  
  35.   
  36.     }  
  37.     imshow("素描",img);  
  38.     waitKey();  
  39.     imwrite("D:/素描.jpg",img);  
  40. }  

原图:


素描:



Reference:http://blog.csdn.net/wsfdl/article/details/7610634


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值