1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 // 2006-02-17 Roman Stanchak <rstancha@cse.wustl.edu>
43 // 2006-07-19 Moved most operators to general/cvarr_operators.i for use with other languages
45 /*M//////////////////////////////////////////////////////////////////////////////////////////
46 // Macros for extending CvMat and IplImage -- primarily for operator overloading
47 //////////////////////////////////////////////////////////////////////////////////////////M*/
49 // Macro to define python function of form B = A.f(c)
50 // where A is a CvArr type, c and B are arbitrary types
51 %define %wrap_cvGeneric_CvArr(cname, rettype, pyfunc, argtype, cvfunc, newobjcall)
52 %newobject cname::pyfunc(argtype arg);
54 rettype pyfunc(argtype arg){
55 rettype retarg = newobjcall;
62 // Macro to define python function of the form B = A.f(c)
63 // where A and B are both CvArr of same size and type
64 %define %wrap_cvArr_binaryop(pyfunc, argtype, cvfunc)
65 %wrap_cvGeneric_CvArr(CvMat, CvMat *, pyfunc, argtype, cvfunc,
66 cvCreateMat(self->rows, self->cols, self->type));
67 %wrap_cvGeneric_CvArr(IplImage, IplImage *, pyfunc, argtype, cvfunc,
68 cvCreateImage(cvGetSize(self), self->depth, self->nChannels));
71 // Macro to define python function of the form A = A.f(c)
72 // where f modifies A inplace
74 %define %wrap_cvGeneric_InPlace(cname, rettype, pyfunc, argtype, cvfunc)
75 %wrap_cvGeneric_CvArr(cname, rettype, pyfunc, argtype, cvfunc, self);
78 /*M//////////////////////////////////////////////////////////////////////////////////////////
79 // Macros to map operators to specific OpenCV functions
80 //////////////////////////////////////////////////////////////////////////////////////////M*/
82 // map any OpenCV function of form cvFunc(src1, src2, dst)
83 %define %wrap_cvArith(pyfunc, cvfunc)
84 %wrap_cvArr_binaryop(pyfunc, CvArr *, cvfunc(self, arg, retarg));
87 // map any OpenCV function of form cvFunc(src1, value, dst)
88 %define %wrap_cvArithS(pyfunc, cvfuncS)
89 %wrap_cvArr_binaryop(pyfunc, CvScalar, cvfuncS(self, arg, retarg));
90 %wrap_cvArr_binaryop(pyfunc, double, cvfuncS(self, cvScalar(arg), retarg));
93 // same as wrap_cvArith
94 %define %wrap_cvLogic(pyfunc, cvfunc)
95 %wrap_cvArr_binaryop(pyfunc, CvArr *, cvfunc(self, arg, retarg))
98 // same as wrap_cvArithS
99 %define %wrap_cvLogicS(pyfunc, cvfuncS)
100 %wrap_cvArr_binaryop(pyfunc, CvScalar, cvfuncS(self, arg, retarg));
101 %wrap_cvArr_binaryop(pyfunc, double, cvfuncS(self, cvScalar(arg), retarg));
104 // Macro to map logical operations to cvCmp
105 %define %wrap_cvCmp(pyfunc, cmp_op)
106 %wrap_cvGeneric_CvArr(CvMat, CvArr *, pyfunc, CvMat *,
107 cvCmp(self, arg, retarg, cmp_op),
108 cvCreateMat(self->rows, self->cols, CV_8U));
109 %wrap_cvGeneric_CvArr(IplImage, CvArr *, pyfunc, IplImage *,
110 cvCmp(self, arg, retarg, cmp_op),
111 cvCreateImage(cvGetSize(self), 8, 1));
114 %define %wrap_cvCmpS(pyfunc, cmp_op)
115 %wrap_cvGeneric_CvArr(CvMat, CvArr *, pyfunc, double,
116 cvCmpS(self, arg, retarg, cmp_op),
117 cvCreateMat(self->rows, self->cols, CV_8U));
118 %wrap_cvGeneric_CvArr(IplImage, CvArr *, pyfunc, double,
119 cvCmpS(self, arg, retarg, cmp_op),
120 cvCreateImage(cvGetSize(self), 8, 1));
123 // special case for cvScale, /, *
124 %define %wrap_cvScale(pyfunc, scale)
125 %wrap_cvGeneric_CvArr(CvMat, CvArr *, pyfunc, double,
126 cvScale(self, retarg, scale),
127 cvCreateMat(self->rows, self->cols, self->type));
128 %wrap_cvGeneric_CvArr(IplImage, CvArr *, pyfunc, double,
129 cvScale(self, retarg, scale),
130 cvCreateImage(cvGetSize(self), self->depth, self->nChannels));
133 /*M//////////////////////////////////////////////////////////////////////////////////////////
134 // Actual Operator Declarations
135 //////////////////////////////////////////////////////////////////////////////////////////M*/
137 // Arithmetic operators
138 %wrap_cvArith(__radd__, cvAdd);
140 // special case for reverse operations
141 %wrap_cvArr_binaryop(__rsub__, CvArr *, cvSub(arg, self, retarg));
142 %wrap_cvArr_binaryop(__rdiv__, CvArr *, cvDiv(arg, self, retarg));
143 %wrap_cvArr_binaryop(__rmul__, CvArr *, cvMatMul(arg, self, retarg));
145 %wrap_cvArithS(__radd__, cvAddS);
146 %wrap_cvArithS(__rsub__, cvSubRS);
149 %wrap_cvScale(__rmul__, arg);
150 %wrap_cvScale(__rdiv__, 1.0/arg);
152 %wrap_cvLogicS(__ror__, cvOrS)
153 %wrap_cvLogicS(__rand__, cvAndS)
154 %wrap_cvLogicS(__rxor__, cvXorS)
156 %wrap_cvCmpS(__req__, CV_CMP_EQ);
157 %wrap_cvCmpS(__rgt__, CV_CMP_GT);
158 %wrap_cvCmpS(__rge__, CV_CMP_GE);
159 %wrap_cvCmpS(__rlt__, CV_CMP_LT);
160 %wrap_cvCmpS(__rle__, CV_CMP_LE);
161 %wrap_cvCmpS(__rne__, CV_CMP_NE);
164 // misc operators for python
165 %wrap_cvArr_binaryop(__pow__, double, cvPow(self, retarg, arg))
167 // TODO -- other Python operators listed below and at:
168 // http://docs.python.org/ref/numeric-types.html
173 // __repr__ -- full string representation
174 // __str__ -- compact representation
176 // __len__ -- number of rows? or elements?
178 // __contains__ -- cvCmpS, cvMax ?
185 // Called to implement the unary arithmetic operations (-, +, abs() and ~).
191 // Called to implement the built-in functions complex(), int(), long(), and float(). Should return a value of the appropriate type. Can I abuse this to return an array of the correct type??? scipy only allows return of length 1 arrays.
192 // __complex__( self )
197 /*M//////////////////////////////////////////////////////////////////////////////////////////
198 // Slice access and assignment for CvArr types
199 //////////////////////////////////////////////////////////////////////////////////////////M*/
203 %newobject CvMat::__getitem__(PyObject * object);
204 %newobject _IplImage::__getitem__(PyObject * object);
206 // Macro to check bounds of slice and throw error if outside
207 %define CHECK_SLICE_BOUNDS(rect,w,h,retval)
208 //printf("__setitem__ slice(%d:%d, %d:%d) array(%d,%d)", rect.x, rect.y, rect.x+rect.width, rect.y+rect.height, w, h);
209 if(rect.width<=0 || rect.height<=0 ||
210 rect.width>w || rect.height>h ||
211 rect.x<0 || rect.y<0 ||
212 rect.x>= w || rect.y >=h){
214 // previous function already set error string
215 if(rect.width==0 && rect.height==0 && rect.x==0 && rect.y==0) return retval;
216 sprintf(errstr, "Requested slice [ %d:%d %d:%d ] oversteps array sized [ %d %d ]",
217 rect.x, rect.y, rect.x+rect.width, rect.y+rect.height, w, h);
218 PyErr_SetString(PyExc_IndexError, errstr);
219 //PyErr_SetString(PyExc_ValueError, errstr);
225 // slice access and assignment for CvMat
236 void __setitem__(PyObject * object, double val){
238 CvRect subrect = PySlice_to_CvRect( self, object );
239 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
240 cvGetSubRect(self, &tmp, subrect);
241 cvSet(&tmp, cvScalarAll(val));
243 void __setitem__(PyObject * object, CvPoint val){
245 CvRect subrect = PySlice_to_CvRect( self, object );
246 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
247 cvGetSubRect(self, &tmp, subrect);
248 cvSet(&tmp, cvScalar(val.x, val.y));
250 void __setitem__(PyObject * object, CvPoint2D32f val){
252 CvRect subrect = PySlice_to_CvRect( self, object );
253 cvGetSubRect(self, &tmp, subrect);
254 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
255 cvSet(&tmp, cvScalar(val.x, val.y));
257 void __setitem__(PyObject * object, CvScalar val){
259 CvRect subrect = PySlice_to_CvRect( self, object );
260 cvGetSubRect(self, &tmp, subrect);
261 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
265 // array slice assignment
266 void __setitem__(PyObject * object, CvArr * arr){
267 CvMat tmp, src_stub, *src;
268 CvRect subrect = PySlice_to_CvRect( self, object );
269 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
270 cvGetSubRect(self, &tmp, subrect);
272 // Reshape source array to fit destination
273 // This will be used a lot for small arrays b/c
274 // PyObject_to_CvArr tries to compress a 2-D python
275 // array with 1-4 columns into a multichannel vector
276 src=cvReshape(arr, &src_stub, CV_MAT_CN(tmp.type), tmp.rows);
278 cvConvert(src, &tmp);
282 PyObject * __getitem__(PyObject * object){
284 CvRect subrect = PySlice_to_CvRect( self, object );
285 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, NULL );
286 if(subrect.width==1 && subrect.height==1){
288 int type = cvGetElemType( self );
289 if(CV_MAT_CN(type) > 1){
291 *s = cvGet2D( self, subrect.y, subrect.x );
292 return SWIG_NewPointerObj( s, $descriptor(CvScalar *), 1 );
294 switch(CV_MAT_DEPTH(type)){
296 return PyLong_FromUnsignedLong( CV_MAT_ELEM(*self, uchar, subrect.y, subrect.x ) );
298 return PyLong_FromLong( CV_MAT_ELEM(*self, char, subrect.y, subrect.x ) );
300 return PyLong_FromUnsignedLong( CV_MAT_ELEM(*self, ushort, subrect.y, subrect.x ) );
302 return PyLong_FromLong( CV_MAT_ELEM(*self, short, subrect.y, subrect.x ) );
304 return PyLong_FromLong( CV_MAT_ELEM(*self, int, subrect.y, subrect.x ) );
306 return PyFloat_FromDouble( CV_MAT_ELEM(*self, float, subrect.y, subrect.x) );
308 return PyFloat_FromDouble( CV_MAT_ELEM(*self, double, subrect.y, subrect.x) );
311 mat = (CvMat *) cvAlloc(sizeof(CvMat));
312 cvGetSubRect(self, mat, subrect);
314 // cvGetSubRect doesn't do this since it assumes mat lives on the stack
315 mat->hdr_refcount = self->hdr_refcount;
316 mat->refcount = self->refcount;
319 return SWIG_NewPointerObj( mat, $descriptor(CvMat *), 1 );
324 generator function iterating through rows in matrix or elements in vector
327 return self.colrange()
328 return self.rowrange()
332 generator function iterating along rows in matrix
334 for i in range(self.rows):
339 generator function iterating along columns in matrix
341 for i in range(self.cols):
344 # if arg is None, python still calls our operator overloads
348 # to do the right thing -- so redefine __ne__ and __eq__
350 def __eq__(self, arg):
353 __eq__(self, CvArr src)
354 __eq__(self, double val)
359 return _cv.CvMat___eq__(self, arg)
360 def __ne__(self, arg):
363 __ne__(self, CvArr src)
364 __ne__(self, double val)
369 return _cv.CvMat___ne__(self, arg)
374 // slice access and assignment for IplImage
385 void __setitem__(PyObject * object, double val){
387 CvRect subrect = PySlice_to_CvRect( self, object );
388 cvGetSubRect(self, &tmp, subrect);
389 cvSet(&tmp, cvScalarAll(val));
391 void __setitem__(PyObject * object, CvPoint val){
393 CvRect subrect = PySlice_to_CvRect( self, object );
394 cvGetSubRect(self, &tmp, subrect);
395 cvSet(&tmp, cvScalar(val.x, val.y));
397 void __setitem__(PyObject * object, CvPoint2D32f val){
399 CvRect subrect = PySlice_to_CvRect( self, object );
400 cvGetSubRect(self, &tmp, subrect);
401 cvSet(&tmp, cvScalar(val.x, val.y));
403 void __setitem__(PyObject * object, CvScalar val){
405 CvRect subrect = PySlice_to_CvRect( self, object );
406 cvGetSubRect(self, &tmp, subrect);
410 // array slice assignment
411 void __setitem__(PyObject * object, CvArr * arr){
413 CvRect subrect = PySlice_to_CvRect( self, object );
414 cvGetSubRect(self, &tmp, subrect);
415 cvConvert(arr, &tmp);
419 PyObject * __getitem__(PyObject * object){
422 CvRect subrect = PySlice_to_CvRect( self, object );
424 // return scalar if single element
425 if(subrect.width==1 && subrect.height==1){
427 int type = cvGetElemType( self );
428 if(CV_MAT_CN(type) > 1){
430 *s = cvGet2D( self, subrect.y, subrect.x );
431 return SWIG_NewPointerObj( s, $descriptor(CvScalar *), 1 );
433 switch(CV_MAT_DEPTH(type)){
435 return PyLong_FromUnsignedLong( CV_IMAGE_ELEM(self, uchar, subrect.y, subrect.x ) );
437 return PyLong_FromLong( CV_IMAGE_ELEM(self, char, subrect.y, subrect.x ) );
439 return PyLong_FromUnsignedLong( CV_IMAGE_ELEM(self, ushort, subrect.y, subrect.x ) );
441 return PyLong_FromLong( CV_IMAGE_ELEM(self, short, subrect.y, subrect.x ) );
443 return PyLong_FromLong( CV_IMAGE_ELEM(self, int, subrect.y, subrect.x ) );
445 return PyFloat_FromDouble( CV_IMAGE_ELEM(self, float, subrect.y, subrect.x) );
447 return PyFloat_FromDouble( CV_IMAGE_ELEM(self, double, subrect.y, subrect.x) );
451 // otherwise return array
452 im = (IplImage *) cvAlloc(sizeof(IplImage));
453 cvGetSubRect(self, &mat, subrect);
454 im = cvGetImage(&mat, im);
455 return SWIG_NewPointerObj( im, $descriptor(_IplImage *), 1 );