Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
vpCannyEdgeDetection.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2023 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 _vpCannyEdgeDetection_h_
32#define _vpCannyEdgeDetection_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>
46using json = nlohmann::json;
47#endif
48
49class VISP_EXPORT vpCannyEdgeDetection
50{
51private:
52 typedef enum EdgeType
53 {
54 STRONG_EDGE,
55 WEAK_EDGE,
56 ON_CHECK
57 } EdgeType;
58
59 // // Gaussian smoothing attributes
60 int m_gaussianKernelSize;
61 float m_gaussianStdev;
63 // // Gradient computation attributes
64 bool m_areGradientAvailable;
65 vpArray2D<float> m_fg;
66 vpArray2D<float> m_fgDg;
67 vpImage<float> m_dIx;
68 vpImage<float> m_dIy;
70 // // Edge thining attributes
71 std::map<std::pair<unsigned int, unsigned int>, float> m_edgeCandidateAndGradient;
73 // // Hysteresis thresholding attributes
74 float m_lowerThreshold;
75 float m_upperThreshold;
77 // // Edge tracking attributes
78 std::map<std::pair<unsigned int, unsigned int>, EdgeType> m_edgePointsCandidates;
80 vpImage<unsigned char> m_edgeMap;
88 void initGaussianFilters();
90
98 void performFilteringAndGradientComputation(const vpImage<unsigned char> &I);
99
105 void performEdgeThining();
106
120 void performHysteresisThresholding(const float &lowerThreshold, const float &upperThreshold);
121
129 bool recursiveSearchForStrongEdge(const std::pair<unsigned int, unsigned int> &coordinates);
130
137 void performEdgeTracking();
139
140public:
148
157 vpCannyEdgeDetection(const int &gaussianKernelSize, const float &gaussianStdev,
158 const float &lowerThreshold = -1., const float &upperThreshold = -1.);
159
160 // // Configuration from files
161#ifdef VISP_HAVE_NLOHMANN_JSON
167 vpCannyEdgeDetection(const std::string &jsonPath);
168
176 void initFromJSON(const std::string &jsonPath);
177
185 inline friend void from_json(const json &j, vpCannyEdgeDetection &detector)
186 {
187 detector.m_gaussianKernelSize = j.value("gaussianSize", detector.m_gaussianKernelSize);
188 detector.m_gaussianStdev = j.value("gaussianStdev", detector.m_gaussianStdev);
189 detector.m_lowerThreshold = j.value("lowerThreshold", detector.m_lowerThreshold);
190 detector.m_upperThreshold = j.value("upperThreshold", detector.m_upperThreshold);
191 }
192
199 inline friend void to_json(json &j, const vpCannyEdgeDetection &detector)
200 {
201 j = json {
202 {"gaussianSize", detector.m_gaussianKernelSize},
203 {"gaussianStdev", detector.m_gaussianStdev},
204 {"lowerThreshold", detector.m_lowerThreshold},
205 {"upperThreshold", detector.m_upperThreshold} };
206 }
207#endif
209
212#ifdef HAVE_OPENCV_CORE
220 vpImage<unsigned char> detect(const cv::Mat &cv_I);
221#endif
222
230 vpImage<unsigned char> detect(const vpImage<vpRGBa> &I_color);
231
240
249 inline void setGradients(const vpImage<float> &dIx, const vpImage<float> &dIy)
250 {
251 m_dIx = dIx;
252 m_dIy = dIy;
253 m_areGradientAvailable = true;
254 }
255
267 inline void setCannyThresholds(const float &lowerThresh, const float &upperThresh)
268 {
269 m_lowerThreshold = lowerThresh;
270 m_upperThreshold = upperThresh;
271 }
272
281 inline void setGaussianFilterParameters(const int &kernelSize, const float &stdev)
282 {
283 m_gaussianKernelSize = kernelSize;
284 m_gaussianStdev = stdev;
285 initGaussianFilters();
286 }
288};
289#endif
Implementation of a generic 2D array used as base class for matrices and vectors.
Definition vpArray2D.h:131
json namespace shortcut
friend void to_json(json &j, const vpCannyEdgeDetection &detector)
Parse a vpCannyEdgeDetection object into JSON format.
friend void from_json(const json &j, vpCannyEdgeDetection &detector)
Read the detector configuration from JSON. All values are optional and if an argument is not present,...
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 setGaussianFilterParameters(const int &kernelSize, const float &stdev)
Set the Gaussian Filters kernel size and standard deviation and initialize the aforementioned filters...
Definition of the vpImage class member functions.
Definition vpImage.h:135