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 octave 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, octfunc, argtype, cvfunc, newobjcall)
52 %newobject cname::octfunc(argtype arg);
54 rettype octfunc(argtype arg){
55 rettype retarg = newobjcall;
62 // Macro to define octave 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(octfunc, argtype, cvfunc)
65 %wrap_cvGeneric_CvArr(CvMat, CvMat *, octfunc, argtype, cvfunc,
66 cvCreateMat(self->rows, self->cols, self->type));
67 %wrap_cvGeneric_CvArr(IplImage, IplImage *, octfunc, argtype, cvfunc,
68 cvCreateImage(cvGetSize(self), self->depth, self->nChannels));
71 // Macro to define octave function of the form A = A.f(c)
72 // where f modifies A inplace
74 %define %wrap_cvGeneric_InPlace(cname, rettype, octfunc, argtype, cvfunc)
75 %wrap_cvGeneric_CvArr(cname, rettype, octfunc, 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(octfunc, cvfunc)
84 %wrap_cvArr_binaryop(octfunc, CvArr *, cvfunc(self, arg, retarg));
87 // map any OpenCV function of form cvFunc(src1, value, dst)
88 %define %wrap_cvArithS(octfunc, cvfuncS)
89 %wrap_cvArr_binaryop(octfunc, CvScalar, cvfuncS(self, arg, retarg));
90 %wrap_cvArr_binaryop(octfunc, double, cvfuncS(self, cvScalar(arg), retarg));
93 // same as wrap_cvArith
94 %define %wrap_cvLogic(octfunc, cvfunc)
95 %wrap_cvArr_binaryop(octfunc, CvArr *, cvfunc(self, arg, retarg))
98 // same as wrap_cvArithS
99 %define %wrap_cvLogicS(octfunc, cvfuncS)
100 %wrap_cvArr_binaryop(octfunc, CvScalar, cvfuncS(self, arg, retarg));
101 %wrap_cvArr_binaryop(octfunc, double, cvfuncS(self, cvScalar(arg), retarg));
104 // Macro to map logical operations to cvCmp
105 %define %wrap_cvCmp(octfunc, cmp_op)
106 %wrap_cvGeneric_CvArr(CvMat, CvArr *, octfunc, CvMat *,
107 cvCmp(self, arg, retarg, cmp_op),
108 cvCreateMat(self->rows, self->cols, CV_8U));
109 %wrap_cvGeneric_CvArr(IplImage, CvArr *, octfunc, IplImage *,
110 cvCmp(self, arg, retarg, cmp_op),
111 cvCreateImage(cvGetSize(self), 8, 1));
114 %define %wrap_cvCmpS(octfunc, cmp_op)
115 %wrap_cvGeneric_CvArr(CvMat, CvArr *, octfunc, double,
116 cvCmpS(self, arg, retarg, cmp_op),
117 cvCreateMat(self->rows, self->cols, CV_8U));
118 %wrap_cvGeneric_CvArr(IplImage, CvArr *, octfunc, double,
119 cvCmpS(self, arg, retarg, cmp_op),
120 cvCreateImage(cvGetSize(self), 8, 1));
123 // special case for cvScale, /, *
124 %define %wrap_cvScale(octfunc, scale)
125 %wrap_cvGeneric_CvArr(CvMat, CvArr *, octfunc, double,
126 cvScale(self, retarg, scale),
127 cvCreateMat(self->rows, self->cols, self->type));
128 %wrap_cvGeneric_CvArr(IplImage, CvArr *, octfunc, 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 octave
165 %wrap_cvArr_binaryop(__pow__, double, cvPow(self, retarg, arg))
167 // TODO -- other Octave operators listed in SWIG/Octave docs (www.swig.org)
172 // __repr__ -- full string representation
173 // __str__ -- compact representation
175 // __len__ -- number of rows? or elements?
177 // __contains__ -- cvCmpS, cvMax ?
184 // Called to implement the unary arithmetic operations (-, +, abs() and ~).
190 // 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.
191 // __complex__( self )
196 /*M//////////////////////////////////////////////////////////////////////////////////////////
197 // Slice access and assignment for CvArr types
198 //////////////////////////////////////////////////////////////////////////////////////////M*/
202 %newobject CvMat::__paren(octave_value object);
203 %newobject _IplImage::__paren(octave_value object);
205 // Macro to check bounds of slice and throw error if outside
206 %define CHECK_SLICE_BOUNDS(rect,w,h,retval)
207 //printf("__setitem__ slice(%d:%d, %d:%d) array(%d,%d)", rect.x, rect.y, rect.x+rect.width, rect.y+rect.height, w, h);
208 if(rect.width<=0 || rect.height<=0 ||
209 rect.width>w || rect.height>h ||
210 rect.x<0 || rect.y<0 ||
211 rect.x>= w || rect.y >=h){
213 // previous function already set error string
214 if(rect.width==0 && rect.height==0 && rect.x==0 && rect.y==0) return retval;
215 sprintf(errstr, "Requested slice [ %d:%d %d:%d ] oversteps array sized [ %d %d ]",
216 rect.x, rect.y, rect.x+rect.width, rect.y+rect.height, w, h);
223 // slice access and assignment for CvMat
235 void __paren_asgn(octave_value object, double val){
237 CvRect subrect = OctSlice_to_CvRect( self, object );
238 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
239 cvGetSubRect(self, &tmp, subrect);
240 cvSet(&tmp, cvScalarAll(val));
242 void __paren_asgn(octave_value object, CvPoint val){
244 CvRect subrect = OctSlice_to_CvRect( self, object );
245 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
246 cvGetSubRect(self, &tmp, subrect);
247 cvSet(&tmp, cvScalar(val.x, val.y));
249 void __paren_asgn(octave_value object, CvPoint2D32f val){
251 CvRect subrect = OctSlice_to_CvRect( self, object );
252 cvGetSubRect(self, &tmp, subrect);
253 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
254 cvSet(&tmp, cvScalar(val.x, val.y));
256 void __paren_asgn(octave_value object, CvScalar val){
258 CvRect subrect = OctSlice_to_CvRect( self, object );
259 cvGetSubRect(self, &tmp, subrect);
260 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
264 // array slice assignment
265 void __paren_asgn(octave_value object, CvArr * arr){
266 CvMat tmp, src_stub, *src;
267 CvRect subrect = OctSlice_to_CvRect( self, object );
268 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, );
269 cvGetSubRect(self, &tmp, subrect);
271 // Reshape source array to fit destination
272 // This will be used a lot for small arrays b/c
273 // OctObject_to_CvArr tries to compress a 2-D octave
274 // array with 1-4 columns into a multichannel vector
275 src=cvReshape(arr, &src_stub, CV_MAT_CN(tmp.type), tmp.rows);
277 cvConvert(src, &tmp);
281 octave_value __paren(octave_value object){
283 CvRect subrect = OctSlice_to_CvRect( self, object );
284 CHECK_SLICE_BOUNDS( subrect, self->cols, self->rows, octave_value() );
285 if(subrect.width==1 && subrect.height==1){
287 int type = cvGetElemType( self );
288 if(CV_MAT_CN(type) > 1){
290 *s = cvGet2D( self, subrect.y, subrect.x );
291 return SWIG_NewPointerObj( s, $descriptor(CvScalar *), 1 );
293 switch(CV_MAT_DEPTH(type)){
295 return OctLong_FromUnsignedLong( CV_MAT_ELEM(*self, uchar, subrect.y, subrect.x ) );
297 return OctLong_FromLong( CV_MAT_ELEM(*self, char, subrect.y, subrect.x ) );
299 return OctLong_FromUnsignedLong( CV_MAT_ELEM(*self, ushort, subrect.y, subrect.x ) );
301 return OctLong_FromLong( CV_MAT_ELEM(*self, short, subrect.y, subrect.x ) );
303 return OctLong_FromLong( CV_MAT_ELEM(*self, int, subrect.y, subrect.x ) );
305 return OctFloat_FromDouble( CV_MAT_ELEM(*self, float, subrect.y, subrect.x) );
307 return OctFloat_FromDouble( CV_MAT_ELEM(*self, double, subrect.y, subrect.x) );
310 mat = (CvMat *) cvAlloc(sizeof(CvMat));
311 cvGetSubRect(self, mat, subrect);
313 // cvGetSubRect doesn't do this since it assumes mat lives on the stack
314 mat->hdr_refcount = self->hdr_refcount;
315 mat->refcount = self->refcount;
318 return SWIG_NewPointerObj( mat, $descriptor(CvMat *), 1 );
325 generator function iterating through rows in matrix or elements in vector
328 return self.colrange()
329 return self.rowrange()
333 generator function iterating along rows in matrix
335 for i in range(self.rows):
340 generator function iterating along columns in matrix
342 for i in range(self.cols):
345 # if arg is None, octave still calls our operator overloads
349 # to do the right thing -- so redefine __ne__ and __eq__
351 def __eq__(self, arg):
354 __eq__(self, CvArr src)
355 __eq__(self, double val)
360 return _cv.CvMat___eq__(self, arg)
361 def __ne__(self, arg):
364 __ne__(self, CvArr src)
365 __ne__(self, double val)
370 return _cv.CvMat___ne__(self, arg)
376 // slice access and assignment for IplImage
388 void __paren_asgn(octave_value object, double val){
390 CvRect subrect = OctSlice_to_CvRect( self, object );
391 cvGetSubRect(self, &tmp, subrect);
392 cvSet(&tmp, cvScalarAll(val));
394 void __paren_asgn(octave_value object, CvPoint val){
396 CvRect subrect = OctSlice_to_CvRect( self, object );
397 cvGetSubRect(self, &tmp, subrect);
398 cvSet(&tmp, cvScalar(val.x, val.y));
400 void __paren_asgn(octave_value object, CvPoint2D32f val){
402 CvRect subrect = OctSlice_to_CvRect( self, object );
403 cvGetSubRect(self, &tmp, subrect);
404 cvSet(&tmp, cvScalar(val.x, val.y));
406 void __paren_asgn(octave_value object, CvScalar val){
408 CvRect subrect = OctSlice_to_CvRect( self, object );
409 cvGetSubRect(self, &tmp, subrect);
413 // array slice assignment
414 void __paren_asgn(octave_value object, CvArr * arr){
416 CvRect subrect = OctSlice_to_CvRect( self, object );
417 cvGetSubRect(self, &tmp, subrect);
418 cvConvert(arr, &tmp);
422 octave_value __paren(octave_value object){
425 CvRect subrect = OctSlice_to_CvRect( self, object );
427 // return scalar if single element
428 if(subrect.width==1 && subrect.height==1){
430 int type = cvGetElemType( self );
431 if(CV_MAT_CN(type) > 1){
433 *s = cvGet2D( self, subrect.y, subrect.x );
434 return SWIG_NewPointerObj( s, $descriptor(CvScalar *), 1 );
436 switch(CV_MAT_DEPTH(type)){
438 return OctLong_FromUnsignedLong( CV_IMAGE_ELEM(self, uchar, subrect.y, subrect.x ) );
440 return OctLong_FromLong( CV_IMAGE_ELEM(self, char, subrect.y, subrect.x ) );
442 return OctLong_FromUnsignedLong( CV_IMAGE_ELEM(self, ushort, subrect.y, subrect.x ) );
444 return OctLong_FromLong( CV_IMAGE_ELEM(self, short, subrect.y, subrect.x ) );
446 return OctLong_FromLong( CV_IMAGE_ELEM(self, int, subrect.y, subrect.x ) );
448 return OctFloat_FromDouble( CV_IMAGE_ELEM(self, float, subrect.y, subrect.x) );
450 return OctFloat_FromDouble( CV_IMAGE_ELEM(self, double, subrect.y, subrect.x) );
454 // otherwise return array
455 im = (IplImage *) cvAlloc(sizeof(IplImage));
456 cvGetSubRect(self, &mat, subrect);
457 im = cvGetImage(&mat, im);
458 return SWIG_NewPointerObj( im, $descriptor(_IplImage *), 1 );