samples/cpp/tutorial_code/photo/seamless_cloning/cloning_demo.cpp#

An example using seamlessClone function

  1/*
  2* cloning_demo.cpp
  3*
  4* Author:
  5* Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
  6*
  7* This tutorial demonstrates how to use OpenCV seamless cloning
  8* module without GUI.
  9*
 10* 1- Normal Cloning
 11* 2- Mixed Cloning
 12* 3- Monochrome Transfer
 13* 4- Color Change
 14* 5- Illumination change
 15* 6- Texture Flattening
 16
 17* The program takes as input a source and a destination image (for 1-3 methods)
 18* and outputs the cloned image.
 19*
 20* Download test images from opencv_extra folder @github.
 21*
 22*/
 23
 24#include "opencv2/photo.hpp"
 25#include "opencv2/imgproc.hpp"
 26#include "opencv2/highgui.hpp"
 27#include "opencv2/core.hpp"
 28#include <iostream>
 29#include <stdlib.h>
 30
 31using namespace std;
 32using namespace cv;
 33
 34int main()
 35{
 36    cout << endl;
 37    cout << "Cloning Module" << endl;
 38    cout << "---------------" << endl;
 39    cout << "Options: " << endl;
 40    cout << endl;
 41    cout << "1) Normal Cloning " << endl;
 42    cout << "2) Mixed Cloning " << endl;
 43    cout << "3) Monochrome Transfer " << endl;
 44    cout << "4) Local Color Change " << endl;
 45    cout << "5) Local Illumination Change " << endl;
 46    cout << "6) Texture Flattening " << endl;
 47    cout << endl;
 48    cout << "Press number 1-6 to choose from above techniques: ";
 49    int num = 1;
 50    cin >> num;
 51    cout << endl;
 52
 53    if(num == 1)
 54    {
 55        string folder =  "cloning/Normal_Cloning/";
 56        string original_path1 = folder + "source1.png";
 57        string original_path2 = folder + "destination1.png";
 58        string original_path3 = folder + "mask.png";
 59
 60        Mat source = imread(original_path1, IMREAD_COLOR);
 61        Mat destination = imread(original_path2, IMREAD_COLOR);
 62        Mat mask = imread(original_path3, IMREAD_COLOR);
 63
 64        if(source.empty())
 65        {
 66            cout << "Could not load source image " << original_path1 << endl;
 67            exit(0);
 68        }
 69        if(destination.empty())
 70        {
 71            cout << "Could not load destination image " << original_path2 << endl;
 72            exit(0);
 73        }
 74        if(mask.empty())
 75        {
 76            cout << "Could not load mask image " << original_path3 << endl;
 77            exit(0);
 78        }
 79
 80        Mat result;
 81        Point p;
 82        p.x = 400;
 83        p.y = 100;
 84
 85        seamlessClone(source, destination, mask, p, result, 1);
 86
 87        imshow("Output",result);
 88        imwrite(folder + "cloned.png", result);
 89    }
 90    else if(num == 2)
 91    {
 92        string folder = "cloning/Mixed_Cloning/";
 93        string original_path1 = folder + "source1.png";
 94        string original_path2 = folder + "destination1.png";
 95        string original_path3 = folder + "mask.png";
 96
 97        Mat source = imread(original_path1, IMREAD_COLOR);
 98        Mat destination = imread(original_path2, IMREAD_COLOR);
 99        Mat mask = imread(original_path3, IMREAD_COLOR);
100
101        if(source.empty())
102        {
103            cout << "Could not load source image " << original_path1 << endl;
104            exit(0);
105        }
106        if(destination.empty())
107        {
108            cout << "Could not load destination image " << original_path2 << endl;
109            exit(0);
110        }
111        if(mask.empty())
112        {
113            cout << "Could not load mask image " << original_path3 << endl;
114            exit(0);
115        }
116
117        Mat result;
118        Point p;
119        p.x = destination.size().width/2;
120        p.y = destination.size().height/2;
121
122        seamlessClone(source, destination, mask, p, result, 2);
123
124        imshow("Output",result);
125        imwrite(folder + "cloned.png", result);
126    }
127    else if(num == 3)
128    {
129        string folder = "cloning/Monochrome_Transfer/";
130        string original_path1 = folder + "source1.png";
131        string original_path2 = folder + "destination1.png";
132        string original_path3 = folder + "mask.png";
133
134        Mat source = imread(original_path1, IMREAD_COLOR);
135        Mat destination = imread(original_path2, IMREAD_COLOR);
136        Mat mask = imread(original_path3, IMREAD_COLOR);
137
138        if(source.empty())
139        {
140            cout << "Could not load source image " << original_path1 << endl;
141            exit(0);
142        }
143        if(destination.empty())
144        {
145            cout << "Could not load destination image " << original_path2 << endl;
146            exit(0);
147        }
148        if(mask.empty())
149        {
150            cout << "Could not load mask image " << original_path3 << endl;
151            exit(0);
152        }
153
154        Mat result;
155        Point p;
156        p.x = destination.size().width/2;
157        p.y = destination.size().height/2;
158
159        seamlessClone(source, destination, mask, p, result, 3);
160
161        imshow("Output",result);
162        imwrite(folder + "cloned.png", result);
163    }
164    else if(num == 4)
165    {
166        string folder = "cloning/Color_Change/";
167        string original_path1 = folder + "source1.png";
168        string original_path2 = folder + "mask.png";
169
170        Mat source = imread(original_path1, IMREAD_COLOR);
171        Mat mask = imread(original_path2, IMREAD_COLOR);
172
173        if(source.empty())
174        {
175            cout << "Could not load source image " << original_path1 << endl;
176            exit(0);
177        }
178        if(mask.empty())
179        {
180            cout << "Could not load mask image " << original_path2 << endl;
181            exit(0);
182        }
183
184        Mat result;
185
186        colorChange(source, mask, result, 1.5, .5, .5);
187
188        imshow("Output",result);
189        imwrite(folder + "cloned.png", result);
190    }
191    else if(num == 5)
192    {
193        string folder = "cloning/Illumination_Change/";
194        string original_path1 = folder + "source1.png";
195        string original_path2 = folder + "mask.png";
196
197        Mat source = imread(original_path1, IMREAD_COLOR);
198        Mat mask = imread(original_path2, IMREAD_COLOR);
199
200        if(source.empty())
201        {
202            cout << "Could not load source image " << original_path1 << endl;
203            exit(0);
204        }
205        if(mask.empty())
206        {
207            cout << "Could not load mask image " << original_path2 << endl;
208            exit(0);
209        }
210
211        Mat result;
212
213        illuminationChange(source, mask, result, 0.2f, 0.4f);
214
215        imshow("Output",result);
216        imwrite(folder + "cloned.png", result);
217    }
218    else if(num == 6)
219    {
220        string folder = "cloning/Texture_Flattening/";
221        string original_path1 = folder + "source1.png";
222        string original_path2 = folder + "mask.png";
223
224        Mat source = imread(original_path1, IMREAD_COLOR);
225        Mat mask = imread(original_path2, IMREAD_COLOR);
226
227        if(source.empty())
228        {
229            cout << "Could not load source image " << original_path1 << endl;
230            exit(0);
231        }
232        if(mask.empty())
233        {
234            cout << "Could not load mask image " << original_path2 << endl;
235            exit(0);
236        }
237
238        Mat result;
239
240        textureFlattening(source, mask, result, 30, 45, 3);
241
242        imshow("Output",result);
243        imwrite(folder + "cloned.png", result);
244    }
245    waitKey(0);
246}