gpt4 book ai didi

c++ - 在 OpenCV、C++ 中创建方形晕影

转载 作者:太空宇宙 更新时间:2023-11-03 22:57:46 27 4
gpt4 key购买 nike

我已经实现了径向晕影,这是我的代码

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);
}

现在我想在 OpenCV 中实现矩形晕影效果,请帮助我。提前致谢。

最佳答案

我按照这些步骤手动实现了方形晕影。 1. 创建一个黑色边框和中心白色的正方形。

 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.与原始图像相乘。

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);

}

关于c++ - 在 OpenCV、C++ 中创建方形晕影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25744524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com