ED.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**************************************************************************************************************
  2. * Edge Drawing (ED) and Edge Drawing Parameter Free (EDPF) source codes.
  3. * Copyright (C) Cihan Topal & Cuneyt Akinlar
  4. * E-mails of the authors: cihantopal@gmail.com, cuneytakinlar@gmail.com
  5. *
  6. * Please cite the following papers if you use Edge Drawing library:
  7. *
  8. * [1] C. Topal and C. Akinlar, ¡°Edge Drawing: A Combined Real-Time Edge and Segment Detector,¡±
  9. * Journal of Visual Communication and Image Representation, 23(6), 862-872, DOI: 10.1016/j.jvcir.2012.05.004 (2012).
  10. *
  11. * [2] C. Akinlar and C. Topal, ¡°EDPF: A Real-time Parameter-free Edge Segment Detector with a False Detection Control,¡±
  12. * International Journal of Pattern Recognition and Artificial Intelligence, 26(1), DOI: 10.1142/S0218001412550026 (2012).
  13. **************************************************************************************************************/
  14. #ifndef _ED_
  15. #define _ED_
  16. #include <opencv2/opencv.hpp>
  17. /// Special defines
  18. #define EDGE_VERTICAL 1
  19. #define EDGE_HORIZONTAL 2
  20. #define ANCHOR_PIXEL 254
  21. #define EDGE_PIXEL 255
  22. #define ED_LEFT 1
  23. #define ED_RIGHT 2
  24. #define ED_UP 3
  25. #define ED_DOWN 4
  26. enum GradientOperator { PREWITT_OPERATOR = 101, SOBEL_OPERATOR = 102, SCHARR_OPERATOR = 103, LSD_OPERATOR = 104 };
  27. struct StackNode {
  28. int r, c; // starting pixel
  29. int parent; // parent chain (-1 if no parent)
  30. int dir; // direction where you are supposed to go
  31. };
  32. // Used during Edge Linking
  33. struct Chain {
  34. int dir; // Direction of the chain
  35. int len; // # of pixels in the chain
  36. int parent; // Parent of this node (-1 if no parent)
  37. int children[2]; // Children of this node (-1 if no children)
  38. cv::Point* pixels; // Pointer to the beginning of the pixels array
  39. };
  40. class ED {
  41. public:
  42. ED(cv::Mat _srcImage, GradientOperator _op = PREWITT_OPERATOR, int _gradThresh = 20, int _anchorThresh = 0, int _scanInterval = 1, int _minPathLen = 10, double _sigma = 1.0, bool _sumFlag = true);
  43. ED(const ED& cpyObj);
  44. ED(short* gradImg, uchar* dirImg, int _width, int _height, int _gradThresh, int _anchorThresh, int _scanInterval = 1, int _minPathLen = 10, bool selectStableAnchors = true);
  45. ED();
  46. cv::Mat getEdgeImage();
  47. cv::Mat getAnchorImage();
  48. cv::Mat getSmoothImage();
  49. cv::Mat getGradImage();
  50. int getSegmentNo();
  51. int getAnchorNo();
  52. std::vector<cv::Point> getAnchorPoints();
  53. std::vector<std::vector<cv::Point>> getSegments();
  54. std::vector<std::vector<cv::Point>> getSortedSegments();
  55. cv::Mat drawParticularSegments(std::vector<int> list);
  56. protected:
  57. int width; // width of source image
  58. int height; // height of source image
  59. uchar* srcImg;
  60. std::vector<std::vector< cv::Point> > segmentPoints;
  61. double sigma; // Gaussian sigma
  62. cv::Mat smoothImage;
  63. uchar* edgeImg; // pointer to edge image data
  64. uchar* smoothImg; // pointer to smoothed image data
  65. int segmentNos;
  66. int minPathLen;
  67. cv::Mat srcImage;
  68. private:
  69. void ComputeGradient();
  70. void ComputeAnchorPoints();
  71. void JoinAnchorPointsUsingSortedAnchors();
  72. void sortAnchorsByGradValue();
  73. int* sortAnchorsByGradValue1();
  74. static int LongestChain(Chain* chains, int root);
  75. static int RetrieveChainNos(Chain* chains, int root, int chainNos[]);
  76. int anchorNos;
  77. std::vector<cv::Point> anchorPoints;
  78. std::vector<cv::Point> edgePoints;
  79. cv::Mat edgeImage;
  80. cv::Mat gradImage;
  81. uchar* dirImg; // pointer to direction image data
  82. short* gradImg; // pointer to gradient image data
  83. GradientOperator op; // operation used in gradient calculation
  84. int gradThresh; // gradient threshold
  85. int anchorThresh; // anchor point threshold
  86. int scanInterval;
  87. bool sumFlag;
  88. };
  89. #endif