OpenCV  4.10.0
开源计算机视觉
正在加载...
正在搜索...
没有匹配项
samples/cpp/tutorial_code/photo/seamless_cloning/cloning_demo.cpp

使用 seamlessClone 函数的示例

/*
* cloning_demo.cpp
*
* 作者
* Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
*
* 本教程演示了如何在没有 GUI 的情况下使用 OpenCV 无缝克隆
* 模块。
*
* 1- 普通克隆
* 2- 混合克隆
* 3- 单色转移
* 4- 颜色变化
* 5- 照明变化
* 6- 纹理平滑
* 程序以源图像和目标图像(用于方法 1-3)作为输入
* 并输出克隆后的图像。
*
* 从 github 上的 opencv_extra 文件夹下载测试图像。
*
*/
#include "opencv2/core.hpp"
#include <iostream>
#include <stdlib.h>
using namespace std;
using namespace cv;
int main()
{
cout << endl;
cout << "克隆模块" << endl;
cout << "---------------" << endl;
cout << "选项: " << endl;
cout << endl;
cout << "1) 普通克隆 " << endl;
cout << "2) 混合克隆 " << endl;
cout << "3) 单色转移 " << endl;
cout << "4) 局部颜色变化 " << endl;
cout << "5) 局部照明变化 " << endl;
cout << "6) 纹理平滑 " << endl;
cout << endl;
cout << "按数字 1-6 从以上技术中选择: ";
int num = 1;
cin >> num;
cout << endl;
if(num == 1)
{
string folder = "cloning/Normal_Cloning/";
string original_path1 = folder + "source1.png";
string original_path2 = folder + "destination1.png";
string original_path3 = folder + "mask.png";
Mat source = imread(original_path1, IMREAD_COLOR);
Mat destination = imread(original_path2, IMREAD_COLOR);
Mat mask = imread(original_path3, IMREAD_COLOR);
if(source.empty())
{
cout << "无法加载源图像 " << original_path1 << endl;
exit(0);
}
if(destination.empty())
{
cout << "无法加载目标图像 " << original_path2 << endl;
exit(0);
}
if(mask.empty())
{
cout << "无法加载掩码图像 " << original_path3 << endl;
exit(0);
}
Mat result;
Point p;
p.x = 400;
p.y = 100;
seamlessClone(source, destination, mask, p, result, 1);
imshow("输出",result);
imwrite(folder + "cloned.png", result);
}
else if(num == 2)
{
string folder = "cloning/Mixed_Cloning/";
string original_path1 = folder + "source1.png";
string original_path2 = folder + "destination1.png";
string original_path3 = folder + "mask.png";
Mat source = imread(original_path1, IMREAD_COLOR);
Mat destination = imread(original_path2, IMREAD_COLOR);
Mat mask = imread(original_path3, IMREAD_COLOR);
if(source.empty())
{
cout << "无法加载源图像 " << original_path1 << endl;
exit(0);
}
if(destination.empty())
{
cout << "无法加载目标图像 " << original_path2 << endl;
exit(0);
}
if(mask.empty())
{
cout << "无法加载掩码图像 " << original_path3 << endl;
exit(0);
}
Mat result;
Point p;
p.x = destination.size().width/2;
p.y = destination.size().height/2;
seamlessClone(source, destination, mask, p, result, 2);
imshow("输出",result);
imwrite(folder + "cloned.png", result);
}
else if(num == 3)
{
string folder = "cloning/Monochrome_Transfer/";
string original_path1 = folder + "source1.png";
string original_path2 = folder + "destination1.png";
string original_path3 = folder + "mask.png";
Mat source = imread(original_path1, IMREAD_COLOR);
Mat destination = imread(original_path2, IMREAD_COLOR);
Mat mask = imread(original_path3, IMREAD_COLOR);
if(source.empty())
{
cout << "无法加载源图像 " << original_path1 << endl;
exit(0);
}
if(destination.empty())
{
cout << "无法加载目标图像 " << original_path2 << endl;
exit(0);
}
if(mask.empty())
{
cout << "无法加载掩码图像 " << original_path3 << endl;
exit(0);
}
Mat result;
Point p;
p.x = destination.size().width/2;
p.y = destination.size().height/2;
seamlessClone(source, destination, mask, p, result, 3);
imshow("输出",result);
imwrite(folder + "cloned.png", result);
}
else if(num == 4)
{
string folder = "cloning/Color_Change/";
string original_path1 = folder + "source1.png";
string original_path2 = folder + "mask.png";
Mat source = imread(original_path1, IMREAD_COLOR);
Mat mask = imread(original_path2, IMREAD_COLOR);
if(source.empty())
{
cout << "无法加载源图像 " << original_path1 << endl;
exit(0);
}
if(mask.empty())
{
cout << "无法加载掩码图像 " << original_path2 << endl;
exit(0);
}
Mat result;
colorChange(source, mask, result, 1.5, .5, .5);
imshow("输出",result);
imwrite(folder + "cloned.png", result);
}
else if(num == 5)
{
string folder = "cloning/Illumination_Change/";
string original_path1 = folder + "source1.png";
string original_path2 = folder + "mask.png";
Mat source = imread(original_path1, IMREAD_COLOR);
Mat mask = imread(original_path2, IMREAD_COLOR);
if(source.empty())
{
cout << "无法加载源图像 " << original_path1 << endl;
exit(0);
}
if(mask.empty())
{
cout << "无法加载掩码图像 " << original_path2 << endl;
exit(0);
}
Mat result;
illuminationChange(source, mask, result, 0.2f, 0.4f);
imshow("输出",result);
imwrite(folder + "cloned.png", result);
}
else if(num == 6)
{
string folder = "cloning/Texture_Flattening/";
string original_path1 = folder + "source1.png";
string original_path2 = folder + "mask.png";
Mat source = imread(original_path1, IMREAD_COLOR);
Mat mask = imread(original_path2, IMREAD_COLOR);
if(source.empty())
{
cout << "无法加载源图像 " << original_path1 << endl;
exit(0);
}
if(mask.empty())
{
cout << "无法加载掩码图像 " << original_path2 << endl;
exit(0);
}
Mat result;
textureFlattening(source, mask, result, 30, 45, 3);
imshow("输出",result);
imwrite(folder + "cloned.png", result);
}
waitKey(0);
}
n 维密集数组类
定义 mat.hpp:812
MatSize size
定义 mat.hpp:2160
bool empty() const
如果数组没有元素,则返回 true。
_Tp y
点的 y 坐标
定义 types.hpp:202
_Tp x
点的 x 坐标
定义 types.hpp:201
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
与磁盘上的文件关联的文件存储的“黑盒”表示。
定义 core.hpp:102
STL 命名空间。