【知識點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 }