jack06215

[OpenCV] interactive crop

Jul 12th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.36 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <Windows.h>
  5. #include <opencv2/core.hpp>
  6. #include <opencv2/highgui.hpp>
  7. #include <opencv2/imgproc.hpp>
  8.  
  9.  
  10. cv::Mat image1, image2;
  11.  
  12. cv::Mat src, img, ROI;
  13. cv::Rect cropRect(0, 0, 0, 0);
  14. cv::Point P1(0, 0);
  15. cv::Point P2(0, 0);
  16.  
  17. const std::string winName = "Crop Image";
  18. bool clicked = false;
  19. int saveIndex = 0;
  20. char imgName[15];
  21.  
  22. void checkBoundary()
  23. {
  24.     //check croping rectangle exceed image boundary
  25.     if (cropRect.width > img.cols - cropRect.x)
  26.         cropRect.width = img.cols - cropRect.x;
  27.  
  28.     if (cropRect.height > img.rows - cropRect.y)
  29.         cropRect.height = img.rows - cropRect.y;
  30.  
  31.     if (cropRect.x < 0)
  32.         cropRect.x = 0;
  33.  
  34.     if (cropRect.y < 0)
  35.         cropRect.height = 0;
  36. }
  37.  
  38. void showImage()
  39. {
  40.     img = src.clone();
  41.     checkBoundary();
  42.     if (cropRect.width > 0 && cropRect.height > 0)
  43.     {
  44.         ROI = src(cropRect);
  45.         imshow("cropped", ROI);
  46.     }
  47.     cv::rectangle(img, cropRect, cv::Scalar(0, 255, 0), 1, 8, 0);
  48.     imshow(winName, img);
  49. }
  50.  
  51.  
  52. void onMouseCrop(int event, int x, int y, int f, void*)
  53. {
  54.     switch (event)
  55.     {
  56.     case  cv::EVENT_LBUTTONDOWN:
  57.         clicked = true;
  58.         P1.x = x;
  59.         P1.y = y;
  60.         P2.x = x;
  61.         P2.y = y;
  62.         break;
  63.     case  cv::EVENT_LBUTTONUP:
  64.         P2.x = x;
  65.         P2.y = y;
  66.         clicked = false;
  67.         break;
  68.     case  cv::EVENT_MOUSEMOVE:
  69.         if (clicked)
  70.         {
  71.             P2.x = x;
  72.             P2.y = y;
  73.         }
  74.         break;
  75.     default:
  76.         break;
  77.     }
  78.     if (clicked)
  79.     {
  80.         if (P1.x > P2.x)
  81.         {
  82.             cropRect.x = P2.x;
  83.             cropRect.width = P1.x - P2.x;
  84.         }
  85.         else
  86.         {
  87.             cropRect.x = P1.x;
  88.             cropRect.width = P2.x - P1.x;
  89.         }
  90.  
  91.         if (P1.y > P2.y)
  92.         {
  93.             cropRect.y = P2.y;
  94.             cropRect.height = P1.y - P2.y;
  95.         }
  96.         else
  97.         {
  98.             cropRect.y = P1.y;
  99.             cropRect.height = P2.y - P1.y;
  100.         }
  101.     }
  102.     showImage();
  103. }
  104.  
  105. void displayRoiMessage()
  106. {
  107.     std::cout << "Click and drag for Selection" << std::endl << std::endl;
  108.     std::cout << "------> Press 's' to save" << std::endl << std::endl;
  109.  
  110.     std::cout << "------> Press '8' to move up" << std::endl;
  111.     std::cout << "------> Press '2' to move down" << std::endl;
  112.     std::cout << "------> Press '6' to move right" << std::endl;
  113.     std::cout << "------> Press '4' to move left" << std::endl << std::endl;
  114.  
  115.     std::cout << "------> Press 'w' increas top" << std::endl;
  116.     std::cout << "------> Press 'x' increas bottom" << std::endl;
  117.     std::cout << "------> Press 'd' increas right" << std::endl;
  118.     std::cout << "------> Press 'a' increas left" << std::endl << std::endl;
  119.  
  120.     std::cout << "------> Press 't' decrease top" << std::endl;
  121.     std::cout << "------> Press 'b' decrease bottom" << std::endl;
  122.     std::cout << "------> Press 'h' decrease right" << std::endl;
  123.     std::cout << "------> Press 'f' decrease left" << std::endl << std::endl;
  124.  
  125.     std::cout << "------> Press 'r' to reset" << std::endl;
  126.     std::cout << "------> Press 'Esc' to quit" << std::endl << std::endl;
  127. }
  128.  
  129. void performOpcode(char c)
  130. {
  131.     if (c == 's' && ROI.data)
  132.     {
  133.         std::sprintf(imgName, "%d.jpg", saveIndex++);
  134.         cv::imwrite(imgName, ROI);
  135.         std::cout << "  Saved " << imgName << std::endl;
  136.     }
  137.     if (c == '6') cropRect.x++;
  138.     if (c == '4') cropRect.x--;
  139.     if (c == '8') cropRect.y--;
  140.     if (c == '2') cropRect.y++;
  141.  
  142.     if (c == 'w')
  143.     {
  144.         cropRect.y--;
  145.         cropRect.height++;
  146.     }
  147.     if (c == 'd') cropRect.width++;
  148.     if (c == 'x') cropRect.height++;
  149.     if (c == 'a')
  150.     {
  151.         cropRect.x--;
  152.         cropRect.width++;
  153.     }
  154.  
  155.     if (c == 't')
  156.     {
  157.         cropRect.y++;
  158.         cropRect.height--;
  159.     }
  160.     if (c == 'h') cropRect.width--;
  161.     if (c == 'b') cropRect.height--;
  162.     if (c == 'f')
  163.     {
  164.         cropRect.x++;
  165.         cropRect.width--;
  166.     }
  167.     if (c == 'r')
  168.     {
  169.         cropRect.x = 0;
  170.         cropRect.y = 0;
  171.         cropRect.width = 0;
  172.         cropRect.height = 0;
  173.     }
  174.  
  175.     if (c == 27)
  176.     {
  177.         std::cout << "Exit program" << std::endl << std::endl;
  178.         std::exit(EXIT_FAILURE);
  179.     }
  180. }
  181.  
  182. int main(int argc, char* argv[])
  183. {
  184.     cv::VideoCapture cap;
  185.     cap.open(1);
  186.     if (!cap.isOpened())
  187.     {
  188.         std::cerr << "cannot open camera for capture" << std::endl;
  189.         std::exit(EXIT_FAILURE);
  190.     }
  191.     cv::waitKey(1000);
  192.     // Let user crop a ROI from the input image
  193.     displayRoiMessage();
  194.     cv::namedWindow(winName, cv::WINDOW_NORMAL);
  195.     cv::setMouseCallback(winName, onMouseCrop, nullptr);
  196.     //imshow(winName, src);
  197.     char c;
  198.     do
  199.     {
  200.         cap >> src;
  201.         showImage();
  202.         c = cv::waitKey();
  203.         performOpcode(c);
  204.  
  205.     } while (c != VK_RETURN);
  206.  
  207.     // Extract feature
  208.     std::cout << "into feature detection" << std::endl;
  209.  
  210.     return 0;
  211. }
Add Comment
Please, Sign In to add comment