Visual Servoing Platform  version 3.6.1 under development (2024-07-27)
vpCannyEdgeDetection.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  */
30 
31 #ifndef VP_CANNY_EDGE_DETECTION_H
32 #define VP_CANNY_EDGE_DETECTION_H
33 
34 // System includes
35 #include <map>
36 #include <vector>
37 
38 // ViSP include
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpImage.h>
41 #include <visp3/core/vpImageFilter.h>
42 
43 // 3rd parties include
44 #ifdef VISP_HAVE_NLOHMANN_JSON
45 #include <nlohmann/json.hpp>
46 #endif
47 
54 class VISP_EXPORT vpCannyEdgeDetection
55 {
56 
57 public:
65 
85  vpCannyEdgeDetection(const int &gaussianKernelSize, const float &gaussianStdev, const unsigned int &sobelAperture,
86  const float &lowerThreshold = -1.f, const float &upperThreshold = -1.f,
87  const float &lowerThresholdRatio = 0.6f, const float &upperThresholdRatio = 0.8f,
89 
90  // // Configuration from files
91 #ifdef VISP_HAVE_NLOHMANN_JSON
97  vpCannyEdgeDetection(const std::string &jsonPath);
98 
106  void initFromJSON(const std::string &jsonPath);
107 
115  friend void from_json(const nlohmann::json &j, vpCannyEdgeDetection &detector);
116 
123  friend void to_json(nlohmann::json &j, const vpCannyEdgeDetection &detector);
124 #endif
126 
129 #ifdef HAVE_OPENCV_CORE
137  vpImage<unsigned char> detect(const cv::Mat &cv_I);
138 #endif
139 
147  vpImage<unsigned char> detect(const vpImage<vpRGBa> &I_color);
148 
157 
166  {
167  m_filteringAndGradientType = type;
168  initGradientFilters();
169  }
170 
177  inline void setGradients(const vpImage<float> &dIx, const vpImage<float> &dIy)
178  {
179  m_dIx = dIx;
180  m_dIy = dIy;
181  m_areGradientAvailable = true;
182  }
183 
195  inline void setCannyThresholds(const float &lowerThresh, const float &upperThresh)
196  {
197  m_lowerThreshold = lowerThresh;
198  m_upperThreshold = upperThresh;
199  }
200 
213  inline void setCannyThresholdsRatio(const float &lowerThreshRatio, const float &upperThreshRatio)
214  {
215  m_lowerThresholdRatio = lowerThreshRatio;
216  m_upperThresholdRatio = upperThreshRatio;
217  }
218 
227  inline void setGaussianFilterParameters(const int &kernelSize, const float &stdev)
228  {
229  m_gaussianKernelSize = kernelSize;
230  m_gaussianStdev = stdev;
231  initGaussianFilters();
232  }
233 
239  inline void setGradientFilterAperture(const unsigned int &apertureSize)
240  {
241  m_gradientFilterKernelSize = apertureSize;
242  initGradientFilters();
243  }
244 
255  inline void setMask(const vpImage<bool> *p_mask)
256  {
257  mp_mask = p_mask;
258  }
260 private:
261  typedef enum EdgeType
262  {
263  STRONG_EDGE,
264  WEAK_EDGE,
265  ON_CHECK
266  } EdgeType;
267 
268  // Filtering + gradient methods choice
269  vpImageFilter::vpCannyFilteringAndGradientType m_filteringAndGradientType;
272  // // Gaussian smoothing attributes
273  int m_gaussianKernelSize;
274  float m_gaussianStdev;
275  vpArray2D<float> m_fg;
277  // // Gradient computation attributes
278  bool m_areGradientAvailable;
279  unsigned int m_gradientFilterKernelSize;
280  vpArray2D<float> m_gradientFilterX;
281  vpArray2D<float> m_gradientFilterY;
282  vpImage<float> m_dIx;
283  vpImage<float> m_dIy;
285  // // Edge thining attributes
286  std::map<std::pair<unsigned int, unsigned int>, float> m_edgeCandidateAndGradient;
288  // // Hysteresis thresholding attributes
289  float m_lowerThreshold;
291  float m_lowerThresholdRatio;
293  float m_upperThreshold;
294  float m_upperThresholdRatio;
297  // // Edge tracking attributes
298  std::map<std::pair<unsigned int, unsigned int>, EdgeType> m_edgePointsCandidates;
300  vpImage<unsigned char> m_edgeMap;
301  const vpImage<bool> *mp_mask;
308  void initGaussianFilters();
309 
313  void initGradientFilters();
315 
323  void computeFilteringAndGradient(const vpImage<unsigned char> &I);
324 
332  void performEdgeThinning(const float &lowerThreshold);
333 
347  void performHysteresisThresholding(const float &lowerThreshold, const float &upperThreshold);
348 
356  bool recursiveSearchForStrongEdge(const std::pair<unsigned int, unsigned int> &coordinates);
357 
364  void performEdgeTracking();
366 };
367 END_VISP_NAMESPACE
368 #endif
Class that implements the Canny's edge detector. It is possible to use a boolean mask to ignore some ...
void setFilteringAndGradientType(const vpImageFilter::vpCannyFilteringAndGradientType &type)
Set the Filtering And Gradient operators to apply to the image before the edge detection operation.
void setMask(const vpImage< bool > *p_mask)
Set a mask to ignore pixels for which the mask is false.
void setCannyThresholdsRatio(const float &lowerThreshRatio, const float &upperThreshRatio)
Set the lower and upper Canny Thresholds ratio that are used to compute them automatically....
void setGradients(const vpImage< float > &dIx, const vpImage< float > &dIy)
Set the Gradients of the image that will be processed.
void setCannyThresholds(const float &lowerThresh, const float &upperThresh)
Set the lower and upper Canny Thresholds used to qualify the edge point candidates....
void setGradientFilterAperture(const unsigned int &apertureSize)
Set the parameters of the gradient filter (Sobel or Scharr) kernel size filters.
void setGaussianFilterParameters(const int &kernelSize, const float &stdev)
Set the Gaussian Filters kernel size and standard deviation and initialize the aforementioned filters...
vpCannyFilteringAndGradientType
Canny filter and gradient operators to apply on the image before the edge detection stage.
Definition: vpImageFilter.h:90
@ CANNY_GBLUR_SOBEL_FILTERING
Apply Gaussian blur + Sobel operator on the input image.
Definition: vpImageFilter.h:91