cloning_demo.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * The program takes as input a source and a destination image (for 1-3 methods)
  17. * and outputs the cloned image.
  18. *
  19. * Download test images from opencv_extra repository.
  20. */
  21. #include "opencv2/photo.hpp"
  22. #include "opencv2/imgproc.hpp"
  23. #include "opencv2/imgcodecs.hpp"
  24. #include "opencv2/highgui.hpp"
  25. #include "opencv2/core.hpp"
  26. #include <iostream>
  27. using namespace std;
  28. using namespace cv;
  29. int main()
  30. {
  31. cout << endl;
  32. cout << "Note: specify OPENCV_SAMPLES_DATA_PATH_HINT=<opencv_extra>/testdata/cv" << endl << endl;
  33. cout << "Cloning Module" << endl;
  34. cout << "---------------" << endl;
  35. cout << "Options: " << endl;
  36. cout << endl;
  37. cout << "1) Normal Cloning " << endl;
  38. cout << "2) Mixed Cloning " << endl;
  39. cout << "3) Monochrome Transfer " << endl;
  40. cout << "4) Local Color Change " << endl;
  41. cout << "5) Local Illumination Change " << endl;
  42. cout << "6) Texture Flattening " << endl;
  43. cout << endl;
  44. cout << "Press number 1-6 to choose from above techniques: ";
  45. int num = 1;
  46. cin >> num;
  47. cout << endl;
  48. if(num == 1)
  49. {
  50. string folder = "cloning/Normal_Cloning/";
  51. string original_path1 = samples::findFile(folder + "source1.png");
  52. string original_path2 = samples::findFile(folder + "destination1.png");
  53. string original_path3 = samples::findFile(folder + "mask.png");
  54. Mat source = imread(original_path1, IMREAD_COLOR);
  55. Mat destination = imread(original_path2, IMREAD_COLOR);
  56. Mat mask = imread(original_path3, IMREAD_COLOR);
  57. if(source.empty())
  58. {
  59. cout << "Could not load source image " << original_path1 << endl;
  60. exit(0);
  61. }
  62. if(destination.empty())
  63. {
  64. cout << "Could not load destination image " << original_path2 << endl;
  65. exit(0);
  66. }
  67. if(mask.empty())
  68. {
  69. cout << "Could not load mask image " << original_path3 << endl;
  70. exit(0);
  71. }
  72. Mat result;
  73. Point p;
  74. p.x = 400;
  75. p.y = 100;
  76. seamlessClone(source, destination, mask, p, result, 1);
  77. imshow("Output",result);
  78. imwrite("cloned.png", result);
  79. }
  80. else if(num == 2)
  81. {
  82. string folder = "cloning/Mixed_Cloning/";
  83. string original_path1 = samples::findFile(folder + "source1.png");
  84. string original_path2 = samples::findFile(folder + "destination1.png");
  85. string original_path3 = samples::findFile(folder + "mask.png");
  86. Mat source = imread(original_path1, IMREAD_COLOR);
  87. Mat destination = imread(original_path2, IMREAD_COLOR);
  88. Mat mask = imread(original_path3, IMREAD_COLOR);
  89. if(source.empty())
  90. {
  91. cout << "Could not load source image " << original_path1 << endl;
  92. exit(0);
  93. }
  94. if(destination.empty())
  95. {
  96. cout << "Could not load destination image " << original_path2 << endl;
  97. exit(0);
  98. }
  99. if(mask.empty())
  100. {
  101. cout << "Could not load mask image " << original_path3 << endl;
  102. exit(0);
  103. }
  104. Mat result;
  105. Point p;
  106. p.x = destination.size().width/2;
  107. p.y = destination.size().height/2;
  108. seamlessClone(source, destination, mask, p, result, 2);
  109. imshow("Output",result);
  110. imwrite("cloned.png", result);
  111. }
  112. else if(num == 3)
  113. {
  114. string folder = "cloning/Monochrome_Transfer/";
  115. string original_path1 = samples::findFile(folder + "source1.png");
  116. string original_path2 = samples::findFile(folder + "destination1.png");
  117. string original_path3 = samples::findFile(folder + "mask.png");
  118. Mat source = imread(original_path1, IMREAD_COLOR);
  119. Mat destination = imread(original_path2, IMREAD_COLOR);
  120. Mat mask = imread(original_path3, IMREAD_COLOR);
  121. if(source.empty())
  122. {
  123. cout << "Could not load source image " << original_path1 << endl;
  124. exit(0);
  125. }
  126. if(destination.empty())
  127. {
  128. cout << "Could not load destination image " << original_path2 << endl;
  129. exit(0);
  130. }
  131. if(mask.empty())
  132. {
  133. cout << "Could not load mask image " << original_path3 << endl;
  134. exit(0);
  135. }
  136. Mat result;
  137. Point p;
  138. p.x = destination.size().width/2;
  139. p.y = destination.size().height/2;
  140. seamlessClone(source, destination, mask, p, result, 3);
  141. imshow("Output",result);
  142. imwrite("cloned.png", result);
  143. }
  144. else if(num == 4)
  145. {
  146. string folder = "cloning/color_change/";
  147. string original_path1 = samples::findFile(folder + "source1.png");
  148. string original_path2 = samples::findFile(folder + "mask.png");
  149. Mat source = imread(original_path1, IMREAD_COLOR);
  150. Mat mask = imread(original_path2, IMREAD_COLOR);
  151. if(source.empty())
  152. {
  153. cout << "Could not load source image " << original_path1 << endl;
  154. exit(0);
  155. }
  156. if(mask.empty())
  157. {
  158. cout << "Could not load mask image " << original_path2 << endl;
  159. exit(0);
  160. }
  161. Mat result;
  162. colorChange(source, mask, result, 1.5, .5, .5);
  163. imshow("Output",result);
  164. imwrite("cloned.png", result);
  165. }
  166. else if(num == 5)
  167. {
  168. string folder = "cloning/Illumination_Change/";
  169. string original_path1 = samples::findFile(folder + "source1.png");
  170. string original_path2 = samples::findFile(folder + "mask.png");
  171. Mat source = imread(original_path1, IMREAD_COLOR);
  172. Mat mask = imread(original_path2, IMREAD_COLOR);
  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. Mat result;
  184. illuminationChange(source, mask, result, 0.2f, 0.4f);
  185. imshow("Output",result);
  186. imwrite("cloned.png", result);
  187. }
  188. else if(num == 6)
  189. {
  190. string folder = "cloning/Texture_Flattening/";
  191. string original_path1 = samples::findFile(folder + "source1.png");
  192. string original_path2 = samples::findFile(folder + "mask.png");
  193. Mat source = imread(original_path1, IMREAD_COLOR);
  194. Mat mask = imread(original_path2, IMREAD_COLOR);
  195. if(source.empty())
  196. {
  197. cout << "Could not load source image " << original_path1 << endl;
  198. exit(0);
  199. }
  200. if(mask.empty())
  201. {
  202. cout << "Could not load mask image " << original_path2 << endl;
  203. exit(0);
  204. }
  205. Mat result;
  206. textureFlattening(source, mask, result, 30, 45, 3);
  207. imshow("Output",result);
  208. imwrite("cloned.png", result);
  209. }
  210. else
  211. {
  212. cerr << "Invalid selection: " << num << endl;
  213. exit(1);
  214. }
  215. waitKey(0);
  216. }