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, CvMat *, pyfunc, CvMat *,
107 cvCmp(self, arg, retarg, cmp_op),
108 cvCreateMat(self->rows, self->cols, CV_8U));
109 %wrap_cvGeneric_CvArr(IplImage, IplImage *, 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, CvMat *, pyfunc, double,
116 cvCmpS(self, arg, retarg, cmp_op),
117 cvCreateMat(self->rows, self->cols, CV_8U));
118 %wrap_cvGeneric_CvArr(IplImage, IplImage *, 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, CvMat *, pyfunc, double,
126 cvScale(self, retarg, scale),
127 cvCreateMat(self->rows, self->cols, self->type));
128 %wrap_cvGeneric_CvArr(IplImage, IplImage *, 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 *, cvMul(arg, self, retarg));
145 %wrap_cvArithS(__radd__, cvAddS);
146 %wrap_cvArithS(__rsub__, cvSubRS);
148 %wrap_cvScale(__rmul__, arg);
150 %wrap_cvLogicS(__ror__, cvOrS)
151 %wrap_cvLogicS(__rand__, cvAndS)
152 %wrap_cvLogicS(__rxor__, cvXorS)
154 %wrap_cvCmpS(__req__, CV_CMP_EQ);
155 %wrap_cvCmpS(__rgt__, CV_CMP_GT);
156 %wrap_cvCmpS(__rge__, CV_CMP_GE);
157 %wrap_cvCmpS(__rlt__, CV_CMP_LT);
158 %wrap_cvCmpS(__rle__, CV_CMP_LE);
159 %wrap_cvCmpS(__rne__, CV_CMP_NE);
161 // special case for scalar-array division
162 %wrap_cvGeneric_CvArr(CvMat, CvMat *, __rdiv__, double,
163 cvDiv(NULL, self, retarg, arg),
164 cvCreateMat(self->rows, self->cols, self->type));
166 // misc operators for python
167 %wrap_cvArr_binaryop(__pow__, double, cvPow(self, retarg, arg))
169 // TODO -- other Python operators listed below and at:
170 // http://docs.python.org/ref/numeric-types.html
175 // __repr__ -- full string representation
176 // __str__ -- compact representation
178 // __len__ -- number of rows? or elements?
180 // __contains__ -- cvCmpS, cvMax ?
187 // Called to implement the unary arithmetic operations (-, +, abs() and ~).
193 // 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.
194 // __complex__( self )
199 /*M//////////////////////////////////////////////////////////////////////////////////////////
200 // Slice access and assignment for CvArr types
201 //////////////////////////////////////////////////////////////////////////////////////////M*/
205 %newobject CvMat::__getitem__(PyObject * object);
206 %newobject _IplImage::__getitem__(PyObject * object);
208 // Macro to check bounds of slice and throw error if outside
209 %define CHECK_SLICE_BOUNDS(rect,w,h,retval)
210 //printf("__setitem__ slice(%d:%d, %d:%d) array(%d,%d)", rect.x, rect.y, rect.x+rect.width, rect.y+rect.height, w, h);
211 if(rect.width<=0 || rect.height<=0 ||
212 rect.width>w || rect.height>h ||
213 rect.x<0 || rect.y<0 ||
214 rect.x>= w || rect.y >=h){
216 // previous function already set error string
217 if(rect.width==0 && rect.height==0 && rect.x==0 && rect.y==0) return retval;
218 sprintf(errstr, "Requested slice [ %d:%d %d:%d ] oversteps array sized [ %d %d ]",
219 rect.x, rect.y, rect.x+rect.width, rect.y+rect.height, w, h);
220 PyErr_SetString(PyExc_IndexError, errstr);
221 //PyErr_SetString(PyExc_ValueError, errstr);
227 // slice access and assignment for CvMat
238 void __setitem__(PyObject * object, double val){
240 CvRect subrect = PySlice_to_CvRect( self, object );
241 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
242 cvGetSubRect(self, &tmp, subrect);
243 cvSet(&tmp, cvScalarAll(val));
245 void __setitem__(PyObject * object, CvPoint val){
247 CvRect subrect = PySlice_to_CvRect( self, object );
248 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
249 cvGetSubRect(self, &tmp, subrect);
250 cvSet(&tmp, cvScalar(val.x, val.y));
252 void __setitem__(PyObject * object, CvPoint2D32f val){
254 CvRect subrect = PySlice_to_CvRect( self, object );
255 cvGetSubRect(self, &tmp, subrect);
256 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
257 cvSet(&tmp, cvScalar(val.x, val.y));
259 void __setitem__(PyObject * object, CvScalar val){
261 CvRect subrect = PySlice_to_CvRect( self, object );
262 cvGetSubRect(self, &tmp, subrect);
263 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
267 // array slice assignment
268 void __setitem__(PyObject * object, CvArr * arr){
269 CvMat tmp, src_stub, *src;
270 CvRect subrect = PySlice_to_CvRect( self, object );
271 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
272 cvGetSubRect(self, &tmp, subrect);
274 // Reshape source array to fit destination
275 // This will be used a lot for small arrays b/c
276 // PyObject_to_CvArr tries to compress a 2-D python
277 // array with 1-4 columns into a multichannel vector
278 src=cvReshape(arr, &src_stub, CV_MAT_CN(tmp.type), tmp.rows);
280 cvConvert(src, &tmp);
284 PyObject * __getitem__(PyObject * object){
286 CvRect subrect = PySlice_to_CvRect( self, object );
287 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, NULL );
288 if(subrect.width==1 && subrect.height==1){
290 int type = cvGetElemType( self );
291 if(CV_MAT_CN(type) > 1){
293 *s = cvGet2D( self, subrect.y, subrect.x );
294 return SWIG_NewPointerObj( s, $descriptor(CvScalar *), 1 );
296 switch(CV_MAT_DEPTH(type)){
298 return PyLong_FromUnsignedLong( CV_MAT_ELEM(*self, uchar, subrect.y, subrect.x ) );
300 return PyLong_FromLong( CV_MAT_ELEM(*self, char, subrect.y, subrect.x ) );
302 return PyLong_FromUnsignedLong( CV_MAT_ELEM(*self, ushort, subrect.y, subrect.x ) );
304 return PyLong_FromLong( CV_MAT_ELEM(*self, short, subrect.y, subrect.x ) );
306 return PyLong_FromLong( CV_MAT_ELEM(*self, int, subrect.y, subrect.x ) );
308 return PyFloat_FromDouble( CV_MAT_ELEM(*self, float, subrect.y, subrect.x) );
310 return PyFloat_FromDouble( CV_MAT_ELEM(*self, double, subrect.y, subrect.x) );
313 mat = (CvMat *) cvAlloc(sizeof(CvMat));
314 cvGetSubRect(self, mat, subrect);
316 // cvGetSubRect doesn't do this since it assumes mat lives on the stack
317 mat->hdr_refcount = self->hdr_refcount;
318 mat->refcount = self->refcount;
321 return SWIG_NewPointerObj( mat, $descriptor(CvMat *), 1 );
324 // ~ operator -- swig doesn't generate this from the C++ equivalent
325 CvMat * __invert__(){
326 CvMat * res = cvCreateMat(self->rows, self->cols, self->type);
334 generator function iterating through rows in matrix or elements in vector
337 return self.colrange()
338 return self.rowrange()
342 generator function iterating along rows in matrix
344 for i in range(self.rows):
349 generator function iterating along columns in matrix
351 for i in range(self.cols):
354 # if arg is None, python still calls our operator overloads
358 # to do the right thing -- so redefine __ne__ and __eq__
360 def __eq__(self, arg):
363 __eq__(self, CvArr src)
364 __eq__(self, double val)
369 return _cv.CvMat___eq__(self, arg)
370 def __ne__(self, arg):
373 __ne__(self, CvArr src)
374 __ne__(self, double val)
379 return _cv.CvMat___ne__(self, arg)
385 // slice access and assignment for IplImage
396 void __setitem__(PyObject * object, double val){
398 CvRect subrect = PySlice_to_CvRect( self, object );
399 cvGetSubRect(self, &tmp, subrect);
400 cvSet(&tmp, cvScalarAll(val));
402 void __setitem__(PyObject * object, CvPoint val){
404 CvRect subrect = PySlice_to_CvRect( self, object );
405 cvGetSubRect(self, &tmp, subrect);
406 cvSet(&tmp, cvScalar(val.x, val.y));
408 void __setitem__(PyObject * object, CvPoint2D32f val){
410 CvRect subrect = PySlice_to_CvRect( self, object );
411 cvGetSubRect(self, &tmp, subrect);
412 cvSet(&tmp, cvScalar(val.x, val.y));
414 void __setitem__(PyObject * object, CvScalar val){
416 CvRect subrect = PySlice_to_CvRect( self, object );
417 cvGetSubRect(self, &tmp, subrect);
421 // array slice assignment
422 void __setitem__(PyObject * object, CvArr * arr){
424 CvRect subrect = PySlice_to_CvRect( self, object );
425 cvGetSubRect(self, &tmp, subrect);
426 cvConvert(arr, &tmp);
430 PyObject * __getitem__(PyObject * object){
433 CvRect subrect = PySlice_to_CvRect( self, object );
435 // return scalar if single element
436 if(subrect.width==1 && subrect.height==1){
438 int type = cvGetElemType( self );
439 if(CV_MAT_CN(type) > 1){
441 *s = cvGet2D( self, subrect.y, subrect.x );
442 return SWIG_NewPointerObj( s, $descriptor(CvScalar *), 1 );
444 switch(CV_MAT_DEPTH(type)){
446 return PyLong_FromUnsignedLong( CV_IMAGE_ELEM(self, uchar, subrect.y, subrect.x ) );
448 return PyLong_FromLong( CV_IMAGE_ELEM(self, char, subrect.y, subrect.x ) );
450 return PyLong_FromUnsignedLong( CV_IMAGE_ELEM(self, ushort, subrect.y, subrect.x ) );
452 return PyLong_FromLong( CV_IMAGE_ELEM(self, short, subrect.y, subrect.x ) );
454 return PyLong_FromLong( CV_IMAGE_ELEM(self, int, subrect.y, subrect.x ) );
456 return PyFloat_FromDouble( CV_IMAGE_ELEM(self, float, subrect.y, subrect.x) );
458 return PyFloat_FromDouble( CV_IMAGE_ELEM(self, double, subrect.y, subrect.x) );
462 // otherwise return array
463 im = (IplImage *) cvAlloc(sizeof(IplImage));
464 cvGetSubRect(self, &mat, subrect);
465 im = cvGetImage(&mat, im);
466 return SWIG_NewPointerObj( im, $descriptor(_IplImage *), 1 );