1 #ifndef sdet_detector_h_
2 #define sdet_detector_h_
3 //:
4 // \file
5 // \brief non-display-based interface class
6 //
7 // An interface to Van-Duc's Canny code. No display is involved.
8 // The Canny has two major phases: 1) detect step edgels, 2)Follow contours
9 // and construct a topological network. Each phase has a number of
10 //  parameters which are defined as follows.
11 //  Step Detection:
12 //
13 // - float contourFactor, junctionFactor: Scale factors for determining the
14 //                                        gradient threshold. Nominally 1.0.
15 //                                        contourFactor is in effect for edgels
16 //                                        on contours (boundaries).
17 //                                        junctionFactor is in effect during the
18 //                                        extension of contours at endpoints.
19 //                                        To extend contours aggressively, use a
20 //                                        low value of junctionFactor, i.e., .5.
21 //
22 // - float noiseThreshold: A weighting factor that determines the relative
23 //                         proportion of sensor noise level and texture noise level
24 //                         as measured in a ROI in the center of the image. The
25 //                         nominal value of -.5 gives equal weight to both. If the
26 //                         value is positive, then a default noise threshold of 1.0
27 //                         is assigned.
28 //
29 // - float filterFactor:  An overall scale factor for determining gradient threshold.
30 //                        Nominally 2.0.
31 //
32 // - bool  junctionp:  If true, then recover junctions by extending contours.
33 //                     Nominally true.
34 //
35 // - Contour Following:
36 // - float hysteresisFactor:     A scale factor which is multiplied by the
37 //                               image noise level to determine the minimum
38 //                               gradient threshold in following an edgel contour.
39 //                               Nominally 2.0.
40 //
41 // - int minLength:              The minimum length contour to constructed.
42 //
43 // - float minJump:              A scale factor which is multiplied by the
44 //                               image noise level to determine the gradient
45 //                               threshold at a junction. Nominally 1.0.
46 //
47 // - float maxGap:               The width of a gap which can be crossed in
48 //                               forming a junction with another edgel contour.
49 //                               Nominally sqrt(5) = 2.24.
50 //
51 // - bool spacingp:              If true, then equalize the sub-pixel locations
52 //                               of each edgel by averaging the adjacent left
53 //                               a right neighbor locations. Nominally true.
54 //
55 // - bool borderp:               If true, insert virtual contours at the border
56 //                               to close regions. Nominally false.
57 //
58 // \author
59 //             Jane S. Liu - 3/27/95
60 //             GE Corporate Research and Development
61 //
62 // \verbatim
63 //  Modifications
64 //   JLM - May 1997
65 //             Added extra interface for parameters not initially
66 //             provided by Jane.These parameters are needed to get
67 //             satisfactory boundary closure. Also expanded comments.
68 //   JLM - November 1997
69 //             Moved most parameters up to sdet_detectorParams in
70 //             order to unify the use of parameters.
71 // \endverbatim
72 //-----------------------------------------------------------------------------
73 
74 class gevd_bufferxy;
75 
76 #include <vector>
77 #include <iostream>
78 #ifdef _MSC_VER
79 #  include <vcl_msvc_warnings.h>
80 #endif
81 #include <vil1/vil1_image.h>
82 #include <vil/vil_image_resource.h>
83 #include <vtol/vtol_vertex_2d_sptr.h>
84 #include <vtol/vtol_edge_2d_sptr.h>
85 #include <vsol/vsol_digital_curve_2d.h>
86 #include <vdgl/vdgl_digital_curve_sptr.h>
87 #include <brip/brip_roi.h>
88 #include <sdet/sdet_detector_params.h>
89 
90 class sdet_detector : public sdet_detector_params
91 {
92  public:
93   // So far, not all parameters are included in the constructor.  These seem to
94   // be the most important in controlling performance - JLM
95   //
96   sdet_detector(sdet_detector_params& params);
97 
98   sdet_detector(const vil1_image&, float smoothSigma = 1.0, float noiseSigma =2.0,
99                 float contourFactor = 1.0, float junctionFactor = 1.5,
100                 int minLength = 6, float maxGap = 2.23606, float minJump=1.0);
101 
102   sdet_detector(vil_image_resource_sptr &, float smoothSigma = 1.0,
103                 float noiseSigma =2.0, float contourFactor = 1.0,
104                 float junctionFactor = 1.5, int minLength = 6,
105                 float maxGap = 2.23606, float minJump=1.0);
106 
107   ~sdet_detector() override;
108 
109   // External interfaces
110   //Step contour detection
111   bool DoContour();
112 
113   //Fold contour detection
114   void DoFoldContourDetector(vil1_image image,
115                              std::vector<vtol_edge_2d_sptr >& edgels);
116 
117   void DoFoldContourDetector(vil_image_resource_sptr const& image,
118                              std::vector<vtol_edge_2d_sptr >& edgels);
119 
120 
121   //Corner detection using curvature on edgel chains
122   //GEOFF  void  DoCornerDetector(vil1_image image, IUPointGroup& corners);
123 
124   //Corner detection using curvature on edgel chains
125   void  DoBreakCorners(std::vector<vtol_edge_2d_sptr >& in_edgels, std::vector<vtol_edge_2d_sptr >& out_edgels);
126 
127   // internal interfaces
128   bool DoFoldContour();
129   bool DoCorner( float angle = 10,      //!< smallest angle at corner
130                  float separation = 1,  //!< |mean1-mean2|/sigma
131                  int length = 5,        //!< min length to find cornersxo
132                  int cycle = 2,         //!< number of corners in a cycle
133                  int ndimension = 2);   //!< number of dimension
134   bool DoStep();
135   bool DoFold();
136 
137   gevd_bufferxy* GetBufferFromImage(); //!< vil1 image conversion
138   gevd_bufferxy* GetBufferFromVilImage();//!< vil image conversion
139 
GetVertices()140   std::vector<vtol_vertex_2d_sptr> *GetVertices() {return vertices;}
GetEdges()141   std::vector<vtol_edge_2d_sptr> *GetEdges() {return edges;}
142 
143   bool get_vdgl_edges(std::vector<vdgl_digital_curve_sptr>& edges );
144 
145   bool get_vsol_edges(std::vector<vsol_digital_curve_2d_sptr>& edges );
146   //:The last type set is used in the execution if both types are valid
147   void SetImage(const vil1_image& img);
148   void SetImage(vil_image_resource_sptr const& img);
149   void SetImage(vil_image_resource_sptr const& img, brip_roi const& roi);
150 
151   void print(std::ostream &strm=std::cout) const;
152   static std::vector<vsol_digital_curve_2d_sptr> convert_vdgl_to_vsol(std::vector<vdgl_digital_curve_sptr> const& vd_edges);
153  protected:
154   void ClearData(); //!< clear buffer
155 
156  protected:
157   bool use_vil_image;//there could be both types set on class
158   bool use_roi_;
159   vil1_image image;
160   vil_image_resource_sptr vimage;
161   brip_roi roi_; //possible roi
162   float noise; //!< noise estimation/threshold
163 
164   gevd_bufferxy *edgel,                      //!< output from DoStep
165     *direction, *locationx, *locationy, *grad_mag, *angle; //!< detect step/fold
166   int *junctionx, *junctiony, njunction; //!< junctions found
167 
168   std::vector<vtol_vertex_2d_sptr >* vertices;//!< network of linked
169   std::vector<vtol_edge_2d_sptr >* edges; //!< edges and vertices
170 
171   float filterFactor;     //!< factor in convolution filter
172   float hysteresisFactor; //!< hysteresis factor
173   float noiseThreshold;
174 };
175 
176 #endif // sdet_detector_h_
177