11 #include "opencv2/xfeatures2d.hpp" 21 map<int,Ptr<LUCID> > obj_;
31 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
37 vector<MxArray> rhs(prhs, prhs+nrhs);
38 int id = rhs[0].toInt();
39 string method(rhs[1].toString());
42 if (method ==
"new") {
44 obj_[++last_id] = createLUCID(rhs.begin() + 2, rhs.end());
50 Ptr<LUCID> obj = obj_[id];
51 if (method ==
"delete") {
55 else if (method ==
"typeid") {
57 plhs[0] =
MxArray(
string(
typeid(*obj).name()));
59 else if (method ==
"clear") {
63 else if (method ==
"load") {
64 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
66 bool loadFromString =
false;
67 for (
int i=3; i<nrhs; i+=2) {
68 string key(rhs[i].toString());
70 objname = rhs[i+1].toString();
71 else if (key ==
"FromString")
72 loadFromString = rhs[i+1].toBool();
74 mexErrMsgIdAndTxt(
"mexopencv:error",
75 "Unrecognized option %s", key.c_str());
84 FileStorage fs(rhs[2].toString(), FileStorage::READ +
85 (loadFromString ? FileStorage::MEMORY : 0));
86 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
88 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
91 else if (method ==
"save") {
93 obj->save(rhs[2].toString());
95 else if (method ==
"empty") {
97 plhs[0] =
MxArray(obj->empty());
99 else if (method ==
"getDefaultName") {
101 plhs[0] =
MxArray(obj->getDefaultName());
103 else if (method ==
"defaultNorm") {
107 else if (method ==
"descriptorSize") {
109 plhs[0] =
MxArray(obj->descriptorSize());
111 else if (method ==
"descriptorType") {
115 else if (method ==
"compute") {
117 if (rhs[2].isNumeric()) {
118 Mat image(rhs[2].toMat(CV_8U)), descriptors;
119 if (image.channels() == 1)
121 cvtColor(image, image, cv::COLOR_GRAY2BGR);
122 vector<KeyPoint> keypoints(rhs[3].toVector<KeyPoint>());
123 obj->compute(image, keypoints, descriptors);
124 plhs[0] =
MxArray(descriptors);
128 else if (rhs[2].isCell()) {
130 vector<Mat> images, descriptors;
132 vector<MxArray> arr(rhs[2].toVector<MxArray>());
133 images.reserve(arr.size());
134 for (vector<MxArray>::const_iterator it = arr.begin(); it != arr.end(); ++it) {
135 Mat img(it->toMat(CV_8U));
136 if (img.channels() == 1)
137 cvtColor(img, img, cv::COLOR_GRAY2BGR);
138 images.push_back(img);
141 vector<vector<KeyPoint> > keypoints(rhs[3].toVector(
142 const_mem_fun_ref_t<vector<KeyPoint>,
MxArray>(
143 &MxArray::toVector<KeyPoint>)));
144 obj->compute(images, keypoints, descriptors);
145 plhs[0] =
MxArray(descriptors);
150 mexErrMsgIdAndTxt(
"mexopencv:error",
"Invalid arguments");
155 mexErrMsgIdAndTxt(
"mexopencv:error",
156 "Unrecognized operation %s",method.c_str());
const ConstMap< int, std::string > NormTypeInv
Inverse norm type map for option processing.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Common definitions for the features2d and xfeatures2d modules.
const ConstMap< int, std::string > ClassNameInvMap
Translates data type definition used in OpenCV to that of MATLAB.
Global constant definitions.
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.