学习OpenCV:滤镜系列(7)——漩涡

本文介绍了一种使用OpenCV实现图像漩涡变换的方法,通过旋转和缩放角度随距离变化来创建漩涡效果,并提供了完整的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【原文:http://blog.youkuaiyun.com/yangtrees/article/details/9114283

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

版权所有:小熊不去实验室优快云博客

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


设图象的漩涡中心为o,设p为图像任意点,只要对p做绕o的旋转,且旋转角随p-o距离的增加而减少,就是"漩涡"变换了.


  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. template<typename T> T sqr(T x){return x*x;}  
  9.   
  10. double Pi=3.14;  
  11.   
  12. double Para=20;  
  13.   
  14. int main()  
  15. {  
  16.     Mat src = imread("D:/img/face01.jpg",1);  
  17.     int heigh = src.rows;  
  18.     int width = src.cols;  
  19.     Point center(width/2, heigh/2);  
  20.     Mat img;  
  21.     src.copyTo(img);  
  22.     Mat src1u[3];  
  23.     split(src,src1u);  
  24.   
  25.     for (int y=0; y<heigh; y++)  
  26.     {  
  27.         uchar* imgP = img.ptr<uchar>(y);  
  28.         uchar* srcP = src.ptr<uchar>(y);  
  29.         for(int x=0; x<width; x++)  
  30.         {  
  31.             int R = norm(Point(x,y)-center);  
  32.             double angle = atan2((double)(y-center.y),(double)(x-center.x));  
  33.             double delta=Pi*Para/sqrtf(R+1);  
  34.             int newX = R*cos(angle+delta) + center.x;  
  35.             int newY = R*sin(angle+delta) + center.y;  
  36.   
  37.             if(newX<0) newX=0;  
  38.             if(newX>width-1) newX=width-1;  
  39.             if(newY<0) newY=0;  
  40.             if(newY>heigh-1) newY=heigh-1;  
  41.   
  42.             imgP[3*x] = src1u[0].at<uchar>(newY,newX);  
  43.             imgP[3*x+1] = src1u[1].at<uchar>(newY,newX);  
  44.             imgP[3*x+2] = src1u[2].at<uchar>(newY,newX);  
  45.         }  
  46.     }  
  47.     imshow("vortex",img);  
  48.     waitKey();  
  49.     imwrite("D:/img/漩涡.jpg",img);  
  50. }  

原图:


漩涡:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值