一个线性渐变的光晕,opencv cpp实现

原图

光晕

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# C++
Mat input_img = imread("/Users/nolan/Desktop/1.jpg");
Mat out_img = Mat::zeros( input_img.size(), input_img.type() );
input_img.copyTo(out_img);
int h = input_img.rows;
int w = input_img.cols;
int center_x = w / 2;
int center_y = h / 2;
int radius = min(center_x, center_y);
for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
        int distance = pow( y - center_y, 2) + pow(x - center_x, 2);
        if (distance < radius * radius) {
            int result = (int) (strength * (1.0 - sqrt(distance) / radius));
            output_img.at<Vec3b>(y, x)[0] = min(255, max(0, output_img.at<Vec3b>(y, x)[0] + result));
            output_img.at<Vec3b>(y, x)[1] = min(255, max(0, output_img.at<Vec3b>(y, x)[1] + result));
            output_img.at<Vec3b>(y, x)[2] = min(255, max(0, output_img.at<Vec3b>(y, x)[2] + result));
        }
    }
}

cv::imshow("output_img", output_img);
cv::waitKey(0);