mexopencv  3.4.1
MEX interface for OpenCV library
findCirclesGrid.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "mexopencv_features2d.hpp" // createFeatureDetector
10 #include "opencv2/calib3d.hpp"
11 using namespace std;
12 using namespace cv;
13 
14 namespace {
20 
29 {
31  if (arr.isStruct()) {
32  params.gridType = GridTypesMap[arr.at("gridType").toString()];
33  if (arr.isField("densityNeighborhoodSize"))
34  params.densityNeighborhoodSize = arr.at("densityNeighborhoodSize").toSize_<float>();
35  if (arr.isField("minDensity"))
36  params.minDensity = arr.at("minDensity").toFloat();
37  if (arr.isField("kmeansAttempts"))
38  params.kmeansAttempts = arr.at("kmeansAttempts").toInt();
39  if (arr.isField("minDistanceToAddKeypoint"))
40  params.minDistanceToAddKeypoint = arr.at("minDistanceToAddKeypoint").toInt();
41  if (arr.isField("keypointScale"))
42  params.keypointScale = arr.at("keypointScale").toInt();
43  if (arr.isField("minGraphConfidence"))
44  params.minGraphConfidence = arr.at("minGraphConfidence").toFloat();
45  if (arr.isField("vertexGain"))
46  params.vertexGain = arr.at("vertexGain").toFloat();
47  if (arr.isField("vertexPenalty"))
48  params.vertexPenalty = arr.at("vertexPenalty").toFloat();
49  if (arr.isField("existingVertexGain"))
50  params.existingVertexGain = arr.at("existingVertexGain").toFloat();
51  if (arr.isField("edgeGain"))
52  params.edgeGain = arr.at("edgeGain").toFloat();
53  if (arr.isField("edgePenalty"))
54  params.edgePenalty = arr.at("edgePenalty").toFloat();
55  if (arr.isField("convexHullFactor"))
56  params.convexHullFactor = arr.at("convexHullFactor").toFloat();
57  if (arr.isField("minRNGEdgeSwitchDist"))
58  params.minRNGEdgeSwitchDist = arr.at("minRNGEdgeSwitchDist").toFloat();
59  if (arr.isField("squareSize"))
60  params.squareSize = arr.at("squareSize").toFloat();
61  if (arr.isField("maxRectifiedDistance"))
62  params.maxRectifiedDistance = arr.at("maxRectifiedDistance").toFloat();
63  }
64  else {
65  vector<MxArray> args(arr.toVector<MxArray>());
66  nargchk(args.size() >= 1 && (args.size()%2)==1);
67  params.gridType = GridTypesMap[args[0].toString()];
68  for (size_t i = 1; i < args.size(); i+=2) {
69  string key(args[i].toString());
70  if (key == "DensityNeighborhoodSize")
71  params.densityNeighborhoodSize = args[i+1].toSize_<float>();
72  else if (key == "MinDensity")
73  params.minDensity = args[i+1].toFloat();
74  else if (key == "KmeansAttempts")
75  params.kmeansAttempts = args[i+1].toInt();
76  else if (key == "MinDistanceToAddKeypoint")
77  params.minDistanceToAddKeypoint = args[i+1].toInt();
78  else if (key == "KeypointScale")
79  params.keypointScale = args[i+1].toInt();
80  else if (key == "MinGraphConfidence")
81  params.minGraphConfidence = args[i+1].toFloat();
82  else if (key == "VertexGain")
83  params.vertexGain = args[i+1].toFloat();
84  else if (key == "VertexPenalty")
85  params.vertexPenalty = args[i+1].toFloat();
86  else if (key == "ExistingVertexGain")
87  params.existingVertexGain = args[i+1].toFloat();
88  else if (key == "EdgeGain")
89  params.edgeGain = args[i+1].toFloat();
90  else if (key == "EdgePenalty")
91  params.edgePenalty = args[i+1].toFloat();
92  else if (key == "ConvexHullFactor")
93  params.convexHullFactor = args[i+1].toFloat();
94  else if (key == "MinRNGEdgeSwitchDist")
95  params.minRNGEdgeSwitchDist = args[i+1].toFloat();
96  else if (key == "SquareSize")
97  params.squareSize = args[i+1].toFloat();
98  else if (key == "MaxRectifiedDistance")
99  params.maxRectifiedDistance = args[i+1].toFloat();
100  else
101  mexErrMsgIdAndTxt("mexopencv:error",
102  "Unrecognized CirclesGridFinderParameters2 option %s", key.c_str());
103  }
104  }
105  return params;
106 }
107 }
108 
116 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
117 {
118  // Check the number of arguments
119  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=2);
120 
121  // Argument vector
122  vector<MxArray> rhs(prhs, prhs+nrhs);
123 
124  // Option processing
125  bool symmetricGrid = true;
126  bool clustering = false;
127  Ptr<FeatureDetector> blobDetector;
129  for (int i=2; i<nrhs; i+=2) {
130  string key(rhs[i].toString());
131  if (key == "SymmetricGrid")
132  symmetricGrid = rhs[i+1].toBool();
133  else if (key == "Clustering")
134  clustering = rhs[i+1].toBool();
135  else if (key == "BlobDetector") {
136  if (rhs[i+1].isChar())
137  blobDetector = createFeatureDetector(rhs[i+1].toString(),
138  rhs.end(), rhs.end());
139  else if (rhs[i+1].isCell() && rhs[i+1].numel() >= 2) {
140  vector<MxArray> args(rhs[i+1].toVector<MxArray>());
141  blobDetector = createFeatureDetector(args[0].toString(),
142  args.begin() + 1, args.end());
143  }
144  else
145  mexErrMsgIdAndTxt("mexopencv:error",
146  "Invalid detector arguments");
147  }
148  else if (key == "FinderParameters")
149  params = MxArrayToFinderParameters(rhs[i+1]);
150  else
151  mexErrMsgIdAndTxt("mexopencv:error",
152  "Unrecognized option %s", key.c_str());
153  }
154  int flags = (symmetricGrid ? cv::CALIB_CB_SYMMETRIC_GRID :
156  (clustering ? cv::CALIB_CB_CLUSTERING : 0);
157  if (blobDetector.empty())
158  blobDetector = SimpleBlobDetector::create();
159 
160  // Process
161  Mat image(rhs[0].toMat(CV_8U));
162  Size patternSize(rhs[1].toSize());
163  vector<Point2f> centers;
164  bool patternFound = findCirclesGrid2(image, patternSize, centers, flags,
165  blobDetector, params);
166  plhs[0] = MxArray(centers);
167  if (nlhs > 1)
168  plhs[1] = MxArray(patternFound);
169 }
CALIB_CB_ASYMMETRIC_GRID
const ConstMap< string, cv::CirclesGridFinderParameters::GridType > GridTypesMap
grid types for option processing
T at(mwIndex index) const
Template for numeric array element accessor.
Definition: MxArray.hpp:1250
#define CV_8U
STL namespace.
T end(T... args)
CirclesGridFinderParameters2 MxArrayToFinderParameters(const MxArray &arr)
Convert MxArray to cv::CirclesGridFinderParameters2.
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
CALIB_CB_CLUSTERING
CALIB_CB_SYMMETRIC_GRID
cv::Ptr< cv::FeatureDetector > createFeatureDetector(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Factory function for FeatureDetector creation.
LIBMWMEX_API_EXTERN_C void mexErrMsgIdAndTxt(const char *identifier, const char *err_msg,...)
Issue formatted error message with corresponding error identifier and return to MATLAB prompt...
bool isField(const std::string &fieldName) const
Determine whether a struct array has a specified field.
Definition: MxArray.hpp:743
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/output arguments number check.
Definition: mexopencv.hpp:181
Common definitions for the features2d and xfeatures2d modules.
bool findCirclesGrid2(InputArray image, Size patternSize, OutputArray centers, int flags, const Ptr< FeatureDetector > &blobDetector, CirclesGridFinderParameters2 parameters)
bool isStruct() const
Determine whether input is structure array.
Definition: MxArray.hpp:708
T size(T... args)
std::vector< T > toVector() const
Convert MxArray to std::vector<T> of primitive types.
Definition: MxArray.hpp:1151
STL class.
bool empty() const
Global constant definitions.
T begin(T... args)
T c_str(T... args)
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
cv::Mat toMat() const