简体   繁体   中英

Create square vignette in OpenCV, C++

i have implemented the radial vignette and this is my code

void Vignette(const Mat&img, Mat &out, jint sigma) {
Mat a, b, c, d, f;
//double sigma = 280; // vignette 'aperture', the param to play with
a = getGaussianKernel(img.cols, sigma, CV_32F);
b = getGaussianKernel(img.rows, sigma, CV_32F);
c = b * a.t();
double minVal;
double maxVal;
minMaxLoc(c, &minVal, &maxVal);
d = c / maxVal;
d.convertTo(d, CV_8UC4, 255);
cvtColor(d, d, COLOR_GRAY2RGBA);

d.convertTo(d, CV_32F, 1.0 / 255);
multiply(img, d, out, 1, CV_8UC4);
}

now i want to implement rectangular vignette effect in OpenCV, please help me with that. Thanks in advance.

I implemented square vignette manually following these steps. 1. Created a Square bordered black and center white.

 void generateSquareMask(Mat& temp1, int rowPercent, int colPercent, int r,
    int g, int b) {

cvtColor(temp1, temp1, CV_BGRA2RGB);
for (int i = 0; i < temp1.rows; ++i) {
    for (int j = 0; j < temp1.cols; ++j) {
        if (i < (int) (rowPercent / 2)
                || i > ((int) (temp1.rows - (rowPercent) / 2))
                || j < (int) (colPercent / 2)
                || ((int) temp1.cols - j) < (int) (colPercent / 2)) {
            temp1.at < cv::Vec3b > (i, j)[0] = b;
            temp1.at < cv::Vec3b > (i, j)[1] = g;
            temp1.at < cv::Vec3b > (i, j)[2] = r;
        } else {
            temp1.at < cv::Vec3b > (i, j)[0] = 255;
            temp1.at < cv::Vec3b > (i, j)[1] = 255;
            temp1.at < cv::Vec3b > (i, j)[2] = 255;
        }
    }
}
cvtColor(temp1, temp1, CV_RGB2RGBA);
blur(temp1, temp1, Size(((int) colPercent / 2), ((int) colPercent / 2)));

}

1. Multiplied with the origonal image.

void square_Vignette(const Mat&img, Mat &out, jint sigma, int r, int g, int     b) {

float cols = img.cols;
float rows = img.rows;

float temper = ((float) sigma) / 100;

float colPercent = cols * temper;
float rowPercent = rows * temper;

Mat temp1(img.size(), img.type());

generateSquareMask(temp1, ((int) rowPercent), ((int) colPercent), r, g, b);

temp1.convertTo(temp1, CV_32F, 1.0 / 255);
multiply(img, temp1, out, 1, CV_8UC4);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM