Home > Database > Mysql Tutorial > 学习OpenCV:滤镜系列(2)扩张&挤压

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

WBOY
Release: 2016-06-07 15:43:20
Original
1267 people have browsed it

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

原图:

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

扩张:

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

挤压:

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template