当前位置:Gxlcms > mysql > 学习OpenCV:滤镜系列(2)扩张&挤压

学习OpenCV:滤镜系列(2)扩张&挤压

时间:2021-07-01 10:21:17 帮助过:42人阅读

【原文:http://blog.csdn.net/yangtrees/article/details/9095731】 ============================================== 小熊不去实验室CSDN博客 ============================================== 原理:凸透镜效果算法 [cpp] view plaincopyprint? #includem

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

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

小熊不去实验室CSDN博客

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


原理:凸透镜效果算法


[cpp] view plaincopyprint?

  1. #include
  2. #include
  3. #include
  4. using namespace cv;
  5. using namespace std;
  6. int main()
  7. {
  8. Mat src = imread("D:/img/face02.jpg",1);
  9. int width = src.cols;
  10. int heigh = src.rows;
  11. Point center(width/2,heigh/2);
  12. Mat img1(src.size(),CV_8UC3);
  13. Mat img2(src.size(),CV_8UC3);
  14. src.copyTo(img1);
  15. src.copyTo(img2);
  16. //【1】放大
  17. int R1 = sqrtf(width*width+heigh*heigh)/2; //直接关系到放大的力度,与R1成正比;
  18. for (int y=0; y
  19. {
  20. uchar *img1_p = img1.ptr(y);
  21. for (int x=0; x
  22. {
  23. int dis = norm(Point(x,y)-center);
  24. if (dis
  25. {
  26. int newX = (x-center.x)*dis/R1+center.x;
  27. int newY = (y-center.y)*dis/R1+center.y;
  28. img1_p[3*x]=src.at(newY,newX*3);
  29. img1_p[3*x+1]=src.at(newY,newX*3+1);
  30. img1_p[3*x+2]=src.at(newY,newX*3+2);
  31. }
  32. }
  33. }
  34. //【2】挤压
  35. for (int y=0; y
  36. {
  37. uchar *img2_p = img2.ptr(y);
  38. for (int x=0; x
  39. {
  40. double theta = atan2((double)(y-center.y),(double)(x-center.x));//使用atan出现问题~
  41. int R2 = sqrtf(norm(Point(x,y)-center))*8; //直接关系到挤压的力度,与R2成反比;
  42. int newX = center.x+(int)(R2*cos(theta));
  43. int newY = center.y+(int)(R2*sin(theta));
  44. if(newX<0) newX=0;
  45. else if(newX>=width) newX=width-1;
  46. if(newY<0) newY=0;
  47. else if(newY>=heigh) newY=heigh-1;
  48. img2_p[3*x]=src.at(newY,newX*3);
  49. img2_p[3*x+1]=src.at(newY,newX*3+1);
  50. img2_p[3*x+2]=src.at(newY,newX*3+2);
  51. }
  52. }
  53. imshow("src",src);
  54. imshow("img1",img1);
  55. imshow("img2",img2);
  56. waitKey();
  57. imwrite("D:/img/扩张.jpg",img1);
  58. imwrite("D:/img/挤压.jpg",img2);
  59. }


原图:


扩张:


挤压:

人气教程排行