【原文:http://blog.youkuaiyun.com/yangtrees/article/details/9114283】
==============================================
版权所有:小熊不去实验室优快云博客
==============================================
设图象的漩涡中心为o,设p为图像任意点,只要对p做绕o的旋转,且旋转角随p-o距离的增加而减少,就是"漩涡"变换了.
- #include<math.h>
- #include <opencv/cv.h>
- #include <opencv/highgui.h>
- using namespace cv;
- using namespace std;
- template<typename T> T sqr(T x){return x*x;}
- double Pi=3.14;
- double Para=20;
- int main()
- {
- Mat src = imread("D:/img/face01.jpg",1);
- int heigh = src.rows;
- int width = src.cols;
- Point center(width/2, heigh/2);
- Mat img;
- src.copyTo(img);
- Mat src1u[3];
- split(src,src1u);
- for (int y=0; y<heigh; y++)
- {
- uchar* imgP = img.ptr<uchar>(y);
- uchar* srcP = src.ptr<uchar>(y);
- for(int x=0; x<width; x++)
- {
- int R = norm(Point(x,y)-center);
- double angle = atan2((double)(y-center.y),(double)(x-center.x));
- double delta=Pi*Para/sqrtf(R+1);
- int newX = R*cos(angle+delta) + center.x;
- int newY = R*sin(angle+delta) + center.y;
- if(newX<0) newX=0;
- if(newX>width-1) newX=width-1;
- if(newY<0) newY=0;
- if(newY>heigh-1) newY=heigh-1;
- imgP[3*x] = src1u[0].at<uchar>(newY,newX);
- imgP[3*x+1] = src1u[1].at<uchar>(newY,newX);
- imgP[3*x+2] = src1u[2].at<uchar>(newY,newX);
- }
- }
- imshow("vortex",img);
- waitKey();
- imwrite("D:/img/漩涡.jpg",img);
- }
原图:
漩涡:
本文介绍了一种使用OpenCV实现图像漩涡变换的方法,通过旋转和缩放角度随距离变化来创建漩涡效果,并提供了完整的C++代码示例。
2万+

被折叠的 条评论
为什么被折叠?



