代码改变世界

学习OpenCV——用OpenCv画漫画

2016-04-17 19:38  GarfieldEr007  阅读(1039)  评论(0编辑  收藏  举报

闲的时候用OpenCV画漫画也挺有意思,虽然效果不好(达不到上面所实现的效果),

参数需要调整,还是大头贴而且噪声小的图像比较合适

而且可以熟悉一下关于各种滤波的操作比如:双边滤波

[cpp] view plain copy
 
 print?
  1. #include "cv.h"  
  2. #include "highgui.h"  
  3.   
  4. using namespace cv;  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     string name="D:/cartoon0.jpg";  
  10.     Mat src1=imread(name,1);  
  11.     Mat img;  
  12.     //双边滤波,第3个参数d可以说d>5时不能实时处理,最后两个参数是sigma参数,一般相同,  
  13.     //<10时基本没效果, >150时漫画效果  
  14.         bilateralFilter(src1,img,5,150,150);  
  15.         bilateralFilter(img,src1,5,150,150);  
  16.         //img.copyTo(src1);  
  17.       
  18.     imshow("bilateral",src1);  
  19.     waitKey(0);  
  20.           
  21.     Mat src;  
  22.     cvtColor(src1,src,CV_BGR2GRAY);  
  23.     //粗线,越大越粗,但是会有大量噪点  
  24.     Mat imgL;  
  25.     //第三个参数ddepth表示目标图像的深度,ddepth=-1时,与原图像一致  
  26.     Laplacian(src,imgL,-1,3,1);  
  27.     imshow("Laplacian",imgL);  
  28.     waitKey(0);  
  29.     //细线  
  30.     Mat imgC;  
  31.     Canny(src,imgC,30,90);  
  32.     imshow("Canny",imgC);  
  33.     waitKey(0);  
  34.   
  35.     Mat imgS,imgSx,imgSy,imgS0;  
  36.     Sobel(src,imgSx,-1,0,1);  
  37.     Sobel(src,imgSx,-1,1,0);  
  38.     imgS=imgSx+imgSy;  
  39.     Sobel(src,imgS0,-1,1,1);  
  40.     imshow("Sobel0",imgS0);  
  41.     imshow("Sobel",imgS);  
  42.     waitKey(0);  
  43.       
  44.     Mat imgTotal;  
  45.     imgTotal=imgC+imgS+imgL;  
  46.     //imgTotal.convertTo(imgTotal,CV_32FC1);  
  47.     normalize(imgTotal,imgTotal,255,0,CV_MINMAX);  
  48.     GaussianBlur(imgTotal,imgTotal,Size(3,3),3);  
  49.     threshold(imgTotal,imgTotal,100,255,THRESH_BINARY_INV);  
  50.     imshow("Total",imgTotal);  
  51.     waitKey(0);  
  52.   
  53.     Mat imgTotalC3;  
  54.     cvtColor(imgTotal,imgTotalC3,CV_GRAY2BGR);  
  55.     bitwise_and(src1,imgTotalC3,src1);  
  56.     imshow("Result",src1);  
  57.     waitKey(0);  
  58.   
  59.       
  60.     name.insert(11,"_edge");  
  61.     imwrite(name,src1);  
  62.   
  63.     /* 
  64.     Mat img(imgTotal.rows,imgTotal.cols,CV_32FC1); 
  65.     for(int i=0;i<imgTotal.rows;i++) 
  66.     { 
  67.         //uchar* p=(uchar*)(imgTotal.ptr()+i*imgTotal.step); 
  68.         for(int j=0;j<imgTotal.cols;j++) 
  69.         { 
  70.             if(imgTotal.at<float>(i,j)==0) 
  71.                 img.at<float>(i,j)=1; 
  72.         } 
  73.     } 
  74.     imshow("Reverse",img); 
  75.     waitKey(0); 
  76.     */  
  77.     /* 
  78.     Mat imgSc; 
  79.     Scharr(src,imgSc,-1,1,0); 
  80.     imshow("Scharr",imgSc); 
  81.     waitKey(0); 
  82.     */  
  83.   
  84. }  

 

from: http://blog.csdn.net/yangtrees/article/details/7544481