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