图片单色渐变效果叠加

<span style="font-family: Arial, Helvetica, sans-serif;">#include<iostream></span>
using namespace cv;
using namespace std;
int main() {
	Mat img1 = imread("D://图片//1.png");
	if (!img1.data)
		return -1;
	img1.convertTo(img1, CV_32F);
	Mat result = img1.clone();

	Point2f center(img1.cols / 2, img1.rows / 2);
	Point2f org(0, 0);
	float dis;

	if (org.x <= center.x && org.y <= center.y) {
		dis = sqrt((img1.cols - 1 - org.x)*(img1.cols - 1 - org.x) + (img1.rows - 1 - org.y)*(img1.rows - 1 - org.y));
	}
	else if (org.x <= center.x && org.y > center.y) {
		dis = sqrt((img1.cols - 1)*(img1.cols - 1) + org.y * org.y);
	}
	else if (org.x > center.x && org.y <= center.y) {
		dis = sqrt(org.x * org.x + (img1.rows - 1)*(img1.rows - 1));
	}
	else {
		dis = sqrt(org.x * org.x + org.y * org.y);
	}

	Scalar c1(255, 255, 255);
	Scalar c2(0, 0, 0);

	float weightB = (c1[0] - c2[0]) / dis;
	float weightG = (c1[1] - c2[1]) / dis;
	float weightR = (c1[2] - c2[2]) / dis;

	float dis2;
	for (int i = 0; i < result.rows; i++) {
		float *pp = result.ptr<float>(i);
		for (int j = 0; j < result.cols; j++) {

			dis2 = sqrt((i - org.y)*(i - org.y) + (j - org.x)*(j - org.x));
			pp[j * 3 + 0] += weightB*dis2;
			pp[j * 3 + 1] += weightG*dis2;
			pp[j * 3 + 2] += weightR*dis2;

		}
	}

	result.convertTo(result, CV_8U);
	imshow("result", result);
	waitKey(0);
	destroyAllWindows;
	return 0;
}

原图:

效果:


参考博主:http://blog.csdn.net/matrix_space

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值