18 map<int,Ptr<ShapeContextDistanceExtractor> > obj_;
28 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
34 vector<MxArray> rhs(prhs, prhs+nrhs);
35 int id = rhs[0].toInt();
36 string method(rhs[1].toString());
39 if (method ==
"new") {
42 rhs.begin() + 2, rhs.end());
48 Ptr<ShapeContextDistanceExtractor> obj = obj_[id];
49 if (method ==
"delete") {
53 else if (method ==
"clear") {
57 else if (method ==
"load") {
58 nargchk(nrhs>=3 && (nrhs%2)!=0 && nlhs==0);
60 bool loadFromString =
false;
61 for (
int i=3; i<nrhs; i+=2) {
62 string key(rhs[i].toString());
64 objname = rhs[i+1].toString();
65 else if (key ==
"FromString")
66 loadFromString = rhs[i+1].toBool();
68 mexErrMsgIdAndTxt(
"mexopencv:error",
69 "Unrecognized option %s", key.c_str());
78 FileStorage fs(rhs[2].toString(), FileStorage::READ +
79 (loadFromString ? FileStorage::MEMORY : 0));
80 obj->read(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
82 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to load algorithm");
85 else if (method ==
"save") {
87 obj->save(rhs[2].toString());
89 else if (method ==
"empty") {
91 plhs[0] =
MxArray(obj->empty());
93 else if (method ==
"getDefaultName") {
95 plhs[0] =
MxArray(obj->getDefaultName());
97 else if (method ==
"computeDistance") {
100 if (rhs[2].isNumeric() && rhs[3].isNumeric()) {
102 Mat contour1(rhs[2].toMat(CV_32F).reshape(2,1)),
103 contour2(rhs[3].toMat(CV_32F).reshape(2,1));
104 dist = obj->computeDistance(contour1, contour2);
106 else if (rhs[2].isCell() && rhs[3].isCell()) {
107 vector<Point2f> contour1(rhs[2].toVector<Point2f>()),
108 contour2(rhs[3].toVector<Point2f>());
109 dist = obj->computeDistance(contour1, contour2);
112 mexErrMsgIdAndTxt(
"mexopencv:error",
"Invalid argument");
115 else if (method ==
"setImages") {
117 Mat image1(rhs[2].toMat(CV_8U)),
118 image2(rhs[3].toMat(CV_8U));
119 obj->setImages(image1, image2);
121 else if (method ==
"setCostExtractor") {
124 rhs[2].toString(), rhs.begin() + 3, rhs.end());
125 obj->setCostExtractor(comparer);
127 else if (method ==
"setTransformAlgorithm") {
130 rhs[2].toString(), rhs.begin() + 3, rhs.end());
131 obj->setTransformAlgorithm(transformer);
133 else if (method ==
"getImages") {
136 obj->getImages(image1, image2);
141 else if (method ==
"getCostExtractor") {
143 Ptr<HistogramCostExtractor> comparer = obj->getCostExtractor();
146 else if (method ==
"getTransformAlgorithm") {
148 Ptr<ShapeTransformer> transformer = obj->getTransformAlgorithm();
151 else if (method ==
"get") {
153 string prop(rhs[2].toString());
154 if (prop ==
"AngularBins")
155 plhs[0] =
MxArray(obj->getAngularBins());
156 else if (prop ==
"RadialBins")
157 plhs[0] =
MxArray(obj->getRadialBins());
158 else if (prop ==
"InnerRadius")
159 plhs[0] =
MxArray(obj->getInnerRadius());
160 else if (prop ==
"OuterRadius")
161 plhs[0] =
MxArray(obj->getOuterRadius());
162 else if (prop ==
"RotationInvariant")
163 plhs[0] =
MxArray(obj->getRotationInvariant());
164 else if (prop ==
"ShapeContextWeight")
165 plhs[0] =
MxArray(obj->getShapeContextWeight());
166 else if (prop ==
"ImageAppearanceWeight")
167 plhs[0] =
MxArray(obj->getImageAppearanceWeight());
168 else if (prop ==
"BendingEnergyWeight")
169 plhs[0] =
MxArray(obj->getBendingEnergyWeight());
170 else if (prop ==
"Iterations")
171 plhs[0] =
MxArray(obj->getIterations());
172 else if (prop ==
"StdDev")
173 plhs[0] =
MxArray(obj->getStdDev());
175 mexErrMsgIdAndTxt(
"mexopencv:error",
176 "Unrecognized property %s", prop.c_str());
178 else if (method ==
"set") {
180 string prop(rhs[2].toString());
181 if (prop ==
"AngularBins")
182 obj->setAngularBins(rhs[3].toInt());
183 else if (prop ==
"RadialBins")
184 obj->setRadialBins(rhs[3].toInt());
185 else if (prop ==
"InnerRadius")
186 obj->setInnerRadius(rhs[3].toFloat());
187 else if (prop ==
"OuterRadius")
188 obj->setOuterRadius(rhs[3].toFloat());
189 else if (prop ==
"RotationInvariant")
190 obj->setRotationInvariant(rhs[3].toBool());
191 else if (prop ==
"ShapeContextWeight")
192 obj->setShapeContextWeight(rhs[3].toFloat());
193 else if (prop ==
"ImageAppearanceWeight")
194 obj->setImageAppearanceWeight(rhs[3].toFloat());
195 else if (prop ==
"BendingEnergyWeight")
196 obj->setBendingEnergyWeight(rhs[3].toFloat());
197 else if (prop ==
"Iterations")
198 obj->setIterations(rhs[3].toInt());
199 else if (prop ==
"StdDev")
200 obj->setStdDev(rhs[3].toFloat());
202 mexErrMsgIdAndTxt(
"mexopencv:error",
203 "Unrecognized property %s", prop.c_str());
206 mexErrMsgIdAndTxt(
"mexopencv:error",
207 "Unrecognized operation %s", method.c_str());
Common definitions for the shape module.
cv::Ptr< cv::ShapeTransformer > create_ShapeTransformer(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of ShapeTransformer using options in arguments.
MxArray toStruct(const std::vector< cv::ml::DTrees::Node > &nodes)
Convert tree nodes to struct array.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.
cv::Ptr< cv::ShapeContextDistanceExtractor > create_ShapeContextDistanceExtractor(std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of ShapeContextDistanceExtractor using options in arguments.
cv::Ptr< cv::HistogramCostExtractor > create_HistogramCostExtractor(const std::string &type, std::vector< MxArray >::const_iterator first, std::vector< MxArray >::const_iterator last)
Create an instance of HistogramCostExtractor using options in arguments.