mexopencv  3.4.1
MEX interface for OpenCV library
VideoCapture_.cpp
Go to the documentation of this file.
1 
8 #include "mexopencv.hpp"
9 #include "opencv2/videoio.hpp"
10 using namespace std;
11 using namespace cv;
12 
13 namespace {
14 // Persistent objects
16 int last_id = 0;
19 
22  ("PosMsec", cv::CAP_PROP_POS_MSEC)
23  ("PosFrames", cv::CAP_PROP_POS_FRAMES)
24  ("PosAviRatio", cv::CAP_PROP_POS_AVI_RATIO)
25  ("FrameWidth", cv::CAP_PROP_FRAME_WIDTH)
26  ("FrameHeight", cv::CAP_PROP_FRAME_HEIGHT)
27  ("FPS", cv::CAP_PROP_FPS)
28  ("FourCC", cv::CAP_PROP_FOURCC)
29  ("FrameCount", cv::CAP_PROP_FRAME_COUNT)
30  ("Format", cv::CAP_PROP_FORMAT)
31  ("Mode", cv::CAP_PROP_MODE)
32  ("Brightness", cv::CAP_PROP_BRIGHTNESS)
33  ("Contrast", cv::CAP_PROP_CONTRAST)
34  ("Saturation", cv::CAP_PROP_SATURATION)
35  ("Hue", cv::CAP_PROP_HUE)
36  ("Gain", cv::CAP_PROP_GAIN)
37  ("Exposure", cv::CAP_PROP_EXPOSURE)
38  ("ConvertRGB", cv::CAP_PROP_CONVERT_RGB)
39  //("WhiteBalanceBlue", cv::CAP_PROP_WHITE_BALANCE_BLUE_U)
40  ("Rectification", cv::CAP_PROP_RECTIFICATION)
41  //TODO: other undocumented properties
42  ("Monochrome", cv::CAP_PROP_MONOCHROME)
43  ("Sharpness", cv::CAP_PROP_SHARPNESS)
44  ("AutoExposure", cv::CAP_PROP_AUTO_EXPOSURE)
45  ("Gamma", cv::CAP_PROP_GAMMA)
46  ("Temperature", cv::CAP_PROP_TEMPERATURE)
47  ("Trigger", cv::CAP_PROP_TRIGGER)
48  ("TriggerDelay", cv::CAP_PROP_TRIGGER_DELAY)
49  //("WhiteBalanceRed", cv::CAP_PROP_WHITE_BALANCE_RED_V)
50  ("Zoom", cv::CAP_PROP_ZOOM)
51  ("Focus", cv::CAP_PROP_FOCUS)
52  ("GUID", cv::CAP_PROP_GUID)
53  ("ISOSpeed", cv::CAP_PROP_ISO_SPEED)
54  ("Backlight", cv::CAP_PROP_BACKLIGHT)
55  ("Pan", cv::CAP_PROP_PAN)
56  ("Tilt", cv::CAP_PROP_TILT)
57  ("Roll", cv::CAP_PROP_ROLL)
58  ("Iris", cv::CAP_PROP_IRIS)
59  ("Settings", cv::CAP_PROP_SETTINGS)
60  ("Buffersize", cv::CAP_PROP_BUFFERSIZE)
61  ("Autofocus", cv::CAP_PROP_AUTOFOCUS)
62  ("SARNum", cv::CAP_PROP_SAR_NUM)
63  ("SARDen", cv::CAP_PROP_SAR_DEN);
64 
67  ("Any", cv::CAP_ANY)
68  ("VfW", cv::CAP_VFW)
69  ("V4L", cv::CAP_V4L)
70  ("V4L2", cv::CAP_V4L2)
71  ("FireWire", cv::CAP_FIREWIRE)
72  ("FireWare", cv::CAP_FIREWARE)
73  ("IEEE1394", cv::CAP_IEEE1394)
74  ("DC1394", cv::CAP_DC1394)
75  ("CMU1394", cv::CAP_CMU1394)
76  ("QuickTime", cv::CAP_QT)
77  ("Unicap", cv::CAP_UNICAP)
78  ("DirectShow", cv::CAP_DSHOW)
79  ("PvAPI", cv::CAP_PVAPI)
80  ("OpenNI", cv::CAP_OPENNI)
81  ("OpenNIAsus", cv::CAP_OPENNI_ASUS)
82  ("Android", cv::CAP_ANDROID)
83  ("XIMEA", cv::CAP_XIAPI)
84  ("AVFoundation", cv::CAP_AVFOUNDATION)
85  ("Giganetix", cv::CAP_GIGANETIX)
86  ("MediaFoundation", cv::CAP_MSMF)
87  ("WinRT", cv::CAP_WINRT)
88  ("IntelPerC", cv::CAP_INTELPERC)
89  ("OpenNI2", cv::CAP_OPENNI2)
90  ("OpenNI2Asus", cv::CAP_OPENNI2_ASUS)
91  ("gPhoto2", cv::CAP_GPHOTO2)
92  ("GStreamer", cv::CAP_GSTREAMER)
93  ("FFMPEG", cv::CAP_FFMPEG)
94  ("Images", cv::CAP_IMAGES)
95  ("Aravis", cv::CAP_ARAVIS)
96  ("MotionJPEG", cv::CAP_OPENCV_MJPEG)
97  ("MediaSDK", cv::CAP_INTEL_MFX);
98 }
99 
107 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
108 {
109  // Check the number of arguments
110  nargchk(nrhs>=2 && nlhs<=1);
111 
112  // Argument vector
113  vector<MxArray> rhs(prhs, prhs+nrhs);
114  int id = rhs[0].toInt();
115  string method(rhs[1].toString());
116 
117  // Constructor is called. Create a new object from arguments
118  if (method == "new") {
119  nargchk(nrhs==2 && nlhs<=1);
120  obj_[++last_id] = makePtr<VideoCapture>();
121  plhs[0] = MxArray(last_id);
122  mexLock();
123  return;
124  }
125 
126  // Big operation switch
127  Ptr<VideoCapture> obj = obj_[id];
128  if (obj.empty())
129  mexErrMsgIdAndTxt("mexopencv:error", "Object not found id=%d", id);
130  if (method == "delete") {
131  nargchk(nrhs==2 && nlhs==0);
132  obj_.erase(id);
133  mexUnlock();
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 propId = (rhs[2].isChar()) ?
206  CapProp[rhs[2].toString()] : rhs[2].toInt();
207  double value = obj->get(propId);
208  plhs[0] = MxArray(value);
209  }
210  else if (method == "set") {
211  nargchk(nrhs==4 && nlhs==0);
212  int propId = (rhs[2].isChar()) ?
213  CapProp[rhs[2].toString()] : rhs[2].toInt();
214  double value = rhs[3].toDouble();
215  bool success = obj->set(propId, value);
216  if (!success)
217  mexWarnMsgIdAndTxt("mexopencv:error",
218  "Error setting property %d", propId);
219  }
220  else
221  mexErrMsgIdAndTxt("mexopencv:error",
222  "Unrecognized operation %s", method.c_str());
223 }
CAP_PROP_BRIGHTNESS
CAP_DSHOW
CAP_PROP_ISO_SPEED
CAP_PROP_MODE
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
virtual bool open(const String &filename)
virtual void release()
CAP_GIGANETIX
CAP_DC1394
CAP_PROP_AUTOFOCUS
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Main entry called from Matlab.
LIBMWMEX_API_EXTERN_C void mexWarnMsgIdAndTxt(const char *identifier, const char *warn_msg,...)
Invoke a warning with message identifier &#39;identifier&#39; and message derived from &#39;fmt&#39; and subsequent a...
CAP_IMAGES
CAP_GSTREAMER
CAP_PROP_SETTINGS
CAP_PROP_PAN
LIBMWMEX_API_EXTERN_C void mexLock(void)
Lock a MEX-function so that it cannot be cleared from memory.
COLOR_BGR2RGB
const ConstMap< string, int > CameraApiMap
Camera API map for option processing.
CAP_OPENNI2
virtual bool read(OutputArray image)
CAP_FFMPEG
CAP_PROP_CONTRAST
CAP_OPENNI2_ASUS
CAP_WINRT
STL namespace.
CAP_INTEL_MFX
CAP_PROP_ZOOM
CAP_PROP_EXPOSURE
struct mxArray_tag mxArray
Forward declaration for mxArray.
Definition: matrix.h:259
CAP_PROP_FOCUS
STL class.
CAP_PROP_GAMMA
const ConstMap< string, int > CapProp
Capture Property map for option processing.
CAP_PROP_TRIGGER
CAP_PROP_CONVERT_RGB
CAP_MSMF
CAP_PROP_FRAME_COUNT
CAP_OPENNI_ASUS
CAP_PROP_SHARPNESS
CAP_GPHOTO2
CAP_PROP_FOURCC
CAP_PROP_FRAME_WIDTH
CAP_PROP_AUTO_EXPOSURE
CAP_PROP_GUID
CAP_PROP_POS_FRAMES
CAP_PROP_ROLL
CAP_PROP_GAIN
CAP_OPENNI
CAP_PROP_FRAME_HEIGHT
CAP_PROP_BUFFERSIZE
CAP_PROP_IRIS
CAP_INTELPERC
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...
int last_id
Last object id to allocate.
CAP_CMU1394
map< int, Ptr< VideoCapture > > obj_
Object container.
LIBMWMEX_API_EXTERN_C void mexUnlock(void)
Unlock a locked MEX-function so that it can be cleared from memory.
virtual bool retrieve(OutputArray image, int flag=0)
CAP_PROP_SATURATION
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
CAP_PROP_MONOCHROME
CAP_FIREWIRE
CAP_PROP_FORMAT
STL class.
bool empty() const
Global constant definitions.
T c_str(T... args)
virtual bool set(int propId, double value)
CAP_PROP_HUE
CAP_OPENCV_MJPEG
CAP_PROP_RECTIFICATION
CAP_PROP_POS_MSEC
CAP_ANDROID
CAP_FIREWARE
CAP_PROP_BACKLIGHT
CAP_ARAVIS
virtual bool grab()
void flip(InputArray src, OutputArray dst, int flipCode)
CAP_PROP_FPS
CAP_PROP_SAR_NUM
CAP_PROP_TEMPERATURE
CAP_IEEE1394
CAP_PROP_SAR_DEN
CAP_PROP_TRIGGER_DELAY
CAP_V4L2
std::map wrapper with one-line initialization and lookup method.
Definition: MxArray.hpp:927
CAP_PROP_TILT
CAP_UNICAP
CAP_PROP_POS_AVI_RATIO
CAP_XIAPI
virtual double get(int propId) const
CAP_PVAPI
int channels() const
virtual bool isOpened() const
CAP_AVFOUNDATION