opencv/samples/cpp/cloning_demo.cpp

252 lines
7.2 KiB
C++
Raw Normal View History

2013-09-13 20:55:23 +08:00
/*
* cloning_demo.cpp
*
* Author:
* Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
*
* This tutorial demonstrates how to use OpenCV seamless cloning
* module without GUI.
*
* 1- Normal Cloning
* 2- Mixed Cloning
* 3- Monochrome Transfer
* 4- Color Change
* 5- Illumination change
* 6- Texture Flattening
* The program takes as input a source and a destination image (for 1-3 methods)
* and outputs the cloned image.
2013-12-04 21:42:43 +08:00
*
2018-10-31 20:48:56 +08:00
* Download test images from opencv_extra repository.
2013-09-13 20:55:23 +08:00
*/
#include "opencv2/photo.hpp"
#include "opencv2/imgproc.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
2013-09-13 20:55:23 +08:00
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
#include <iostream>
using namespace std;
using namespace cv;
2013-09-14 19:17:44 +08:00
int main()
2013-09-13 20:55:23 +08:00
{
cout << endl;
2018-10-31 20:48:56 +08:00
cout << "Note: specify OPENCV_SAMPLES_DATA_PATH_HINT=<opencv_extra>/testdata/cv" << endl << endl;
2013-09-13 20:55:23 +08:00
cout << "Cloning Module" << endl;
cout << "---------------" << endl;
cout << "Options: " << endl;
cout << endl;
cout << "1) Normal Cloning " << endl;
cout << "2) Mixed Cloning " << endl;
cout << "3) Monochrome Transfer " << endl;
cout << "4) Local Color Change " << endl;
cout << "5) Local Illumination Change " << endl;
cout << "6) Texture Flattening " << endl;
cout << endl;
cout << "Press number 1-6 to choose from above techniques: ";
int num = 1;
2013-09-13 20:55:23 +08:00
cin >> num;
cout << endl;
2013-09-13 20:55:23 +08:00
if(num == 1)
{
string folder = "cloning/Normal_Cloning/";
2018-10-31 20:48:56 +08:00
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "destination1.png");
string original_path3 = samples::findFile(folder + "mask.png");
2013-09-13 20:55:23 +08:00
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 << "Could not load source image " << original_path1 << endl;
exit(0);
}
if(destination.empty())
2013-09-13 20:55:23 +08:00
{
cout << "Could not load destination image " << original_path2 << endl;
exit(0);
}
if(mask.empty())
{
cout << "Could not load mask image " << original_path3 << endl;
exit(0);
}
Mat result;
Point p;
p.x = 400;
p.y = 100;
2013-09-13 20:55:23 +08:00
seamlessClone(source, destination, mask, p, result, 1);
2013-09-13 20:55:23 +08:00
imshow("Output",result);
2018-10-31 20:48:56 +08:00
imwrite("cloned.png", result);
2013-09-13 20:55:23 +08:00
}
else if(num == 2)
{
2013-09-13 20:55:23 +08:00
string folder = "cloning/Mixed_Cloning/";
2018-10-31 20:48:56 +08:00
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "destination1.png");
string original_path3 = samples::findFile(folder + "mask.png");
2013-09-13 20:55:23 +08:00
Mat source = imread(original_path1, IMREAD_COLOR);
Mat destination = imread(original_path2, IMREAD_COLOR);
Mat mask = imread(original_path3, IMREAD_COLOR);
if(source.empty())
2013-09-13 20:55:23 +08:00
{
cout << "Could not load source image " << original_path1 << endl;
exit(0);
}
if(destination.empty())
{
cout << "Could not load destination image " << original_path2 << endl;
exit(0);
}
if(mask.empty())
2013-09-13 20:55:23 +08:00
{
cout << "Could not load mask image " << original_path3 << endl;
exit(0);
}
Mat result;
Point p;
p.x = destination.size().width/2;
p.y = destination.size().height/2;
2013-09-13 20:55:23 +08:00
seamlessClone(source, destination, mask, p, result, 2);
imshow("Output",result);
2018-10-31 20:48:56 +08:00
imwrite("cloned.png", result);
2013-09-13 20:55:23 +08:00
}
else if(num == 3)
{
string folder = "cloning/Monochrome_Transfer/";
2018-10-31 20:48:56 +08:00
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "destination1.png");
string original_path3 = samples::findFile(folder + "mask.png");
2013-09-13 20:55:23 +08:00
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 << "Could not load source image " << original_path1 << endl;
exit(0);
}
if(destination.empty())
2013-09-13 20:55:23 +08:00
{
cout << "Could not load destination image " << original_path2 << endl;
exit(0);
}
if(mask.empty())
2013-09-13 20:55:23 +08:00
{
cout << "Could not load mask image " << original_path3 << endl;
exit(0);
}
Mat result;
Point p;
p.x = destination.size().width/2;
p.y = destination.size().height/2;
2013-09-13 20:55:23 +08:00
seamlessClone(source, destination, mask, p, result, 3);
imshow("Output",result);
2018-10-31 20:48:56 +08:00
imwrite("cloned.png", result);
2013-09-13 20:55:23 +08:00
}
else if(num == 4)
{
2018-10-31 20:48:56 +08:00
string folder = "cloning/color_change/";
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "mask.png");
2013-09-13 20:55:23 +08:00
Mat source = imread(original_path1, IMREAD_COLOR);
Mat mask = imread(original_path2, IMREAD_COLOR);
if(source.empty())
{
cout << "Could not load source image " << original_path1 << endl;
exit(0);
}
if(mask.empty())
{
cout << "Could not load mask image " << original_path2 << endl;
exit(0);
}
Mat result;
2013-09-13 20:55:23 +08:00
colorChange(source, mask, result, 1.5, .5, .5);
imshow("Output",result);
2018-10-31 20:48:56 +08:00
imwrite("cloned.png", result);
2013-09-13 20:55:23 +08:00
}
else if(num == 5)
{
string folder = "cloning/Illumination_Change/";
2018-10-31 20:48:56 +08:00
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "mask.png");
2013-09-13 20:55:23 +08:00
Mat source = imread(original_path1, IMREAD_COLOR);
Mat mask = imread(original_path2, IMREAD_COLOR);
if(source.empty())
2013-09-13 20:55:23 +08:00
{
cout << "Could not load source image " << original_path1 << endl;
exit(0);
}
if(mask.empty())
{
cout << "Could not load mask image " << original_path2 << endl;
exit(0);
}
Mat result;
2013-12-06 17:29:53 +08:00
illuminationChange(source, mask, result, 0.2f, 0.4f);
2013-09-13 20:55:23 +08:00
imshow("Output",result);
2018-10-31 20:48:56 +08:00
imwrite("cloned.png", result);
2013-09-13 20:55:23 +08:00
}
else if(num == 6)
{
string folder = "cloning/Texture_Flattening/";
2018-10-31 20:48:56 +08:00
string original_path1 = samples::findFile(folder + "source1.png");
string original_path2 = samples::findFile(folder + "mask.png");
2013-09-13 20:55:23 +08:00
Mat source = imread(original_path1, IMREAD_COLOR);
Mat mask = imread(original_path2, IMREAD_COLOR);
if(source.empty())
{
cout << "Could not load source image " << original_path1 << endl;
exit(0);
}
if(mask.empty())
{
cout << "Could not load mask image " << original_path2 << endl;
exit(0);
}
Mat result;
2013-09-13 20:55:23 +08:00
textureFlattening(source, mask, result, 30, 45, 3);
imshow("Output",result);
2018-10-31 20:48:56 +08:00
imwrite("cloned.png", result);
}
else
{
cerr << "Invalid selection: " << num << endl;
exit(1);
2013-09-13 20:55:23 +08:00
}
waitKey(0);
}