You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
5.2 KiB

4 years ago
  1. #include <iostream>
  2. #include <opencv2/core/core.hpp>
  3. #include <opencv2/tracking.hpp>
  4. #include <opencv2/highgui/highgui.hpp>
  5. #include <opencv2/imgproc/imgproc.hpp>
  6. #include <X11/Xlib.h>
  7. using namespace cv;
  8. Rect2d getSelection(Mat img, char* title, bool fromCenter, bool showCrosshair){
  9. Rect2d r = selectROI(title, img, fromCenter, showCrosshair);
  10. if(r.empty()){
  11. std::cout << "Cancelled selection, exiting.\n";
  12. exit(0);
  13. }
  14. return r;
  15. }
  16. Mat getImgFromFile(char* impath){
  17. Mat img = imread(impath);
  18. if(img.empty())
  19. {
  20. std::cerr<< "Could not read image from: " << impath << std::endl;
  21. exit(1);
  22. }
  23. return img;
  24. }
  25. Mat getImgFromClipboard(){
  26. // TODO implement a C++ implementation instead
  27. system("xclip -o -selection clipboard > /tmp/from.png");
  28. Mat img = imread("/tmp/from.png");
  29. if(img.empty())
  30. {
  31. std::cerr<< "Could not read image from clipboard. "<< std::endl;
  32. exit(1);
  33. }
  34. return img;
  35. }
  36. void saveImgToClipboard(Mat img){
  37. // TODO implement a C++ implementation instead
  38. imwrite("/tmp/to.png", img);
  39. system("xclip -selection clipboard -target image/png -i /tmp/to.png");
  40. }
  41. void exitHelp(){
  42. const char* HELP_TEXT = "Usage:\n"
  43. " simcrop [OPTION?] -f(c) [PATH?] -s(c) [PATH?]\n\n"
  44. "Help Options:\n"
  45. " -h, --help Show help options\n\n"
  46. "Application Options:\n"
  47. " -c, --center Select from center\n"
  48. " -x, --crosshair Show crosshair\n\n"
  49. "From & To:\n"
  50. " -f(c) [PATH] Image to crop, adding c fetches the image from clipboard\n"
  51. " -s(c) [PATH] Path to save cropped, adding c saves the image to clipboard\n\n"
  52. "Keybinds:\n"
  53. " * In selection, press enter or space to confirm.\n"
  54. " * To cancel, press c in selection window.\n"
  55. " * In confirmation, press q to exit and s to save.\n";
  56. std::cout << HELP_TEXT << "\n";
  57. exit(0);
  58. }
  59. int main(int argc, char** argv)
  60. {
  61. bool showCrosshair = false;
  62. bool selectFromCenter = false;
  63. Mat fromImg = Mat();
  64. bool saveClipboard = false;
  65. char* savePath = "";
  66. char* title = "SimCrop";
  67. if(argc < 3){
  68. std::cout << "Not enough parameters!\n";
  69. exitHelp();
  70. }
  71. for (int i = 0; i < argc; ++i){
  72. if(argv[i][0] != '-'){
  73. continue;
  74. }
  75. if(!strcmp(argv[i], "-h")){
  76. exitHelp();
  77. }else if(!(strcmp(argv[i], "-c") && strcmp(argv[i], "--center"))){
  78. selectFromCenter = true;
  79. }else if(!(strcmp(argv[i], "-x") && strcmp(argv[i], "--crosshair"))){
  80. showCrosshair = true;
  81. }else if(!(strcmp(argv[i], "-t") && strcmp(argv[i], "--title"))){
  82. if(argc <= i+1){
  83. std::cerr << "You must provide a string with -t!\n";
  84. exitHelp();
  85. }
  86. title = argv[i+1];
  87. }else if(!strcmp(argv[i], "-f")){
  88. if(!fromImg.empty()){
  89. std::cerr << "You can only provide one from argument!\n";
  90. exitHelp();
  91. }
  92. if(argc <= i+1){
  93. std::cerr << "You must provide a path with -f!\n";
  94. exitHelp();
  95. }
  96. fromImg = getImgFromFile(argv[i+1]);
  97. }else if(!strcmp(argv[i], "-s")){
  98. if(strcmp(savePath, "") || saveClipboard){
  99. std::cerr << "You can only provide one save argument!\n";
  100. exitHelp();
  101. }
  102. if(argc <= i+1){
  103. std::cerr << "You must provide a path with -s!\n";
  104. exitHelp();
  105. }
  106. savePath = argv[i+1];
  107. }else if(!strcmp(argv[i], "-fc")){
  108. if(!fromImg.empty()){
  109. std::cerr << "You can only provide one from arguement!\n";
  110. exitHelp();
  111. }
  112. fromImg = getImgFromClipboard();
  113. }else if(!strcmp(argv[i], "-sc")){
  114. if(strcmp(savePath, "") || saveClipboard){
  115. std::cerr << "You can only provide one save argument!\n";
  116. exitHelp();
  117. }
  118. saveClipboard = true;
  119. }else{
  120. std::cout << "Unknown parameter " << argv[i] << "\n";
  121. exitHelp();
  122. }
  123. }
  124. printf("%d", fromImg.empty());
  125. if(!(saveClipboard || strcmp(savePath, "")) || fromImg.empty()){
  126. std::cerr << "From and To arguements are mandatory!\n";
  127. exitHelp();
  128. }
  129. uint8_t key = 98;
  130. Mat cropped = Mat();
  131. Rect2d selection = Rect2d();
  132. while(key == 98){
  133. selection = getSelection(fromImg, title, selectFromCenter, showCrosshair);
  134. cropped = fromImg(selection);
  135. imshow(title, cropped);
  136. do{
  137. key = waitKey(1);
  138. }while(key != 113 && key != 115 && key != 98);
  139. }
  140. if(key == 113){
  141. return 0;
  142. }
  143. if(saveClipboard){
  144. saveImgToClipboard(cropped);
  145. }else{
  146. imwrite(savePath, cropped);
  147. }
  148. return 0;
  149. }