mexopencv  3.4.1
MEX interface for OpenCV library
Plot2d_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/plot.hpp"
10 using namespace std;
11 using namespace cv;
12 using namespace cv::plot;
13 
14 // Persistent objects
15 namespace {
17 int last_id = 0;
20 }
21 
29 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
30 {
31  // Check the number of arguments
32  nargchk(nrhs>=2 && nlhs<=1);
33 
34  // Arguments vector
35  vector<MxArray> rhs(prhs, prhs+nrhs);
36  int id = rhs[0].toInt();
37  string method(rhs[1].toString());
38 
39  // Constructor is called. Create a new object from argument
40  if (method == "new") {
41  nargchk((nrhs==3||nrhs==4) && nlhs<=1);
42  Ptr<Plot2d> p;
43  if (nrhs == 3) {
44  Mat data(rhs[2].toMat(CV_64F));
45  p = Plot2d::create(data);
46  }
47  else {
48  Mat dataX(rhs[2].toMat(CV_64F)),
49  dataY(rhs[3].toMat(CV_64F));
50  p = Plot2d::create(dataX, dataY);
51  }
52  obj_[++last_id] = p;
53  plhs[0] = MxArray(last_id);
54  mexLock();
55  return;
56  }
57 
58  // Big operation switch
59  Ptr<Plot2d> obj = obj_[id];
60  if (obj.empty())
61  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
62  if (method == "delete") {
63  nargchk(nrhs==2 && nlhs==0);
64  obj_.erase(id);
65  mexUnlock();
66  }
67  else if (method == "clear") {
68  nargchk(nrhs==2 && nlhs==0);
69  obj->clear();
70  }
71  else if (method == "load") {
72  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs==0);
73  string objname;
74  bool loadFromString = false;
75  for (int i=3; i<nrhs; i+=2) {
76  string key(rhs[i].toString());
77  if (key == "ObjName")
78  objname = rhs[i+1].toString();
79  else if (key == "FromString")
80  loadFromString = rhs[i+1].toBool();
81  else
82  mexErrMsgIdAndTxt("mexopencv:error",
83  "Unrecognized option %s", key.c_str());
84  }
85  /*
86  obj_[id] = (loadFromString ?
87  Algorithm::loadFromString<Plot2d>(rhs[2].toString(), objname) :
88  Algorithm::load<Plot2d>(rhs[2].toString(), objname));
89  */
91  // HACK: workaround for missing Plot2d::create()
92  FileStorage fs(rhs[2].toString(), FileStorage::READ +
93  (loadFromString ? FileStorage::MEMORY : 0));
94  if (!fs.isOpened())
95  mexErrMsgIdAndTxt("mexopencv:error", "Failed to open file");
96  FileNode fn(objname.empty() ? fs.getFirstTopLevelNode() : fs[objname]);
97  if (fn.empty())
98  mexErrMsgIdAndTxt("mexopencv:error", "Failed to get node");
99  obj->read(fn);
100  //*/
101  }
102  else if (method == "save") {
103  nargchk(nrhs==3 && nlhs==0);
104  obj->save(rhs[2].toString());
105  }
106  else if (method == "empty") {
107  nargchk(nrhs==2 && nlhs<=1);
108  plhs[0] = MxArray(obj->empty());
109  }
110  else if (method == "getDefaultName") {
111  nargchk(nrhs==2 && nlhs<=1);
112  plhs[0] = MxArray(obj->getDefaultName());
113  }
114  else if (method == "render") {
115  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
116  bool flip = true;
117  for (int i=2; i<nrhs; i+=2) {
118  string key(rhs[i].toString());
119  if (key == "FlipChannels")
120  flip = rhs[i+1].toBool();
121  else
122  mexErrMsgIdAndTxt("mexopencv:error",
123  "Unrecognized option %s", key.c_str());
124  }
125  Mat img;
126  obj->render(img);
127  if (flip && (img.channels() == 3 || img.channels() == 4)) {
128  // OpenCV's default is BGR/BGRA while MATLAB's is RGB/RGBA
129  cvtColor(img, img, (img.channels()==3 ?
131  }
132  plhs[0] = MxArray(img);
133  }
134  else if (method == "set") {
135  nargchk(nrhs==4 && nlhs==0);
136  string prop(rhs[2].toString());
137  if (prop == "MinX")
138  obj->setMinX(rhs[3].toDouble());
139  else if (prop == "MinY")
140  obj->setMinY(rhs[3].toDouble());
141  else if (prop == "MaxX")
142  obj->setMaxX(rhs[3].toDouble());
143  else if (prop == "MaxY")
144  obj->setMaxY(rhs[3].toDouble());
145  else if (prop == "PlotLineWidth")
146  obj->setPlotLineWidth(rhs[3].toInt());
147  else if (prop == "NeedPlotLine")
148  obj->setNeedPlotLine(rhs[3].toBool());
149  else if (prop == "PlotLineColor")
150  obj->setPlotLineColor((rhs[3].isChar()) ?
151  ColorType[rhs[3].toString()] : rhs[3].toScalar());
152  else if (prop == "PlotBackgroundColor")
153  obj->setPlotBackgroundColor((rhs[3].isChar()) ?
154  ColorType[rhs[3].toString()] : rhs[3].toScalar());
155  else if (prop == "PlotAxisColor")
156  obj->setPlotAxisColor((rhs[3].isChar()) ?
157  ColorType[rhs[3].toString()] : rhs[3].toScalar());
158  else if (prop == "PlotGridColor")
159  obj->setPlotGridColor((rhs[3].isChar()) ?
160  ColorType[rhs[3].toString()] : rhs[3].toScalar());
161  else if (prop == "PlotTextColor")
162  obj->setPlotTextColor((rhs[3].isChar()) ?
163  ColorType[rhs[3].toString()] : rhs[3].toScalar());
164  else if (prop == "PlotSize") {
165  Size sz(rhs[3].toSize());
166  obj->setPlotSize(sz.width, sz.height);
167  }
168  else if (prop == "ShowGrid")
169  obj->setShowGrid(rhs[3].toBool());
170  else if (prop == "ShowText")
171  obj->setShowText(rhs[3].toBool());
172  else if (prop == "GridLinesNumber")
173  obj->setGridLinesNumber(rhs[3].toInt());
174  else if (prop == "InvertOrientation")
175  obj->setInvertOrientation(rhs[3].toBool());
176  else if (prop == "PointIdxToPrint")
177  obj->setPointIdxToPrint(rhs[3].toInt());
178  else
179  mexErrMsgIdAndTxt("mexopencv:error",
180  "Unrecognized property %s", prop.c_str());
181  }
182  else
183  mexErrMsgIdAndTxt("mexopencv:error",
184  "Unrecognized operation %s", method.c_str());
185 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
Definition: Plot2d_.cpp:29
int last_id
Last object id to allocate.
Definition: Plot2d_.cpp:17
T empty(T... args)
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
COLOR_BGR2RGB
virtual void setGridLinesNumber(int gridLinesNumber)=0
virtual void setNeedPlotLine(bool _needPlotLine)=0
virtual void setMinY(double _plotMinY)=0
STL namespace.
virtual bool isOpened() const
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
STL class.
virtual void setShowText(bool needShowText)=0
virtual void clear()
virtual void setPlotTextColor(Scalar _plotTextColor)=0
COLOR_BGRA2RGBA
virtual void setInvertOrientation(bool _invertOrientation)=0
virtual void setPlotLineColor(Scalar _plotLineColor)=0
virtual void read(const FileNode &fn)
virtual void setPlotAxisColor(Scalar _plotAxisColor)=0
virtual void setMaxX(double _plotMaxX)=0
virtual void setPlotGridColor(Scalar _plotGridColor)=0
#define CV_64F
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...
virtual void setPlotSize(int _plotSizeWidth, int _plotSizeHeight)=0
virtual void setPointIdxToPrint(int pointIdx)=0
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
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
FileNode getFirstTopLevelNode() const
virtual void setMinX(double _plotMinX)=0
STL class.
bool empty() const
virtual void setShowGrid(bool needShowGrid)=0
virtual String getDefaultName() const
Global constant definitions.
T c_str(T... args)
virtual void setMaxY(double _plotMaxY)=0
virtual void setPlotBackgroundColor(Scalar _plotBackgroundColor)=0
void flip(InputArray src, OutputArray dst, int flipCode)
virtual void save(const String &filename) const
virtual bool empty() const
virtual void render(OutputArray _plotResult)=0
map< int, Ptr< Plot2d > > obj_
Object container.
Definition: Plot2d_.cpp:19
const ConstMap< std::string, cv::Scalar > ColorType
Translates MATLAB color names (see ColorSpec) into OpenCV scalars.
Definition: mexopencv.hpp:55
void create(int arows, int acols, int atype, Target target=ARRAY_BUFFER, bool autoRelease=false)
virtual void setPlotLineWidth(int _plotLineWidth)=0
cv::Mat toMat() const
int channels() const