oil.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <opencv2/core.hpp>
  2. #include <opencv2/highgui.hpp>
  3. #include <opencv2/imgproc.hpp>
  4. #include <opencv2/xphoto.hpp>
  5. #include "opencv2/xphoto/oilpainting.hpp"
  6. #include <iostream>
  7. using namespace cv;
  8. using namespace std;
  9. static void TrackSlider(int , void *);
  10. static void addSlider(String sliderName, String windowName, int minSlider, int maxSlider, int valDefault, int *valSlider, void(*f)(int, void *), void *r);
  11. vector<int> colorSpace = { COLOR_BGR2GRAY,COLOR_BGR2HSV,COLOR_BGR2YUV,COLOR_BGR2XYZ };
  12. struct OilImage {
  13. String winName = "Oil painting";
  14. int size;
  15. int dynRatio;
  16. int colorSpace;
  17. Mat img;
  18. };
  19. const String keys =
  20. "{Help h usage ? help | | Print this message }"
  21. "{v | 0 | video index }"
  22. "{a | 700 | API index }"
  23. "{s | 10 | neighbouring size }"
  24. "{d | 1 | dynamic ratio }"
  25. "{c | 0 | color space }"
  26. "{@arg1 | | file path}"
  27. ;
  28. int main(int argc, char* argv[])
  29. {
  30. CommandLineParser parser(argc, argv, keys);
  31. if (parser.has("help"))
  32. {
  33. parser.printMessage();
  34. return 0;
  35. }
  36. String filename = parser.get<String>(0);
  37. OilImage p;
  38. p.dynRatio = parser.get<int>("d");
  39. p.size = parser.get<int>("s");
  40. p.colorSpace = parser.get<int>("c");
  41. if (p.colorSpace < 0 || p.colorSpace >= static_cast<int>(colorSpace.size()))
  42. {
  43. std::cout << "Color space must be >= 0 and <"<< colorSpace.size()<<"\n";
  44. return EXIT_FAILURE;
  45. }
  46. if (!filename.empty())
  47. {
  48. p.img = imread(filename);
  49. if (p.img.empty())
  50. {
  51. std::cout << "Check file path!\n";
  52. return EXIT_FAILURE;
  53. }
  54. Mat dst;
  55. xphoto::oilPainting(p.img, dst, p.size, p.dynRatio, colorSpace[p.colorSpace]);
  56. imshow("oil painting effect", dst);
  57. waitKey();
  58. return 0;
  59. }
  60. VideoCapture v(parser.get<int>("v")+ parser.get<int>("a"));
  61. v>> p.img;
  62. p.winName="Oil Painting";
  63. namedWindow(p.winName);
  64. addSlider("DynRatio", p.winName, 1,127,p.dynRatio,&p.dynRatio, TrackSlider, &p);
  65. addSlider("Size", p.winName, 1, 100, p.size, &p.size, TrackSlider, &p);
  66. addSlider("ColorSpace", p.winName, 0, static_cast<int>(colorSpace.size()-1), p.colorSpace, &p.colorSpace, TrackSlider, &p);
  67. while (waitKey(20) != 27)
  68. {
  69. v>>p.img;
  70. imshow("Original", p.img);
  71. TrackSlider(0, &p);
  72. waitKey(10);
  73. }
  74. return 0;
  75. }
  76. void addSlider(String sliderName, String windowName, int minSlider, int maxSlider, int valDefault, int *valSlider, void(*f)(int, void *), void *r)
  77. {
  78. createTrackbar(sliderName, windowName, valSlider, 1, f, r);
  79. setTrackbarMin(sliderName, windowName, minSlider);
  80. setTrackbarMax(sliderName, windowName, maxSlider);
  81. setTrackbarPos(sliderName, windowName, valDefault);
  82. }
  83. void TrackSlider(int , void *r)
  84. {
  85. OilImage *p = (OilImage *)r;
  86. Mat dst;
  87. p->img = p->img / p->dynRatio;
  88. p->img = p->img*p->dynRatio;
  89. xphoto::oilPainting(p->img, dst, p->size, p->dynRatio,colorSpace[p->colorSpace]);
  90. if (!dst.empty())
  91. {
  92. imshow(p->winName, dst);
  93. }
  94. }