学习OpenCV——用OpenCv画漫画-LMLPHP

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

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

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

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

学习OpenCV——用OpenCv画漫画-LMLPHP学习OpenCV——用OpenCv画漫画-LMLPHP

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

05-07 15:02