OpenCV 无缝融合seamlessClone(),调试颜色colorChange(),消除高亮illuminationChange(),纹理扁平化textureFlattening()(OpenCV案例源码cloning_demo.cpp解读)

【知识点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

 1 #include<opencv2\opencv.hpp>
 2 #include<iostream>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     string folder = "cloning/Normal_Cloning/"; //可更换Mixed_Cloning,Monochrome_Transfer目录
10     string original_path1 = samples::findFile(folder + "source1.png");
11     string original_path2 = samples::findFile(folder + "destination1.png");
12     string original_path3 = samples::findFile(folder + "mask.png");
13 
14     Mat source = imread(original_path1, IMREAD_COLOR);
15     Mat destination = imread(original_path2, IMREAD_COLOR);
16     Mat mask = imread(original_path3, IMREAD_COLOR);
17 
18     Mat result;
19     Point p;
20     p.x = destination.size().width / 2;
21     p.y = destination.size().height / 2;
22 
23     seamlessClone(source, destination, mask, p, result, NORMAL_CLONE); //可更换MIXED_CLONE,MONOCHROME_TRANSFER
24 
25     imshow("Output", result);
26     imwrite("cloned.png", result);
27 
28     waitKey(0);
29     return 0;
30 }

【知识点2】

对感兴趣区域进行颜色调整。如下图,花朵更鲜艳。主要是colorChange()函数的使用。

 1 #include<opencv2\opencv.hpp>
 2 #include<iostream>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     string folder = "cloning/color_change/";
10     string original_path1 = samples::findFile(folder + "source1.png");
11     string original_path2 = samples::findFile(folder + "mask.png");
12 
13     Mat source = imread(original_path1, IMREAD_COLOR);
14     Mat mask = imread(original_path2, IMREAD_COLOR);
15 
16     Mat result;
17     colorChange(source, mask, result, 1.5, .5, .5); //mask定位source中的roi区域,调整该区域颜色r,g,b
18 
19     imshow("Output", result);
20     imwrite("cloned.png", result);
21 
22     waitKey(0);
23     return 0;
24 }

【知识点3】

消除高亮区域,illuminationChange()函数的使用。alpha,beta两个参数共同决定消除高光后图像的模糊程度(范围0~2,0比较清晰,2比较模糊)

 1 #include<opencv2\opencv.hpp>
 2 #include<iostream>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     string folder = "cloning/Illumination_Change/";
10     string original_path1 = samples::findFile(folder + "source1.png");
11     string original_path2 = samples::findFile(folder + "mask.png");
12 
13     Mat source = imread(original_path1, IMREAD_COLOR);
14     Mat mask = imread(original_path2, IMREAD_COLOR);
15 
16     Mat result;
17 
18     illuminationChange(source, mask, result, 0.2f, 0.4f); //消除source中mask锁定的高亮区域,后两个参数0-2调整
19 
20     imshow("Output", result);
21     imwrite("cloned.png", result);
22 
23     waitKey(0);
24     return 0;
25 }

【知识点4】

纹理扁平化,边缘检测器选取的边缘越少(选择性越强),边缘映射就越稀疏,扁平化效果就越明显。textureFlattening()函数的使用。

 1 #include<opencv2\opencv.hpp>
 2 #include<iostream>
 3 
 4 using namespace cv;
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     string folder = "cloning/Texture_Flattening/";
10     string original_path1 = samples::findFile(folder + "source1.png");
11     string original_path2 = samples::findFile(folder + "mask.png");
12 
13     Mat source = imread(original_path1, IMREAD_COLOR);
14     Mat mask = imread(original_path2, IMREAD_COLOR);
15 
16     Mat result;
17 
18     textureFlattening(source, mask, result, 30, 45, 3); //对mask锁定的source中的区域进行纹理扁平化,低阈值,高阈值,核尺寸
19 
20     imshow("Output", result);
21     imwrite("cloned.png", result);
22 
23     waitKey(0);
24     return 0;
25 }
原文地址:https://www.cnblogs.com/ybqjymy/p/15699052.html