您的位置:首页 > 其它

图片单色渐变效果叠加

2016-06-11 00:04 204 查看
<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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: