Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <boost/python.hpp>
00009 #include <boost/python/stl_iterator.hpp>
00010 #include <iostream>
00011 #include <string>
00012 #include <vector>
00013 #include <list>
00014 #include <iterator>
00015 #include <boost/foreach.hpp>
00016 #include <pano_core/pano_core.h>
00017 #include <pano_py/opencv.h>
00018 #include <opencv2/highgui/highgui.hpp>
00019
00020 #include "wrappers.h"
00021
00022 using namespace pano;
00023 namespace bp = boost::python;
00024 using namespace cv;
00025 namespace pano_py
00026 {
00027
00028 namespace
00029 {
00030 struct memtrack_t {
00031 PyObject_HEAD
00032 void *ptr;
00033 Py_ssize_t size;
00034 };
00035
00036
00037 struct cvmat_t
00038 {
00039 PyObject_HEAD
00040 CvMat *a;
00041 PyObject *data;
00042 size_t offset;
00043 };
00044
00045 struct iplimage_t {
00046 PyObject_HEAD
00047 IplImage *a;
00048 PyObject *data;
00049 size_t offset;
00050 };
00051
00052 cv::Mat convert_from_cvmat(PyObject *o, const char* name)
00053 {
00054 Mat dest;
00055 cvmat_t *m = (cvmat_t*)o;
00056 void *buffer;
00057 Py_ssize_t buffer_len;
00058
00059 m->a->refcount = NULL;
00060 if (m->data && PyString_Check(m->data))
00061 {
00062 assert(cvGetErrStatus() == 0);
00063 char *ptr = PyString_AsString(m->data) + m->offset;
00064 cvSetData(m->a, ptr, m->a->step);
00065 assert(cvGetErrStatus() == 0);
00066 dest = m->a;
00067
00068 }
00069 else if (m->data && PyObject_AsWriteBuffer(m->data, &buffer, &buffer_len) == 0)
00070 {
00071 cvSetData(m->a, (void*)((char*)buffer + m->offset), m->a->step);
00072 assert(cvGetErrStatus() == 0);
00073 dest = m->a;
00074 }
00075 else
00076 {
00077 failmsg("CvMat argument '%s' has no data", name);
00078 }
00079 return dest;
00080
00081 }
00082
00083 cv::Mat convert_from_cviplimage(PyObject *o,const char *name)
00084 {
00085 Mat dest;
00086 iplimage_t *ipl = (iplimage_t*)o;
00087 void *buffer;
00088 Py_ssize_t buffer_len;
00089
00090 if (PyString_Check(ipl->data)) {
00091 cvSetData(ipl->a, PyString_AsString(ipl->data) + ipl->offset, ipl->a->widthStep);
00092 assert(cvGetErrStatus() == 0);
00093 dest = ipl->a;
00094 } else if (ipl->data && PyObject_AsWriteBuffer(ipl->data, &buffer, &buffer_len) == 0) {
00095 cvSetData(ipl->a, (void*)((char*)buffer + ipl->offset), ipl->a->widthStep);
00096 assert(cvGetErrStatus() == 0);
00097 dest = ipl->a;
00098 } else {
00099 failmsg("IplImage argument '%s' has no data", name);
00100 }
00101 return dest;
00102 }
00103
00104
00105 }
00106
00108 int failmsg(const char *fmt, ...)
00109 {
00110 char str[1000];
00111
00112 va_list ap;
00113 va_start(ap, fmt);
00114 vsnprintf(str, sizeof(str), fmt, ap);
00115 va_end(ap);
00116
00117 PyErr_SetString(PyExc_TypeError, str);
00118 return 0;
00119 }
00120
00121 cv::Mat convertObj2Mat(bp::object image)
00122 {
00123 if(strcmp(image.ptr()->ob_type->tp_name,"cv.iplimage") == 0){
00124 return convert_from_cviplimage(image.ptr(),image.ptr()->ob_type->tp_name);
00125 }else
00126 return convert_from_cvmat(image.ptr(), image.ptr()->ob_type->tp_name);
00127
00128 }
00129
00130 cv::Mat convertNumpy2Mat(bp::object np)
00131 {
00132 Mat m;
00133 numpy_to_mat(np.ptr(),m);
00134 return m;
00135 }
00136
00137 void imwrite_noargs(const std::string& window_name, const cv::Mat& image){
00138 imwrite(window_name,image);
00139 }
00140
00141 BOOST_PYTHON_MODULE(pano_cv)
00142 {
00143
00144 bp::object opencv = bp::scope();
00145 opencv.attr("CV_8UC1") = CV_8UC1;
00146 opencv.attr("CV_32SC1") = CV_32SC1;
00147 opencv.attr("CV_32FC1") = CV_32FC1;
00148 opencv.attr("CV_64FC1") = CV_64FC1;
00149 opencv.attr("CV_WINDOW_KEEPRATIO") = int(CV_WINDOW_KEEPRATIO);
00150 opencv.attr("CV_WINDOW_NORMAL") = int(CV_WINDOW_NORMAL);
00151
00152 bp::class_<cv::Size>("Size")
00153 .def(bp::init<int, int>())
00154 .def_readwrite("width", &cv::Size::width)
00155 .def_readwrite("height",&cv::Size::height)
00156 .def("area",&cv::Size::area)
00157 ;
00158
00159 wrapMat();
00160
00161 bp::def("convertNumpy2Mat",convertNumpy2Mat);
00162 bp::def("convertCvMat2Mat",convertObj2Mat);
00163
00164 bp::def("namedWindow",cv::namedWindow);
00165 bp::def("imshow",cv::imshow);
00166 bp::def("imwrite",imwrite_noargs);
00167 bp::def("waitKey",cv::waitKey);
00168 }
00169 }