水波效果

<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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值