17 map<int,Ptr<PCA> > obj_;
21 (
"Row", PCA::DATA_AS_ROW)
22 (
"Col", PCA::DATA_AS_COL);
32 void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs,
const mxArray *prhs[])
38 vector<MxArray> rhs(prhs, prhs+nrhs);
39 int id = rhs[0].toInt();
40 string method(rhs[1].toString());
43 if (method ==
"new") {
45 obj_[++last_id] = makePtr<PCA>();
51 Ptr<PCA> obj = obj_[id];
52 if (method ==
"delete") {
56 else if (method ==
"read") {
57 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
58 bool loadFromString =
false;
59 for (
int i=3; i<nrhs; i+=2) {
60 string key(rhs[i].toString());
61 if (key==
"FromString")
62 loadFromString = rhs[i+1].toBool();
64 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
66 FileStorage fs(rhs[2].toString(), FileStorage::READ +
67 (loadFromString ? FileStorage::MEMORY : 0));
69 mexErrMsgIdAndTxt(
"mexopencv:error",
"Failed to open file");
72 else if (method ==
"write") {
74 FileStorage fs(rhs[2].toString(), FileStorage::WRITE);
77 else if (method ==
"compute") {
78 nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
80 int flags = PCA::DATA_AS_ROW;
81 int maxComponents = 0;
82 double retainedVariance = 1.0;
83 bool use_second_variant =
false;
84 for (
int i=3; i<nrhs; i+=2) {
85 string key(rhs[i].toString());
87 mean = rhs[i+1].toMat();
88 else if (key==
"DataAs")
89 flags = DataAs[rhs[i+1].toString()];
90 else if (key==
"MaxComponents")
91 maxComponents = rhs[i+1].toInt();
92 else if (key==
"RetainedVariance") {
93 retainedVariance = rhs[i+1].toDouble();
94 use_second_variant =
true;
97 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
99 Mat data(rhs[2].toMat());
100 if (use_second_variant)
101 obj->operator()(data, mean, flags, retainedVariance);
103 obj->operator()(data, mean, flags, maxComponents);
105 else if (method ==
"project") {
107 plhs[0] =
MxArray(obj->project(rhs[2].toMat()));
109 else if (method ==
"backProject") {
111 plhs[0] =
MxArray(obj->backProject(rhs[2].toMat()));
113 else if (method ==
"get") {
115 string prop(rhs[2].toString());
116 if (prop ==
"eigenvectors")
117 plhs[0] =
MxArray(obj->eigenvectors);
118 else if (prop ==
"eigenvalues")
119 plhs[0] =
MxArray(obj->eigenvalues);
120 else if (prop ==
"mean")
123 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
125 else if (method ==
"set") {
127 string prop(rhs[2].toString());
128 if (prop ==
"eigenvectors")
129 obj->eigenvectors = rhs[3].toMat();
130 else if (prop ==
"eigenvalues")
131 obj->eigenvalues = rhs[3].toMat();
132 else if (prop ==
"mean")
133 obj->mean = rhs[3].toMat();
135 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized option");
138 mexErrMsgIdAndTxt(
"mexopencv:error",
"Unrecognized operation");
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.