[mousetrap/ng] Committing missed boost folder
- From: Flavio Percoco <flaper src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [mousetrap/ng] Committing missed boost folder
- Date: Sat, 16 Jan 2010 15:25:22 +0000 (UTC)
commit 4b311d1aba701e7497fd9054a282b1bf3280f9d4
Author: Flavio Percoco Premoli <flaper87 gmail com>
Date: Sat Jan 16 17:34:50 2010 +0100
Committing missed boost folder
src/mousetrap/ocvfw/boost/IplImage.cpp | 158 ++++++++++++++++++++++++++++
src/mousetrap/ocvfw/boost/Jamroot | 51 +++++++++
src/mousetrap/ocvfw/boost/PyMtpCapture.cpp | 84 +++++++++++++++
3 files changed, 293 insertions(+), 0 deletions(-)
---
diff --git a/src/mousetrap/ocvfw/boost/IplImage.cpp b/src/mousetrap/ocvfw/boost/IplImage.cpp
new file mode 100644
index 0000000..18f6bad
--- /dev/null
+++ b/src/mousetrap/ocvfw/boost/IplImage.cpp
@@ -0,0 +1,158 @@
+#define PY_ARRAY_UNIQUE_SYMBOL PyArrayXXX
+
+#include <stdio.h>
+#include <iostream>
+#include <string>
+
+#include <boost/python.hpp>
+#include <boost/python/numeric.hpp>
+
+#include <opencv/cv.h>
+#include <opencv/highgui.h>
+
+#include "numpy_boost.hpp"
+// #include "pymultiarray.hpp"
+
+using namespace std;
+using namespace boost::python;
+
+
+/****************************************************************************
+ * Utility functions to convert between NumPy and OpenCV data-structures
+ */
+
+/// Converts an IplImage into a python array with a single assginment
+PyObject* cv2py(IplImage* img)
+{
+ npy_intp* shape = new npy_intp[3];
+ shape[0] = img->height;
+ shape[1] = img->width;
+ shape[2] = img->nChannels;
+
+ char type;
+ switch(img->depth)
+ {
+ case IPL_DEPTH_8U : type = NPY_UBYTE ; break;
+ case IPL_DEPTH_8S : type = NPY_BYTE ; break;
+ case IPL_DEPTH_16U: type = NPY_USHORT; break;
+ case IPL_DEPTH_16S: type = NPY_SHORT ; break;
+ case IPL_DEPTH_32S: type = NPY_INT ; break;
+ case IPL_DEPTH_32F: type = NPY_FLOAT ; break;
+ default : type = NPY_DOUBLE; break;
+ }
+
+ PyObject* obj = PyArray_SimpleNewFromData(3, shape, type, img->imageData);
+
+ PyArrayObject* arr = (PyArrayObject*)obj;
+
+ int typesize = sizeof(img->imageData[0]);
+ arr->strides[0] = img->widthStep;
+ arr->strides[1] = img->nChannels*typesize;
+ arr->strides[2] = typesize;
+
+ return (PyObject*)arr;
+}
+
+/// Converts a python array into an IplImage with a single assginment
+IplImage* py2cv(PyObject* obj)
+{
+ PyArrayObject* arr = (PyArrayObject*)obj;
+
+ int depth;
+ switch(arr->descr->type_num)
+ {
+ case NPY_UBYTE : depth = IPL_DEPTH_8U ; break;
+ case NPY_BYTE : depth = IPL_DEPTH_8S ; break;
+ case NPY_USHORT: depth = IPL_DEPTH_16U; break;
+ case NPY_SHORT : depth = IPL_DEPTH_16S; break;
+ case NPY_INT : depth = IPL_DEPTH_32S; break;
+ case NPY_FLOAT : depth = IPL_DEPTH_32F; break;
+ default : depth = IPL_DEPTH_64F; break;
+ }
+
+ IplImage* img = cvCreateImage(cvSize(arr->dimensions[1], arr->dimensions[0]),
+ depth, arr->dimensions[2]);
+
+ img->imageData = arr->data;
+
+ return img;
+}
+
+/*****************************************************************************
+* FireWire Camera Class
+*/
+class CvFWCamera
+{
+ private:
+ // OpenCV capture structure
+ CvCapture *_capture;
+ int _width, _hight;
+
+ public:
+ /**
+ * Constructor
+ */
+ CvFWCamera(int w, int h)
+ {
+ _width = w;
+ _hight = h;
+ _capture = cvCreateCameraCapture(0);
+
+ if (!_capture) {
+ throw std::runtime_error("Could not create camera capture");
+ }
+
+ cvSetCaptureProperty(_capture,CV_CAP_PROP_FPS, 7.5);
+ }
+
+ /**
+ * Capture next frame and return a tuple containing two NumPy arrays.
+ * One beeing the gray image, the other one beeing the RGB image.
+ */
+ tuple next_frame()
+ {
+ IplImage *frame;
+ IplImage *image;
+
+ // If out capture source really open?
+ if (!_capture) {
+ throw std::runtime_error("File not open");
+ }
+
+ // Get next frame
+ frame = cvQueryFrame(_capture);
+
+ if (!frame) {
+ throw std::runtime_error("Could not capture frame from Camera");
+ }
+
+ image = cvCreateImage(cvSize(_width, _hight),
+ frame->depth, frame->nChannels);
+
+ cvResize(frame, image);
+
+ // Convert to gray image
+ IplImage* gray_image = cvCreateImage(cvSize(image->width,image->height),
+ IPL_DEPTH_8U, 1);
+
+ cvCvtColor(image, gray_image, CV_BGR2GRAY);
+
+ // Convert to NumPy arrays
+ boost::python::handle<> gray_arr( cv2py(gray_image) );
+ boost::python::handle<> rgb_arr( cv2py(image) );
+
+ // Make and return a tuple
+ return make_tuple(gray_arr, rgb_arr);
+ }
+};
+
+/*****************************************************************************
+* Use boost-python to actually export classes and functions...
+*/
+BOOST_PYTHON_MODULE(fwcamera_ext) {
+ import_array();
+
+ class_<CvFWCamera>("CvFWCamera", init<int, int>())
+ .def("next_frame", &CvFWCamera::next_frame)
+ ;
+}
diff --git a/src/mousetrap/ocvfw/boost/Jamroot b/src/mousetrap/ocvfw/boost/Jamroot
new file mode 100644
index 0000000..e2a88d4
--- /dev/null
+++ b/src/mousetrap/ocvfw/boost/Jamroot
@@ -0,0 +1,51 @@
+using python ;
+
+# LIBS
+lib libboost_python : : <name>boost_python ;
+lib libcv : : <name>cv ;
+lib libhighgui : : <name>highgui ;
+lib libcxcore : : <name>cxcore ;
+lib libcvaux : : <name>cvaux ;
+lib libml : : <name>ml ;
+lib libgobject-2.0 : : <name>gobject-2.0 ;
+lib libgmodule-2.0 : : <name>gmodule-2.0 ;
+lib libgthread-2.0 : : <name>gthread-2.0 ;
+lib librt : : <name>rt ;
+lib libxml2 : : <name>xml2 ;
+lib libglib-2.0 : : <name>glib-2.0 ;
+lib libglibmm-2.4 : : <name>glibmm-2.4 ;
+lib libsigc-2.0 : : <name>sigc-2.0 ;
+
+
+project
+ : requirements <library>libboost_python
+ <library>libcv
+ <library>libhighgui
+ <library>libcxcore
+ <library>libcvaux
+ <library>libml
+ <library>libgobject-2.0
+ <library>libgmodule-2.0
+ <library>libgthread-2.0
+ <library>librt
+ <library>libxml2
+ <library>libglib-2.0
+ <library>libglibmm-2.4
+ <library>libsigc-2.0
+ <include>/usr/include/opencv
+ <include>/usr/include/glib-2.0/
+ <include>/usr/lib/glib-2.0/include/
+ <include>/usr/include/glibmm-2.4/
+ <include>/usr/lib/glibmm-2.4/include/
+ <include>/usr/include/libxml2/
+ <include>/usr/include/sigc++-2.0/
+ <include>/usr/include/sigc++-2.0/sigc++/
+ <include>/usr/lib/sigc++-2.0/include
+ <include>../include
+ ;
+
+# Declare the three extension modules. You can specify multiple
+# source files after the colon separated by spaces.
+python-extension camera : PyMtpCapture.cpp ;
+
+
diff --git a/src/mousetrap/ocvfw/boost/PyMtpCapture.cpp b/src/mousetrap/ocvfw/boost/PyMtpCapture.cpp
new file mode 100644
index 0000000..1e19249
--- /dev/null
+++ b/src/mousetrap/ocvfw/boost/PyMtpCapture.cpp
@@ -0,0 +1,84 @@
+/**
+ * Ocvfw
+ *
+ * Copyright 2009 Flavio Percoco Premoli
+ *
+ * This file is part of Ocvfw.
+ *
+ * Ocvfw is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License v2 as published
+ * by the Free Software Foundation.
+ *
+ * Ocvfw is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Ocvfw. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "cv.h"
+#include "highgui.h"
+#include "MtpCapture.h"
+#include <boost/python.hpp>
+#include <boost/python/module.hpp>
+#include <boost/python/def.hpp>
+
+using namespace std;
+using namespace cv;
+using namespace boost::python;
+
+// Defining memebers overloads
+BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(color_overloads, color, 2, 3)
+
+BOOST_PYTHON_MODULE(camera)
+{
+ class_<Mat>("Mat", init<>())
+ .def_readwrite("cols", &Mat::cols )
+ .def_readwrite("rows", &Mat::rows )
+ .def_readwrite("step", &Mat::step )
+ //.def_readwrite( "data", &Mat::data )
+ .def_readwrite( "flags", &Mat::flags )
+ .def_readwrite( "dataend", &Mat::dataend )
+ .def_readwrite( "refcount", &Mat::refcount )
+ .def_readwrite( "datastart", &Mat::datastart )
+
+// .staticmethod( "diag" )
+// .staticmethod("from_ndarray")
+
+ .def( init< Size, int >() )
+ .def( init< Mat const & >() )
+ .def( init< int, int, int >() )
+ .def( init< Size, int, Scalar const & >() )
+ .def( init< Mat const &, Rect const & >() )
+ .def( init< int, int, int, Scalar const & >() )
+ .def( init< Size, int, void *, optional< size_t > >() )
+ .def( init< int, int, int, void *, optional< size_t > >() )
+ .def( init< Mat const &, Range const &, Range const & >() )
+
+ .def("col", &Mat::col)
+ .def("row", &Mat::row)
+ .def("size", &Mat::size)
+ .def("type", &Mat::type)
+ .def("empty", &Mat::empty)
+ .def("clone", &Mat::clone)
+ .def("depth", &Mat::depth)
+ //.def("colRange", &Mat::colRange)
+ .def("assignTo", &Mat::assignTo)
+ .def("channels", &Mat::channels)
+ //.def("from_ndarray", &as_Mat);
+ //.def("adjustROI", &Mat::adjustROI)
+ ;
+
+ class_<MtpCapture>("Capture")
+ .def("color", &MtpCapture::color, color_overloads())
+ .def("flip", &MtpCapture::flip)
+ .def("image", &MtpCapture::image)
+ .def("init", &MtpCapture::init)
+ .def("rect", &MtpCapture::rect)
+ .def("resize", &MtpCapture::resize)
+ .def("set_async", &MtpCapture::set_async)
+ .def("sync", &MtpCapture::sync)
+ ;
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]