<span style="font-family: Arial, Helvetica, sans-serif;">#include<opencv2\opencv.hpp></span>
#include<iostream>
using namespace cv;
using namespace std;
#define PI 3.1415926
Mat waterDeal(const Mat &img);
int main() {
Mat img = imread("D://图片//1.png");
if (img.empty())
return -1;
Mat result = waterDeal(img);
imshow("result", result);
waitKey(0);
destroyAllWindows;
return 0;
}
Mat waterDeal(const Mat & img)
{
Mat result = img.clone();
if (result.type() != CV_8UC3)
result.convertTo(result, CV_8UC3);
float x_tem, y_tem, x_start, y_start, new_x, new_y;
float tem1, tem2, r_tem, r_res;
float theta;
float num1 = 2, num2 = 4; //num1越小深度越小,num2越大圈数越多
Point center(img.cols / 5, img.rows / 2); //水波的中心
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
x_start = j - center.x;
y_start = center.y - i;
theta = atan(y_start / (x_start + 0.00001)); //变换关系
if (x_start < 0)
theta += PI;
r_tem = sqrt(x_start*x_start + y_start*y_start); //每圈的半径
r_res = r_tem + img.cols*0.01*num1*sin(num2*0.1*r_tem);
//r_res = r_tem + img.cols*0.01*num1*sin(num2*r_tem);
new_x = center.x + r_res*cos(theta);
new_y = center.y - r_res*sin(theta); //使图像正立
if (new_x < 0)
new_x = 0;
if (new_x > img.cols - 2)
new_x = img.cols - 2;
if (new_y < 0)
new_y = 0;
if (new_y > img.rows - 2)
new_y = img.rows - 2;
x_tem = (int)new_x;
y_tem = (int)new_y;
tem1 = new_x - x_tem;
tem2 = new_y - y_tem;
for (int k = 0; k < 3; k++) { //联系邻域处理中心像素
result.at<Vec3b>(i, j)[k] = (1 - tem1)*(1 - tem2)*img.at<Vec3b>(y_tem, x_tem)[k] +
(tem1)*(tem2)*img.at<Vec3b>(y_tem + 1, x_tem + 1)[k] +
(1 - tem1)*(tem2)*img.at<Vec3b>(y_tem + 1, x_tem)[k] +
(tem1)*(1 - tem2)*img.at<Vec3b>(y_tem, x_tem + 1)[k];
}
}
}
return result;
}
效果:
昨天看到的时候觉得好惊喜,就自己记录下来了,方便以后的查阅
作者还有好多厉害的文章,链接:http://m.blog.csdn.net/blog/index?username=shinian1987