OpenCV 无缝融合seamlessClone(),colorChange(),illuminationChange(),textureFlattening()
Posted javastart
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV 无缝融合seamlessClone(),colorChange(),illuminationChange(),textureFlattening()相关的知识,希望对你有一定的参考价值。
【知识点1】
把一幅图无缝融合到另一幅图里,主要是seamlessClone() 的使用。
seamlessClone( InputArray src, InputArray dst, InputArray mask, Point p, OutputArray blend, int flags);
注意需要三幅图合为一幅图,src与mask抠图(逻辑与,尺寸一致),把抠出的图融合到dst中的p位置处(抠出的图尺寸≤dst图)。p位置也是抠出的图的中心。
3种融合模式flags:NORMAL_CLONE = 1,MIXED_CLONE = 2,MONOCHROME_TRANSFER = 3
#include<opencv2\\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
string folder = "cloning/Normal_Cloning/"; //可更换Mixed_Cloning,Monochrome_Transfer目录
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "destination1.png");
string original_path3 = samples::findFile(folder + "mask.png");
Mat source = imread(original_path1, IMREAD_COLOR);
Mat destination = imread(original_path2, IMREAD_COLOR);
Mat mask = imread(original_path3, IMREAD_COLOR);
Mat result;
Point p;
p.x = destination.size().width / 2;
p.y = destination.size().height / 2;
seamlessClone(source, destination, mask, p, result, NORMAL_CLONE); //可更换MIXED_CLONE,MONOCHROME_TRANSFER
imshow("Output", result);
imwrite("cloned.png", result);
waitKey(0);
return 0;
【知识点2】
对感兴趣区域进行颜色调整。如下图,花朵更鲜艳。主要是colorChange()函数的使用。
#include<opencv2\\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
string folder = "cloning/color_change/";
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "mask.png");
Mat source = imread(original_path1, IMREAD_COLOR);
Mat mask = imread(original_path2, IMREAD_COLOR);
Mat result;
colorChange(source, mask, result, 1.5, .5, .5); //mask定位source中的roi区域,调整该区域颜色r,g,b
imshow("Output", result);
imwrite("cloned.png", result);
waitKey(0);
return 0;
【知识点3】
消除高亮区域,illuminationChange()函数的使用。alpha,beta两个参数共同决定消除高光后图像的模糊程度(范围0~2,0比较清晰,2比较模糊)
#include<opencv2\\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
string folder = "cloning/Illumination_Change/";
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "mask.png");
Mat source = imread(original_path1, IMREAD_COLOR);
Mat mask = imread(original_path2, IMREAD_COLOR);
Mat result;
illuminationChange(source, mask, result, 0.2f, 0.4f); //消除source中mask锁定的高亮区域,后两个参数0-2调整
imshow("Output", result);
imwrite("cloned.png", result);
waitKey(0);
return 0;
【知识点4】
纹理扁平化,边缘检测器选取的边缘越少(选择性越强),边缘映射就越稀疏,扁平化效果就越明显。textureFlattening()函数的使用。
#include<opencv2\\opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
int main()
string folder = "cloning/Texture_Flattening/";
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "mask.png");
Mat source = imread(original_path1, IMREAD_COLOR);
Mat mask = imread(original_path2, IMREAD_COLOR);
Mat result;
textureFlattening(source, mask, result, 30, 45, 3); //对mask锁定的source中的区域进行纹理扁平化,低阈值,高阈值,核尺寸
imshow("Output", result);
imwrite("cloned.png", result);
waitKey(0);
return 0;
以上是关于OpenCV 无缝融合seamlessClone(),colorChange(),illuminationChange(),textureFlattening()的主要内容,如果未能解决你的问题,请参考以下文章