cropper.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // cropper.h
  3. // SlamView
  4. //
  5. // Created by Mosong Cheng on 4/30/14.
  6. // Copyright (c) 2014 Mosong Cheng. All rights reserved.
  7. //
  8. #pragma once
  9. #include <iostream>
  10. #include "opencv2/highgui/highgui.hpp"
  11. #include "opencv2/imgproc/imgproc.hpp"
  12. #include "opencv2/imgcodecs.hpp"
  13. using namespace std;
  14. using namespace cv;
  15. /** Usage: suppose you want to crop an image img, which is
  16. * CV8UC3 mat, BGR color format. Call this class:
  17. *
  18. * Cropper cropper;
  19. * Mat cropped_image = cropper.crop(img);
  20. *
  21. * Or like this:
  22. * Cropper cropper;
  23. * Mat cropped_image;
  24. * cv::Rect bound;
  25. * cropper.crop(img, cropped_image, bound);
  26. *
  27. * Now #bound stores the bounding rectangle of the #cropped_image
  28. * inside the original #img.
  29. * **********************************************************/
  30. class Cropper{
  31. public:
  32. int kstep = 10;
  33. public:
  34. /** cropping the image to rid the blank part **/
  35. Mat crop(const Mat& img);
  36. void crop(const Mat& src, Mat& dst, cv::Rect& dstBound);
  37. inline void setStep(int kp){
  38. kstep = kp;
  39. }
  40. void setROI(const cv::Rect& roi){
  41. finalROI = roi;
  42. // roi = [4165 x 2207 from (465, 253)]
  43. }
  44. ~Cropper(){
  45. mask.release();
  46. }
  47. private:
  48. Mat mask;
  49. cv::Rect rect;
  50. cv::Rect finalROI; // the final cropping from image
  51. private:
  52. void getMask(const Mat& img);
  53. void expandROI(cv::Rect& r);
  54. void framing();
  55. bool lineHasBlankPixels(const Point2i& st, const Point2i& ed);
  56. bool rectHasBlankPixels(const cv::Rect& roi);
  57. bool lineHasContent(const Point2i& st, const Point2i& ed);
  58. void searchContentLine(Point2i& p1, Point2i& p2, Point2i& delta);
  59. };