我已经实现了径向小插图,这是我的代码
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);
}