mexopencv  0.1
mex interface for opencv library
VideoCapture_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 using namespace std;
10 using namespace cv;
11 
12 namespace {
13 // Persistent objects
15 int last_id = 0;
17 map<int,Ptr<VideoCapture> > obj_;
18 
21  ("PosMsec", cv::CAP_PROP_POS_MSEC)
22  ("PosFrames", cv::CAP_PROP_POS_FRAMES)
23  ("PosAviRatio", cv::CAP_PROP_POS_AVI_RATIO)
24  ("FrameWidth", cv::CAP_PROP_FRAME_WIDTH)
25  ("FrameHeight", cv::CAP_PROP_FRAME_HEIGHT)
26  ("FPS", cv::CAP_PROP_FPS)
27  ("FourCC", cv::CAP_PROP_FOURCC)
28  ("FrameCount", cv::CAP_PROP_FRAME_COUNT)
29  ("Format", cv::CAP_PROP_FORMAT)
30  ("Mode", cv::CAP_PROP_MODE)
31  ("Brightness", cv::CAP_PROP_BRIGHTNESS)
32  ("Contrast", cv::CAP_PROP_CONTRAST)
33  ("Saturation", cv::CAP_PROP_SATURATION)
34  ("Hue", cv::CAP_PROP_HUE)
35  ("Gain", cv::CAP_PROP_GAIN)
36  ("Exposure", cv::CAP_PROP_EXPOSURE)
37  ("ConvertRGB", cv::CAP_PROP_CONVERT_RGB)
38  //("WhiteBalance", cv::CAP_PROP_WHITE_BALANCE)
39  ("Rectification", cv::CAP_PROP_RECTIFICATION)
40  //TODO: other undocumented properties
41  ("Monochrome", cv::CAP_PROP_MONOCHROME)
42  ("Sharpness", cv::CAP_PROP_SHARPNESS)
43  ("AutoExposure", cv::CAP_PROP_AUTO_EXPOSURE)
44  ("Gamma", cv::CAP_PROP_GAMMA)
45  ("Temperature", cv::CAP_PROP_TEMPERATURE)
46  ("Trigger", cv::CAP_PROP_TRIGGER)
47  ("TriggerDelay", cv::CAP_PROP_TRIGGER_DELAY)
48  ("Zoom", cv::CAP_PROP_ZOOM)
49  ("Focus", cv::CAP_PROP_FOCUS)
50  ("GUID", cv::CAP_PROP_GUID)
51  ("ISOSpeed", cv::CAP_PROP_ISO_SPEED)
52  ("Backlight", cv::CAP_PROP_BACKLIGHT)
53  ("Pan", cv::CAP_PROP_PAN)
54  ("Tilt", cv::CAP_PROP_TILT)
55  ("Roll", cv::CAP_PROP_ROLL)
56  ("Iris", cv::CAP_PROP_IRIS)
57  ("Settings", cv::CAP_PROP_SETTINGS)
58  ("Buffersize", cv::CAP_PROP_BUFFERSIZE)
59  ("Autofocus", cv::CAP_PROP_AUTOFOCUS);
60 
62 const ConstMap<string,int> CameraApiMap = ConstMap<string,int>
63  ("Any", cv::CAP_ANY)
64  ("VfW", cv::CAP_VFW)
65  ("V4L", cv::CAP_V4L)
66  ("V4L2", cv::CAP_V4L2)
67  ("FireWare", cv::CAP_FIREWARE)
68  ("FireWire", cv::CAP_FIREWIRE)
69  ("IEEE1394", cv::CAP_IEEE1394)
70  ("DC1394", cv::CAP_DC1394)
71  ("CMU1394", cv::CAP_CMU1394)
72  ("QuickTime", cv::CAP_QT)
73  ("Unicap", cv::CAP_UNICAP)
74  ("DirectShow", cv::CAP_DSHOW)
75  ("PvAPI", cv::CAP_PVAPI)
76  ("OpenNI", cv::CAP_OPENNI)
77  ("OpenNIAsus", cv::CAP_OPENNI_ASUS)
78  ("Android", cv::CAP_ANDROID)
79  ("XIMEA", cv::CAP_XIAPI)
80  ("AVFoundation", cv::CAP_AVFOUNDATION)
81  ("Giganetix", cv::CAP_GIGANETIX)
82  ("MediaFoundation", cv::CAP_MSMF)
83  ("WinRT", cv::CAP_WINRT)
84  ("IntelPerC", cv::CAP_INTELPERC)
85  ("OpenNI2", cv::CAP_OPENNI2)
86  ("OpenNI2Asus", cv::CAP_OPENNI2_ASUS)
87  ("gPhoto2", cv::CAP_GPHOTO2)
88  ("GStreamer", cv::CAP_GSTREAMER)
89  ("FFMPEG", cv::CAP_FFMPEG)
90  ("Images", cv::CAP_IMAGES);
91 }
92 
100 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
101 {
102  // Check the number of arguments
103  nargchk(nrhs>=2 && nlhs<=1);
104 
105  // Argument vector
106  vector<MxArray> rhs(prhs, prhs+nrhs);
107  int id = rhs[0].toInt();
108  string method(rhs[1].toString());
109 
110  // Constructor is called. Create a new object from arguments
111  if (method == "new") {
112  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
113  int pref = cv::CAP_ANY;
114  for (int i=3; i<nrhs; i+=2) {
115  string key(rhs[i].toString());
116  if (key == "API")
117  pref = CameraApiMap[rhs[i+1].toString()];
118  else
119  mexErrMsgIdAndTxt("mexopencv:error",
120  "Unrecognized option %s", key.c_str());
121  }
122  obj_[++last_id] = (rhs[2].isChar()) ?
123  makePtr<VideoCapture>(rhs[2].toString(), pref) :
124  makePtr<VideoCapture>(rhs[2].toInt() + pref);
125  plhs[0] = MxArray(last_id);
126  return;
127  }
128 
129  // Big operation switch
130  Ptr<VideoCapture> obj = obj_[id];
131  if (method == "delete") {
132  nargchk(nrhs==2 && nlhs==0);
133  obj_.erase(id);
134  }
135  else if (method == "open") {
136  nargchk(nrhs>=3 && (nrhs%2)==1 && nlhs<=1);
137  int pref = cv::CAP_ANY;
138  for (int i=3; i<nrhs; i+=2) {
139  string key(rhs[i].toString());
140  if (key == "API")
141  pref = CameraApiMap[rhs[i+1].toString()];
142  else
143  mexErrMsgIdAndTxt("mexopencv:error",
144  "Unrecognized option %s", key.c_str());
145  }
146  // index should be within 0-99, and pref is multiples of 100
147  bool b = (rhs[2].isChar()) ?
148  obj->open(rhs[2].toString(), pref) :
149  obj->open(rhs[2].toInt() + pref);
150  plhs[0] = MxArray(b);
151  }
152  else if (method == "isOpened") {
153  nargchk(nrhs==2 && nlhs<=1);
154  bool b = obj->isOpened();
155  plhs[0] = MxArray(b);
156  }
157  else if (method == "release") {
158  nargchk(nrhs==2 && nlhs==0);
159  obj->release();
160  }
161  else if (method == "grab") {
162  nargchk(nrhs==2 && nlhs<=1);
163  bool b = obj->grab();
164  plhs[0] = MxArray(b);
165  }
166  else if (method == "retrieve") {
167  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
168  int idx = 0;
169  bool flip = true;
170  for (int i=2; i<nrhs; i+=2) {
171  string key(rhs[i].toString());
172  if (key == "FlipChannels")
173  flip = rhs[i+1].toBool();
174  else if (key == "StreamIdx")
175  idx = rhs[i+1].toInt();
176  else
177  mexErrMsgIdAndTxt("mexopencv:error",
178  "Unrecognized option %s", key.c_str());
179  }
180  Mat image;
181  bool b = obj->retrieve(image, idx);
182  if (b && flip && image.channels()==3)
183  cvtColor(image, image, cv::COLOR_BGR2RGB);
184  plhs[0] = MxArray(b ? image : Mat());
185  }
186  else if (method == "read") {
187  nargchk(nrhs>=2 && (nrhs%2)==0 && nlhs<=1);
188  bool flip = true;
189  for (int i=2; i<nrhs; i+=2) {
190  string key(rhs[i].toString());
191  if (key == "FlipChannels")
192  flip = rhs[i+1].toBool();
193  else
194  mexErrMsgIdAndTxt("mexopencv:error",
195  "Unrecognized option %s", key.c_str());
196  }
197  Mat image;
198  bool b = obj->read(image);
199  if (b && flip && image.channels()==3)
200  cvtColor(image, image, cv::COLOR_BGR2RGB);
201  plhs[0] = MxArray(b ? image : Mat());
202  }
203  else if (method == "get") {
204  nargchk(nrhs==3 && nlhs<=1);
205  int prop = (rhs[2].isChar()) ?
206  CapProp[rhs[2].toString()] : rhs[2].toInt();
207  double value = obj->get(prop);
208  plhs[0] = MxArray(value);
209  }
210  else if (method == "set") {
211  nargchk(nrhs==4 && nlhs==0);
212  int prop = (rhs[2].isChar()) ?
213  CapProp[rhs[2].toString()] : rhs[2].toInt();
214  double value = rhs[3].toDouble();
215  bool success = obj->set(prop, value);
216  if (!success)
217  mexWarnMsgIdAndTxt("mexopencv:error",
218  "Error setting property %d", prop);
219  }
220  else
221  mexErrMsgIdAndTxt("mexopencv:error","Unrecognized operation");
222 }
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
mxArray object wrapper for data conversion and manipulation.
Definition: MxArray.hpp:123
void nargchk(bool cond)
Alias for input/ouput arguments number check.
Definition: mexopencv.hpp:166
Global constant definitions.
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927