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.
41 %include "exception.i"
42 %include "./pyhelpers.i"
44 %typemap(in) (CvArr *) (bool freearg=false)
46 $1 = PyObject_to_CvArr($input, &freearg);
49 %typemap(freearg) (CvArr *)
51 if($1!=NULL && freearg$argnum)
58 %typemap(in) CvMat* (bool freearg=false), const CvMat* (bool freearg=false)
60 $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
63 %typemap(freearg) CvMat*,const CvMat* {
64 if($1!=NULL && freearg$argnum){
70 /* typecheck typemaps */
71 %typecheck(SWIG_TYPECHECK_POINTER) CvArr * {
72 $1 = CvArr_Check( $input );
75 %typecheck(SWIG_TYPECHECK_POINTER) CvScalar {
76 $1 = CvScalar_Check( $input );
79 /* copy built-in swig typemaps for these types */
80 %typemap(typecheck) CvPoint = SWIGTYPE;
81 %typemap(typecheck) CvPoint2D32f = SWIGTYPE;
82 %typemap(typecheck) CvPoint3D32f = SWIGTYPE;
83 %typemap(typecheck) CvPoint2D64f = SWIGTYPE;
84 %typemap(typecheck) CvPoint3D64f = SWIGTYPE;
85 %typemap(typecheck) CvRect = SWIGTYPE;
86 %typemap(typecheck) CvSize = SWIGTYPE;
87 %typemap(typecheck) CvSize2D32f = SWIGTYPE;
88 %typemap(typecheck) CvSlice = SWIGTYPE;
89 %typemap(typecheck) CvBox2D = SWIGTYPE;
90 %typemap(typecheck) CvTermCriteria = SWIGTYPE;
93 // for cvReshape, cvGetRow, where header is passed, then filled in
94 %typemap(in, numinputs=0) CvMat * OUTPUT (CvMat * header, bool freearg=false) {
95 header = (CvMat *)cvAlloc(sizeof(CvMat));
99 %apply CvMat * OUTPUT {CvMat * header};
100 %apply CvMat * OUTPUT {CvMat * submat};
102 %newobject cvReshape;
104 %newobject cvGetRows;
106 %newobject cvGetCols;
107 %newobject cvGetSubRect;
108 %newobject cvGetDiag;
111 * In C, these functions assume input will always be around at least as long as header,
112 * presumably because the most common usage is to pass in a reference to a stack object.
115 * cvGetRow(A, &row, 0);
117 * As a result, the header is not refcounted (see the C source for cvGetRow, Reshape, in cxarray.cpp)
118 * However, in python, the header parameter is implicitly created so it is easier to create
119 * situations where the sub-array outlives the original header. A simple example is:
120 * A = cvReshape(A, -1, A.rows*A.cols)
122 * since python doesn't have an assignment operator, the new header simply replaces the original,
123 * the refcount of the original goes to zero, and cvReleaseMat is called on the original, freeing both
124 * the header and data. The new header is left pointing to invalid data. To avoid this, need to add
125 * refcount field to the returned header.
127 %typemap(argout) (const CvArr* arr, CvMat* header)
129 $2->hdr_refcount = ((CvMat *)$1)->hdr_refcount;
130 $2->refcount = ((CvMat *)$1)->refcount;
134 %typemap(argout) (const CvArr* arr, CvMat* submat)
136 $2->hdr_refcount = ((CvMat *)$1)->hdr_refcount;
137 $2->refcount = ((CvMat *)$1)->refcount;
141 /* map scalar or sequence to CvScalar, CvPoint2D32f, CvPoint */
142 %typemap(in) (CvScalar)
144 $1 = PyObject_to_CvScalar( $input );
147 //%typemap(in) (CvPoint) {
148 // $1 = PyObject_to_CvPoint($input);
150 //%typemap(in) (CvPoint2D32f) {
151 // $1 = PyObject_to_CvPoint2D32f($input);
155 // ============================================================================================
157 %define TUPLE_OR_TYPE (item, destination, typename, number, description, ...)
159 if (PySequence_Check(item) && PySequence_Length(item) == number)
161 PyObject * as_tuple = PySequence_Tuple (item);
162 if (!PyArg_ParseTuple (as_tuple, __VA_ARGS__))
164 PyErr_SetString(PyExc_TypeError, "each entry must consist of " # number " values " # description);
165 Py_DECREF (as_tuple);
168 Py_DECREF (as_tuple);
173 if (SWIG_ConvertPtr (item, (void **) & ptr, $descriptor(typename *), SWIG_POINTER_EXCEPTION) == -1)
175 PyErr_SetString (PyExc_TypeError, "expected a sequence of " # number " values " # description " or a " # typename);
184 %define INPUT_ARRAY_OF_TUPLES_OR_TYPES (typename, number, description, ...)
186 if(! PySequence_Check ($input))
188 PyErr_SetString(PyExc_TypeError, "Expected a list or tuple");
192 // TODO: will this ever be freed?
193 int count = PySequence_Size ($input);
194 int array = (typename *) malloc (count * sizeof (typename));
196 // extract all the points values from the list */
197 typename * element = array;
198 for (int i = 0; i < count; i++, element++)
200 PyObject * item = PySequence_GetItem ($input, i);
202 // use the macro we have to expand a single entry
203 TUPLE_OR_TYPE (item, *element, typename, number, description, __VA_ARGS__)
204 // *corner, "ff", & corner->x, & corner->y
207 // these are the arguments passed to the OpenCV function
214 // ============================================================================================
215 // Tiny typemaps for tiny types ...
217 %typemap(in) CvRect (CvRect temp)
218 //TUPLE_OR_TYPE ($input, $1, CvRect, 4, "(x,y,w,h)", "iiii", & temp.x, & temp.y, & temp.width, & temp.height)
220 if (PyTuple_Check($input))
222 if (!PyArg_ParseTuple($input,"iiii", & temp.x, & temp.y, & temp.width, & temp.height))
224 PyErr_SetString(PyExc_TypeError,"tuple must consist of 4 integers (x, y, width, height)");
232 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvRect, SWIG_POINTER_EXCEPTION) == -1)
234 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvRect");
242 %typemap(in) CvTermCriteria (CvTermCriteria temp)
244 if (PyTuple_Check($input))
246 if (!PyArg_ParseTuple($input,"iid", & temp.type, & temp.max_iter, & temp.epsilon))
248 PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 integers and a float (type, max_iter, epsilon)");
255 CvTermCriteria * ptr;
256 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvTermCriteria, SWIG_POINTER_EXCEPTION) == -1)
258 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvTermCriteria");
266 %typemap(in) CvPoint (CvPoint temp)
268 if (PyTuple_Check($input))
270 if (!PyArg_ParseTuple($input,"ii", & temp.x, & temp.y))
272 PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 integers (x, y)");
280 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint, SWIG_POINTER_EXCEPTION) == -1)
282 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint");
290 %typemap(in) CvPoint2D32f (CvPoint2D32f temp)
292 if (PyTuple_Check($input))
294 if (!PyArg_ParseTuple($input,"ff", & temp.x, & temp.y))
296 PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 floats (x, y)");
304 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint2D32f, SWIG_POINTER_EXCEPTION) == -1)
306 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint2D32f");
314 %typemap(in) CvPoint3D32f (CvPoint3D32f temp)
316 if (PyTuple_Check($input))
318 if (!PyArg_ParseTuple($input,"fff", & temp.x, & temp.y, &temp.z))
320 PyErr_SetString(PyExc_TypeError,"tuple must consist of 3 floats (x, y, z)");
328 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint3D32f, SWIG_POINTER_EXCEPTION) == -1)
330 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint3D32f");
338 %typemap(in) CvPoint2D64f (CvPoint2D64f temp)
340 if (PyTuple_Check($input))
342 if (!PyArg_ParseTuple($input,"dd", & temp.x, & temp.y))
344 PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 floats (x, y)");
352 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint2D64f, SWIG_POINTER_EXCEPTION) == -1)
354 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint2D64f");
362 %typemap(in) CvPoint3D64f (CvPoint3D64f temp)
364 if (PyTuple_Check($input))
366 if (!PyArg_ParseTuple($input,"ddd", & temp.x, & temp.y, &temp.z))
368 PyErr_SetString(PyExc_TypeError,"tuple must consist of 3 floats (x, y, z)");
376 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvPoint3D64f, SWIG_POINTER_EXCEPTION) == -1)
378 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvPoint3D64f");
386 %typemap(in) CvSize (CvSize temp)
388 if (PyTuple_Check($input))
390 if (!PyArg_ParseTuple($input,"ii", & temp.width, & temp.height))
392 PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 integers (width, height)");
400 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvSize, SWIG_POINTER_EXCEPTION) == -1)
402 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvSize");
410 %typemap(in) CvSize2D32f (CvSize2D32f temp)
412 if (PyTuple_Check($input))
414 if (!PyArg_ParseTuple($input,"ff", & temp.width, & temp.height))
416 PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 floats (width, height)");
424 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvSize2D32f, SWIG_POINTER_EXCEPTION) == -1)
426 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvSize2D32f");
434 %typemap(in) CvBox2D (CvBox2D temp)
436 if (PyTuple_Check($input))
438 if (!PyArg_ParseTuple($input,"fffff", & temp.center.x, & temp.center.y, & temp.size.width, & temp.size.height, & temp.angle))
440 PyErr_SetString(PyExc_TypeError,"tuple must consist of 5 floats (center_x, center_y, width, height, angle)");
448 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvBox2D, SWIG_POINTER_EXCEPTION) == -1)
450 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvBox2D");
458 %typemap(in) CvSlice (CvSlice temp)
460 if (PyTuple_Check($input))
462 if (!PyArg_ParseTuple($input,"ii", & temp.start_index, & temp.end_index))
464 PyErr_SetString(PyExc_TypeError,"tuple must consist of 2 integers (start_index, end_index)");
472 if (SWIG_ConvertPtr ($input, (void **) & ptr, SWIGTYPE_p_CvSlice, SWIG_POINTER_EXCEPTION) == -1)
474 PyErr_SetString (PyExc_TypeError,"expected a tuple or a CvSlice");
482 /* typemap for cvGetDims */
483 %typemap(in) (const CvArr * arr, int * sizes = NULL) (void * myarr, int mysizes[CV_MAX_DIM]){
484 SWIG_Python_ConvertPtr($input, &myarr, 0, SWIG_POINTER_EXCEPTION);
489 %typemap(argout) (const CvArr * arr, int * sizes = NULL) {
490 int len = PyInt_AsLong( $result );
491 PyObject * obj = PyTuple_FromIntArray( $2, len );
492 Py_DECREF( $result );
496 /* map one list of integer to the two parameters dimension/sizes */
497 %typemap(in) (int dims, int* sizes) {
500 /* get the size of the dimention array */
501 $1 = PyList_Size ($input);
503 /* allocate the needed memory */
504 $2 = (int *)malloc ($1 * sizeof (int));
506 /* extract all the integer values from the list */
507 for (i = 0; i < $1; i++) {
508 PyObject *item = PyList_GetItem ($input, i);
509 $2 [i] = (int)PyInt_AsLong (item);
513 /* map one list of integer to the parameter idx of
514 cvGetND, cvSetND, cvClearND, cvGetRealND, cvSetRealND and cvClearRealND */
515 %typemap(in) (int* idx) {
519 /* get the size of the dimention array */
520 size = PyList_Size ($input);
522 /* allocate the needed memory */
523 $1 = (int *)malloc (size * sizeof (int));
525 /* extract all the integer values from the list */
526 for (i = 0; i < size; i++) {
527 PyObject *item = PyList_GetItem ($input, i);
528 $1 [i] = (int)PyInt_AsLong (item);
532 /* map a list of list of float to an matrix of floats*/
533 %typemap(in) float** ranges {
539 /* get the number of lines of the matrix */
540 size1 = PyList_Size ($input);
542 /* allocate the correct number of lines for the destination matrix */
543 $1 = (float **)malloc (size1 * sizeof (float *));
545 for (i1 = 0; i1 < size1; i1++) {
547 /* extract all the lines of the matrix */
548 PyObject *list = PyList_GetItem ($input, i1);
551 /* size 2 wasn't computed before */
552 size2 = PyList_Size (list);
553 } else if (size2 != PyList_Size (list)) {
554 /* the current line as a different size than the previous one */
555 /* so, generate an exception */
556 SWIG_exception (SWIG_ValueError, "Lines must be the same size");
559 /* allocate the correct number of rows for the current line */
560 $1 [i1] = (float *)malloc (size2 * sizeof (float));
562 /* extract all the float values of this row */
563 for (i2 = 0; i2 < size2; i2++) {
564 PyObject *item = PyList_GetItem (list, i2);
565 $1 [i1][i2] = (float)PyFloat_AsDouble (item);
571 * map the output parameter of the cvGetMinMaxHistValue()
572 * so, we can call cvGetMinMaxHistValue() in Python like:
573 * min_value, max_value = cvGetMinMaxHistValue (hist, None, None)
575 %apply int *OUTPUT {int *min_idx};
576 %apply int *OUTPUT {int *max_idx};
577 %apply float *OUTPUT {float *min_value};
578 %apply float *OUTPUT {float *max_value};
581 * map output parameters of cvMinMaxLoc
583 %apply double *OUTPUT {double* min_val};
584 %apply double *OUTPUT {double* max_val};
586 %typemap(in, numinputs=0) CvPoint * OUTPUT {
587 $1 = (CvPoint *) malloc(sizeof(CvPoint));
590 %typemap(argout) CvPoint * OUTPUT {
591 PyObject * to_add = SWIG_NewPointerObj ($1, $descriptor(CvPoint *), SWIG_POINTER_OWN);
592 $result = SWIG_AppendOutput( $result, to_add );
595 %apply CvPoint *OUTPUT {CvPoint *min_loc};
596 %apply CvPoint *OUTPUT {CvPoint *max_loc};
599 * the input argument of cvPolyLine "CvPoint** pts" is converted from
600 * a "list of list" (aka. an array) of CvPoint().
601 * The next parameters "int* npts" and "int contours" are computed from
604 %typemap(in) (CvPoint** pts, int* npts, int contours){
608 CvPoint **points = NULL;
609 int *nb_vertex = NULL;
611 if(!PySequence_Check($input)){
612 SWIG_exception(SWIG_TypeError, "Expected a list for argument $argnum\n");
616 /* get the number of polylines input array */
617 int size1 = PySequence_Size ($input);
621 /* create the points array */
622 points = (CvPoint **)malloc (size1 * sizeof (CvPoint *));
624 /* point to the created array for passing info to the C function */
627 /* create the array for memorizing the vertex */
628 nb_vertex = (int *)malloc (size1 * sizeof (int));
631 for (i = 0; i < size1; i++) {
633 /* get the current item */
634 PyObject *line = PySequence_GetItem ($input, i);
636 if(!PySequence_Check(line)){
637 SWIG_exception(SWIG_TypeError, "Expected a sequence of sequences of integers for argument $argnum\n");
638 // TODO: cleanup here
641 /* get the size of the current polyline */
642 size2 = PySequence_Size (line);
646 /* allocate the necessary memory to store the points */
647 points [i] = (CvPoint *)malloc (size2 * sizeof (CvPoint));
650 /* memorize the size of the polyline in the vertex list */
651 nb_vertex [i] = size2;
653 for (j = 0; j < size2; j++) {
654 /* get the current item */
655 PyObject *item = PySequence_GetItem (line, j);
656 points[i][j] = PyObject_to_CvPoint( item );
660 /** Free arguments allocated before the function call */
661 %typemap(freearg) (CvPoint **pts, int* npts, int contours){
671 /** this typemap is meant to help cvCalcOpticalFlowPyrLK */
672 %typemap(in, numinputs = 0) (int count, char* status, float* track_error) {
673 $1 [count] = (char *) malloc (count * sizeof (char));
674 $2 [count] = (float *) malloc (count * sizeof (float));
677 %typemap(argout) float *track_error {
678 PyObject * to_add = SWIG_NewPointerObj ($1, $descriptor(float *), SWIG_POINTER_OWN);
679 $result = SWIG_AppendOutput( $result, to_add );
682 /** Macro to define typemaps to convert a python list of CvPoints to a C array of CvPoints */
683 %define %typemap_CvPoint_CArr(points_arg, numpoints_arg)
685 %typemap(in, numinputs=1) (CvPoint * points_arg, int numpoints_arg){
688 if(!PySequence_Check($input)){
689 SWIG_exception(SWIG_TypeError, "Expected a list for argument $argnum\n");
692 int size = PySequence_Size($input);
693 CvPoint * points = (CvPoint *)malloc(size*sizeof(CvPoint));
694 for(i=0; i<size; i++){
695 PyObject *item = PySequence_GetItem($input, i);
696 points[i] = PyObject_to_CvPoint( item );
701 %typemap(freearg) (CvPoint *points_arg, int numpoints_arg){
706 /* apply to cvFillConvexPoly */
707 %typemap_CvPoint_CArr(pts, npts)
710 * this is mainly an "output parameter"
711 * So, just allocate the memory as input
713 %typemap (in, numinputs=0) (CvSeq ** OUTPUT) (CvSeq * seq) {
718 * return the contours with all the others parametres
720 %typemap(argout) (CvSeq ** OUTPUT) {
723 /* extract the pointer we want to add to the returned tuple */
724 /* sequence is allocated in CvMemStorage, so python_ownership=0 */
725 to_add = SWIG_NewPointerObj (*$1, $descriptor(CvSeq*), 0);
727 $result = SWIG_AppendResult($result, &to_add, 1);
729 %apply CvSeq **OUTPUT {CvSeq **first_contour};
730 %apply CvSeq **OUTPUT {CvSeq **comp};
733 * CvArr ** image can be either one CvArr or one array of CvArr
734 * (for example like in cvCalcHist() )
735 * From Python, the array of CvArr can be a tuple.
737 %typemap(in) (CvArr ** INPUT) (
738 CvArr * one_image=NULL,
739 bool free_one_arg=false,
740 CvArr ** many_images=NULL,
741 bool *free_many_args=NULL,
744 /* first, check if this is a tuple */
745 if PyTuple_Check ($input) {
746 /* This is a tuple, so we need to test each element and pass
747 them to the called function */
751 /* get the size of the tuple */
752 nimages = PyTuple_Size ($input);
754 /* allocate the necessary place */
755 many_images = (CvArr **)malloc (nimages * sizeof (CvArr *));
756 free_many_args = (bool *)malloc(nimages * sizeof(bool));
758 for (i = 0; i < nimages; i++) {
760 /* convert the current tuple element to a CvArr *, and
761 store to many_images [i] */
762 many_images[i] = PyObject_to_CvArr (PyTuple_GetItem ($input, i),
765 /* check that the current item is a correct type */
766 if(!many_images[i]) {
772 /* what to give to the called function */
775 } else if((one_image = PyObject_to_CvArr( $input, &free_one_arg ))){
777 /* this is just one CvArr *, so one_image will receive it */
781 /* not a CvArr *, not a tuple, this is wrong */
785 %apply CvArr ** INPUT {CvArr ** img};
786 %apply CvArr ** INPUT {CvArr ** image};
787 %apply CvArr ** INPUT {CvArr ** arr};
788 %apply CvArr ** INPUT {CvArr ** vects};
790 %typemap(freearg) (CvArr ** FREEARG) {
791 if(free_one_arg$argnum){
792 cvFree(&(one_image$argnum));
794 else if(free_many_args$argnum){
796 for (i=0; i<nimages$argnum; i++){
797 if(free_many_args$argnum[i]){
798 cvReleaseData(many_images$argnum[i]);
799 cvFree(many_images$argnum+i);
802 free(many_images$argnum);
803 free(free_many_args$argnum);
807 %apply CvArr ** FREEARG {CvArr ** img};
808 %apply CvArr ** FREEARG {CvArr ** image};
809 %apply CvArr ** FREEARG {CvArr ** arr};
810 %apply CvArr ** FREEARG {CvArr ** vects};
813 * Map the CvFont * parameter from the cvInitFont() as an output parameter
815 %typemap (in, numinputs=1) (CvFont* font, int font_face) {
816 $1 = (CvFont *)malloc (sizeof (CvFont));
817 $2 = (int)PyInt_AsLong ($input);
818 if (SWIG_arg_fail($argnum)) SWIG_fail;
820 %typemap(argout) (CvFont* font, int font_face) {
823 /* extract the pointer we want to add to the returned tuple */
824 to_add = SWIG_NewPointerObj ($1, $descriptor(CvFont *), 0);
826 $result = SWIG_AppendResult($result, &to_add, 1);
830 * these are output parameters for cvGetTextSize
832 %typemap (in, numinputs=0) (CvSize* text_size, int* baseline) {
833 CvSize *size = (CvSize *)malloc (sizeof (CvSize));
834 int *baseline = (int *)malloc (sizeof (int));
840 * return the finded parameters for cvGetTextSize
842 %typemap(argout) (CvSize* text_size, int* baseline) {
843 PyObject * to_add[2];
845 /* extract the pointers we want to add to the returned tuple */
846 to_add [0] = SWIG_NewPointerObj ($1, $descriptor(CvSize *), 0);
847 to_add [1] = PyInt_FromLong (*$2);
849 $result = SWIG_AppendResult($result, to_add, 2);
854 * curr_features is output parameter for cvCalcOpticalFlowPyrLK
856 %typemap (in, numinputs=1) (CvPoint2D32f* curr_features, int count)
858 /* as input, we only need the size of the wanted features */
860 /* memorize the size of the wanted features */
861 tmpCount = (int)PyInt_AsLong ($input);
863 /* create the array for the C call */
864 $1 = (CvPoint2D32f *) malloc(tmpCount * sizeof (CvPoint2D32f));
866 /* the size of the array for the C call */
871 * the features returned by cvCalcOpticalFlowPyrLK
873 %typemap(argout) (CvPoint2D32f* curr_features, int count) {
877 /* create the list to return */
878 to_add = PyList_New (tmpCount$argnum);
880 /* extract all the points values of the result, and add it to the
881 final resulting list */
882 for (i = 0; i < tmpCount$argnum; i++) {
883 PyList_SetItem (to_add, i,
884 SWIG_NewPointerObj (&($1 [i]),
885 $descriptor(CvPoint2D32f *), 0));
888 $result = SWIG_AppendResult($result, &to_add, 1);
892 * status is an output parameters for cvCalcOpticalFlowPyrLK
894 %typemap (in, numinputs=1) (char *status) (int tmpCountStatus){
895 /* as input, we still need the size of the status array */
897 /* memorize the size of the status array */
898 tmpCountStatus = (int)PyInt_AsLong ($input);
900 /* create the status array for the C call */
901 $1 = (char *)malloc (tmpCountStatus * sizeof (char));
905 * the status returned by cvCalcOpticalFlowPyrLK
907 %typemap(argout) (char *status) {
911 /* create the list to return */
912 to_add = PyList_New (tmpCountStatus$argnum);
914 /* extract all the integer values of the result, and add it to the
915 final resulting list */
916 for (i = 0; i < tmpCountStatus$argnum; i++) {
917 PyList_SetItem (to_add, i, PyBool_FromLong ($1 [i]));
920 $result = SWIG_AppendResult($result, &to_add, 1);
923 /* map one list of points to the two parameters dimenssion/sizes
924 for cvCalcOpticalFlowPyrLK */
925 %typemap(in) (CvPoint2D32f* prev_features)
930 /* get the size of the input array */
931 size = PyList_Size ($input);
933 /* allocate the needed memory */
934 CvPoint2D32f * features = (CvPoint2D32f *) malloc (size * sizeof (CvPoint2D32f));
936 /* extract all the points values from the list */
937 for (i = 0; i < size; i++)
939 PyObject *item = PyList_GetItem ($input, i);
942 SWIG_Python_ConvertPtr (item, &vptr,
943 $descriptor(CvPoint2D32f*),
944 SWIG_POINTER_EXCEPTION);
945 CvPoint2D32f *p = (CvPoint2D32f *)vptr;
946 features[i].x = p->x;
947 features[i].y = p->y;
950 // these are the arguments passed to the OpenCV function
955 * the corners returned by cvGoodFeaturesToTrack
957 %typemap (in, numinputs=1) (CvPoint2D32f* corners, int* corner_count) (int tmpCount)
959 /* as input, we still need the size of the corners array */
961 /* memorize the size of the status corners */
962 tmpCount = (int) PyInt_AsLong ($input);
964 // these are the arguments passed to the OpenCV function
965 $1 = (CvPoint2D32f *) malloc (tmpCount * sizeof (CvPoint2D32f));
970 * the corners returned by cvGoodFeaturesToTrack
972 %typemap(argout) (CvPoint2D32f* corners, int* corner_count)
977 /* create the list to return */
978 to_add = PyList_New (tmpCount$argnum);
980 /* extract all the integer values of the result, and add it to the final resulting list */
981 for (i = 0; i < tmpCount$argnum; i++)
982 PyList_SetItem (to_add, i, SWIG_NewPointerObj (&($1 [i]), $descriptor(CvPoint2D32f *), 0));
984 $result = SWIG_AppendResult($result, &to_add, 1);
987 /* map one list of points to the two parameters dimension/sizes for cvFindCornerSubPix */
988 %typemap(in, numinputs=1) (CvPoint2D32f* corners, int count) (int cornersCount, CvPoint2D32f* corners)
990 if(! PySequence_Check ($input))
992 PyErr_SetString(PyExc_TypeError, "Expected a list or tuple");
996 // TODO: will this ever be freed?
997 cornersCount = PySequence_Size ($input);
998 corners = (CvPoint2D32f *) malloc (cornersCount * sizeof (CvPoint2D32f));
1000 // extract all the points values from the list */
1001 CvPoint2D32f * corner = corners;
1002 for (int i = 0; i < cornersCount; i++, corner++)
1004 PyObject * item = PySequence_GetItem ($input, i);
1006 if (PySequence_Check(item) && PySequence_Length(item) == 2)
1008 PyObject * tuple = PySequence_Tuple (item);
1009 if (!PyArg_ParseTuple (tuple, "ff", & corner->x, & corner->y))
1011 PyErr_SetString(PyExc_TypeError,"each entry must consist of 2 floats (x, y)");
1021 if (SWIG_ConvertPtr (item, (void **) & ptr, $descriptor(CvPoint2D32f *), SWIG_POINTER_EXCEPTION) == -1)
1023 PyErr_SetString (PyExc_TypeError,"expected a sequence of 2 floats (x, y) or a CvPoint2D32f");
1033 // these are the arguments passed to the OpenCV function
1039 * the corners returned by cvFindCornerSubPix
1041 %typemap(argout) (CvPoint2D32f* corners, int count)
1046 /* create the list to return */
1047 to_add = PyList_New (cornersCount$argnum);
1049 /* extract all the corner values of the result, and add it to the
1050 final resulting list */
1051 for (i = 0; i < cornersCount$argnum; i++)
1052 PyList_SetItem (to_add, i, SWIG_NewPointerObj (&(corners$argnum [i]), $descriptor(CvPoint2D32f *), 0));
1054 $result = SWIG_AppendResult( $result, &to_add, 1);
1058 * return the corners for cvFindChessboardCorners
1060 %typemap(in, numinputs=1) (CvSize pattern_size, CvPoint2D32f * corners, int * corner_count )
1061 (CvSize * pattern_size, CvPoint2D32f * tmp_corners, int tmp_ncorners) {
1063 if( SWIG_ConvertPtr($input, &vptr, $descriptor( CvSize * ), SWIG_POINTER_EXCEPTION ) == -1){
1066 pattern_size=(CvSize *)vptr;
1067 tmp_ncorners = pattern_size->width*pattern_size->height;
1069 tmp_corners = (CvPoint2D32f *) malloc(sizeof(CvPoint2D32f)*tmp_ncorners);
1075 %typemap(argout) (CvSize pattern_size, CvPoint2D32f * corners, int * corner_count){
1079 /* create the list to return */
1080 to_add = PyList_New ( tmp_ncorners$argnum );
1082 /* extract all the corner values of the result, and add it to the
1083 final resulting list */
1084 for (i = 0; i < tmp_ncorners$argnum; i++) {
1085 CvPoint2D32f * pt = new CvPoint2D32f;
1086 pt->x = tmp_corners$argnum[i].x;
1087 pt->y = tmp_corners$argnum[i].y;
1089 PyList_SetItem (to_add, i,
1090 SWIG_NewPointerObj( pt, $descriptor(CvPoint2D32f *), 0));
1093 $result = SWIG_AppendResult( $result, &to_add, 1);
1094 free(tmp_corners$argnum);
1098 * return the matrices for cvCameraCalibrate
1100 %typemap(in, numinputs=0) (CvMat * intrinsic_matrix, CvMat * distortion_coeffs)
1102 $1 = cvCreateMat(3,3,CV_32F);
1103 $2 = cvCreateMat(4,1,CV_32F);
1106 %typemap(argout) (CvMat * intrinsic_matrix, CvMat * distortion_coeffs)
1108 PyObject * to_add[2] = {NULL, NULL};
1109 to_add[0] = SWIG_NewPointerObj($1, $descriptor(CvMat *), 1);
1110 to_add[1] = SWIG_NewPointerObj($2, $descriptor(CvMat *), 1);
1111 $result = SWIG_AppendResult( $result, to_add, 2 );
1115 * Fix OpenCV inheritance for CvSeq, CvSet, CvGraph
1116 * Otherwise, can't call CvSeq functions on CvSet or CvGraph
1118 %typemap(in, numinputs=1) (CvSeq *) (void * ptr)
1121 if( SWIG_ConvertPtr($input, &ptr, $descriptor(CvSeq *), 0) == -1 &&
1122 SWIG_ConvertPtr($input, &ptr, $descriptor(CvSet *), 0) == -1 &&
1123 SWIG_ConvertPtr($input, &ptr, $descriptor(CvGraph *), 0) == -1 &&
1124 SWIG_ConvertPtr($input, &ptr, $descriptor(CvSubdiv2D *), 0) == -1 &&
1125 SWIG_ConvertPtr($input, &ptr, $descriptor(CvChain *), 0) == -1 &&
1126 SWIG_ConvertPtr($input, &ptr, $descriptor(CvContour *), 0) == -1 &&
1127 SWIG_ConvertPtr($input, &ptr, $descriptor(CvContourTree *), 0) == -1 )
1129 SWIG_exception (SWIG_TypeError, "could not convert to CvSeq");
1135 %typemap(in, numinputs=1) (CvSet *) (void * ptr)
1137 if( SWIG_ConvertPtr($input, &ptr, $descriptor(CvSet *), 0) == -1 &&
1138 SWIG_ConvertPtr($input, &ptr, $descriptor(CvGraph *), 0) == -1 &&
1139 SWIG_ConvertPtr($input, &ptr, $descriptor(CvSubdiv2D *), 0) == -1)
1141 SWIG_exception (SWIG_TypeError, "could not convert to CvSet");
1147 %typemap(in, numinputs=1) (CvGraph *) (void * ptr)
1149 if( SWIG_ConvertPtr($input, &ptr, $descriptor(CvGraph *), 0) == -1 &&
1150 SWIG_ConvertPtr($input, &ptr, $descriptor(CvSubdiv2D *), 0) == -1)
1152 SWIG_exception (SWIG_TypeError, "could not convert to CvGraph");
1155 $1 = (CvGraph *)ptr;
1159 * Remap output arguments to multiple return values for cvMinEnclosingCircle
1161 %typemap(in, numinputs=0) (CvPoint2D32f * center, float * radius) (CvPoint2D32f * tmp_center, float tmp_radius)
1163 tmp_center = (CvPoint2D32f *) malloc(sizeof(CvPoint2D32f));
1167 %typemap(argout) (CvPoint2D32f * center, float * radius)
1169 PyObject * to_add[2] = {NULL, NULL};
1170 to_add[0] = SWIG_NewPointerObj( tmp_center$argnum, $descriptor(CvPoint2D32f *), 1);
1171 to_add[1] = PyFloat_FromDouble( tmp_radius$argnum );
1173 $result = SWIG_AppendResult($result, to_add, 2);
1177 %typemap(in, numinputs=0) (CvPoint2D32f pt[4]) (CvPoint2D32f tmp_pts[4])
1181 %typemap(argout) (CvPoint2D32f pt[4])
1183 PyObject * to_add = PyList_New(4);
1186 CvPoint2D32f * p = new CvPoint2D32f;
1187 *p = tmp_pts$argnum[i];
1188 PyList_SetItem(to_add, i, SWIG_NewPointerObj( p, $descriptor(CvPoint2D32f *), 1 ) );
1190 $result = SWIG_AppendResult($result, &to_add, 1);
1193 /** Macro to wrap a built-in type that is used as an object like CvRNG and CvSubdiv2DEdge */
1194 %define %wrap_builtin(type)
1197 class type##_Wrapper {
1201 type##_Wrapper( const type & val ) :
1205 type * ptr() { return &m_val; }
1206 type & ref() { return m_val; }
1207 bool operator==(const type##_Wrapper & x){
1208 return m_val==x.m_val;
1210 bool operator!=(const type##_Wrapper & x){
1211 return m_val!=x.m_val;
1217 type##_Wrapper * wrapper = new type##_Wrapper( $1 );
1218 $result = SWIG_NewPointerObj( wrapper, $descriptor( type##_Wrapper * ), 1 );
1220 %typemap(out) type *
1222 type##_Wrapper * wrapper = new type##_Wrapper( *($1) );
1223 $result = SWIG_NewPointerObj( wrapper, $descriptor( type##_Wrapper * ), 1 );
1226 %typemap(in) (type *) (void * vptr, type##_Wrapper * wrapper){
1227 if(SWIG_ConvertPtr($input, &vptr, $descriptor(type##_Wrapper *), 0)==-1){
1228 SWIG_exception( SWIG_TypeError, "could not convert Python object to C value");
1231 wrapper = (type##_Wrapper *) vptr;
1232 $1 = wrapper->ptr();
1234 %typemap(in) (type) (void * vptr, type##_Wrapper * wrapper){
1235 if(SWIG_ConvertPtr($input, &vptr, $descriptor(type##_Wrapper *), 0)==-1){
1236 SWIG_exception( SWIG_TypeError, "could not convert Python object to C value");
1239 wrapper = (type##_Wrapper *) vptr;
1240 $1 = wrapper->ref();
1244 /** Application of wrapper class to built-in types */
1245 %wrap_builtin(CvRNG);
1246 %wrap_builtin(CvSubdiv2DEdge);
1249 * Allow CvQuadEdge2D to be interpreted as CvSubdiv2DEdge
1251 %typemap(in, numinputs=1) (CvSubdiv2DEdge) (CvSubdiv2DEdge_Wrapper * wrapper, CvQuadEdge2D * qedge, void *vptr)
1253 if( SWIG_ConvertPtr($input, &vptr, $descriptor(CvSubdiv2DEdge_Wrapper *), 0) != -1 ){
1254 wrapper = (CvSubdiv2DEdge_Wrapper *) vptr;
1255 $1 = wrapper->ref();
1257 else if( SWIG_ConvertPtr($input, &vptr, $descriptor(CvQuadEdge2D *), 0) != -1 ){
1258 qedge = (CvQuadEdge2D *) vptr;
1259 $1 = (CvSubdiv2DEdge)qedge;
1262 SWIG_exception( SWIG_TypeError, "could not convert to CvSubdiv2DEdge");
1268 * return the vertex and edge for cvSubdiv2DLocate
1270 %typemap(in, numinputs=0) (CvSubdiv2DEdge * edge, CvSubdiv2DPoint ** vertex)
1271 (CvSubdiv2DEdge tmpEdge, CvSubdiv2DPoint * tmpVertex)
1276 %typemap(argout) (CvSubdiv2DEdge * edge, CvSubdiv2DPoint ** vertex)
1278 PyObject * to_add[2] = {NULL, NULL};
1279 if(result==CV_PTLOC_INSIDE || result==CV_PTLOC_ON_EDGE){
1280 CvSubdiv2DEdge_Wrapper * wrapper = new CvSubdiv2DEdge_Wrapper( tmpEdge$argnum );
1281 to_add[0] = SWIG_NewPointerObj( wrapper, $descriptor(CvSubdiv2DEdge_Wrapper *), 0);
1282 to_add[1] = Py_None;
1284 if(result==CV_PTLOC_VERTEX){
1285 to_add[0] = Py_None;
1286 to_add[1] = SWIG_NewPointerObj( tmpVertex$argnum, $descriptor(CvSubdiv2DPoint *), 0);
1289 $result = SWIG_AppendResult($result, to_add, 2);
1293 * int *value in cvCreateTrackbar() is only used for input in the Python wrapper.
1294 * for output, use the pos in the callback
1295 * TODO: remove the memory leak introducted by the malloc () (if needed).
1297 %typemap(in, numinputs=1) (int *value)
1299 $1 = (int *)malloc (sizeof (int));
1300 *$1 = PyInt_AsLong ($input);
1305 * take (query_points,k) and return (indices,dist)
1306 * for cvLSHQuery, cvFindFeatures
1308 %typemap(in, noblock=1) (const CvMat* query_points) (bool freearg=false, int num_query_points)
1310 $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
1311 num_query_points = $1->rows;
1313 %typemap(freearg) (const CvMat* query_points) {
1314 if($1!=NULL && freearg$argnum){
1315 cvReleaseData( $1 );
1319 %typemap(in) (CvMat* indices, CvMat* dist, int k)
1321 $3 = (int)PyInt_AsLong($input);
1322 $1 = cvCreateMat(num_query_points2, $3, CV_32SC1);
1323 $2 = cvCreateMat(num_query_points2, $3, CV_64FC1);
1325 %typemap(argout) (CvMat* indices, CvMat* dist, int k)
1327 $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($1, $descriptor(CvMat *), 1) );
1328 $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($2, $descriptor(CvMat *), 1) );
1332 * take (data) and return (indices)
1335 %typemap(in) (const CvMat* data, CvMat* indices) (bool freearg=false)
1337 $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
1338 CvMat* m = (CvMat*)$1;
1339 $2 = cvCreateMat(m->rows, 1, CV_32SC1 );
1341 %typemap(argout) (const CvMat* data, CvMat* indices)
1343 $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($2, $descriptor(CvMat *), 1) );
1345 %typemap(freearg) (const CvMat* data, CvMat* indices) {
1346 if($1!=NULL && freearg$argnum){
1347 cvReleaseData( $1 );
1353 * take (max_out_indices) and return (indices)
1354 * for cvFindFeaturesBoxed
1356 %typemap(in) (CvMat* out_indices) (bool freearg=false)
1358 int max_out_indices = (int)PyInt_AsLong($input);
1359 $1 = cvCreateMat(max_out_indices, 1, CV_32SC1 );
1361 %typemap(argout) (CvMat* out_indices)
1363 $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($1, $descriptor(CvMat *), 1) );
1367 * suppress (CvSeq** keypoints, CvSeq** descriptors, CvMemStorage* storage) and return (keypoints, descriptors)
1370 %typemap(in,numinputs=0) (CvSeq** keypoints, CvSeq** descriptors, CvMemStorage* storage)
1371 (CvSeq* keypoints = 0, CvSeq* descriptors = 0,CvMemStorage* storage)
1373 storage = cvCreateMemStorage();
1378 %typemap(argout) (CvSeq** keypoints, CvSeq** descriptors, CvMemStorage* storage)
1381 int n2 = (*$2)->elem_size / sizeof(float);
1382 assert((*$2)->elem_size == 64 * sizeof(float) ||
1383 (*$2)->elem_size == 128 * sizeof(float));
1384 assert((*$2)->total == (*$1)->total);
1385 CvMat* m1 = cvCreateMat((*$2)->total,n1,CV_32FC1);
1386 CvMat* m2 = cvCreateMat((*$2)->total,n2,CV_32FC1);
1388 cvStartReadSeq(*$1, &r1);
1389 float* m1p = m1->data.fl;
1390 for (int j=0;j<(*$2)->total;++j) {
1391 CvSURFPoint* sp = (CvSURFPoint*)r1.ptr;
1394 m1p[2] = sp->laplacian;
1397 m1p[5] = sp->hessian;
1399 CV_NEXT_SEQ_ELEM((*$1)->elem_size, r1);
1402 cvStartReadSeq(*$2, &r2);
1403 float* m2p = m2->data.fl;
1404 for (int j=0;j<(*$2)->total;++j) {
1405 memcpy(m2p,r2.ptr,n2*sizeof(float));
1407 CV_NEXT_SEQ_ELEM((*$2)->elem_size, r2);
1409 $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj(m1, $descriptor(CvMat *), 1) );
1410 $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj(m2, $descriptor(CvMat *), 1) );
1411 cvReleaseMemStorage(&$3);
1415 * suppress (CvMat* homography) and return (homography)
1416 * for cvFindHomography
1418 %typemap(in,numinputs=0) (CvMat* homography) (bool freearg=false)
1420 $1 = cvCreateMat(3,3,CV_64FC1);
1422 %typemap(argout) (CvMat* homography)
1424 $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($1, $descriptor(CvMat *), 1) );
1428 * take (coeffs) for (const CvMat* coeffs, CvMat *roots) and return (roots)
1431 %typemap(in) (const CvMat* coeffs, CvMat *roots) (bool freearg=false)
1433 $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
1434 int m = $1->rows * $1->cols;
1436 PyErr_SetString (PyExc_TypeError,"must give at least 2 coefficients");
1439 $2 = cvCreateMat(m-1, 1, CV_MAKETYPE(CV_MAT_DEPTH($1->type),1));
1441 %typemap(argout) (const CvMat* coeffs, CvMat *roots)
1443 $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($2, $descriptor(CvMat *), 1) );
1445 %typemap(freearg) (const CvMat* coeffs, CvMat *roots)
1447 if($1!=NULL && freearg$argnum){
1448 cvReleaseData( $1 );
1454 * take (coeffs) for (const CvMat* coeffs, CvMat *roots2) and return (roots2)
1457 %typemap(in) (const CvMat* coeffs, CvMat *roots2) (bool freearg=false)
1459 $1 = (CvMat*)PyObject_to_CvArr($input, &freearg);
1460 int m = $1->rows * $1->cols;
1462 PyErr_SetString (PyExc_TypeError,"must give at least 2 coefficients");
1465 $2 = cvCreateMat(m-1, 1, CV_MAKETYPE(CV_MAT_DEPTH($1->type),2));
1467 %typemap(argout) (const CvMat* coeffs, CvMat *roots2)
1469 $result = SWIG_AppendOutput( $result, SWIG_NewPointerObj($2, $descriptor(CvMat *), 1) );
1471 %typemap(freearg) (const CvMat* coeffs, CvMat *roots2)
1473 if($1!=NULL && freearg$argnum){
1474 cvReleaseData( $1 );