[mousetrap/ng] Adde new methods



commit e00065c95fbed018f66961b7abf72d0a28c0e48b
Author: Flavio Percoco Premoli <flaper87 gmail com>
Date:   Mon Nov 9 10:06:02 2009 +0100

    Adde new methods

 src/mousetrap/ocvfw/.project             |    6 ++
 src/mousetrap/ocvfw/dev/MtpCapture.cpp   |   30 +++++++++---
 src/mousetrap/ocvfw/dev/MtpCapture.h     |   30 +++++++++++-
 src/mousetrap/ocvfw/dev/PyMtpCapture.cpp |   77 ++++++++++++++++++++++++++---
 4 files changed, 127 insertions(+), 16 deletions(-)
---
diff --git a/src/mousetrap/ocvfw/.project b/src/mousetrap/ocvfw/.project
index 107f57c..abd3c7e 100644
--- a/src/mousetrap/ocvfw/.project
+++ b/src/mousetrap/ocvfw/.project
@@ -6,6 +6,11 @@
 	</projects>
 	<buildSpec>
 		<buildCommand>
+			<name>org.python.pydev.PyDevBuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
 			<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
 			<triggers>clean,full,incremental,</triggers>
 			<arguments>
@@ -77,5 +82,6 @@
 		<nature>org.eclipse.cdt.core.cnature</nature>
 		<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
 		<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
+		<nature>org.python.pydev.pythonNature</nature>
 	</natures>
 </projectDescription>
diff --git a/src/mousetrap/ocvfw/dev/MtpCapture.cpp b/src/mousetrap/ocvfw/dev/MtpCapture.cpp
index 6d7972b..62b219c 100644
--- a/src/mousetrap/ocvfw/dev/MtpCapture.cpp
+++ b/src/mousetrap/ocvfw/dev/MtpCapture.cpp
@@ -53,24 +53,40 @@ void MtpCapture::set_async(int set_fps, bool set_async) {
 }
 
 bool MtpCapture::sync() {
-	this->image = this->webcam.queryFrame();
+	this->img = this->webcam.queryFrame();
 
-	if (!this->image)
+	if (!this->img)
 		return true;
 
-	printf( "%d", this->image->height);
-
 	return true;
 }
 
+IplImage *MtpCapture::image() {
+	return this->img;
+}
+
+IplImage *MtpCapture::rect(CvRect rect) {
+	IplImage *p_rect;
+	CvMat p_mat;
+
+
+	p_rect = (IplImage *) cvAlloc(sizeof(IplImage));
+
+	cvGetSubRect(this->img, &p_mat, rect);
+
+	p_rect = cvGetImage(&p_mat, p_rect);
+
+	return p_rect;
+}
+
 IplImage *MtpCapture::resize(int width, int height, bool copy) {
 	IplImage *tmp;
 
-	tmp = cvCreateImage(cvSize(width, height), 8, this->image->nChannels);
-	cvResize(this->image, tmp, CV_INTER_AREA);
+	tmp = cvCreateImage(cvSize(width, height), 8, this->img->nChannels);
+	cvResize(this->img, tmp, CV_INTER_AREA);
 
 	if (!copy)
-		this->image = tmp;
+		this->img = tmp;
 
 	return tmp;
 }
diff --git a/src/mousetrap/ocvfw/dev/MtpCapture.h b/src/mousetrap/ocvfw/dev/MtpCapture.h
index 49704fd..98ef39e 100644
--- a/src/mousetrap/ocvfw/dev/MtpCapture.h
+++ b/src/mousetrap/ocvfw/dev/MtpCapture.h
@@ -54,29 +54,57 @@ public:
 
 	/**
 	 * Used to init the camera and capture vars
+	 *
+	 * @param set_fps The how frequently should be called the sync method.
+	 * @param set_async Whether to set or not the asynchronous calls.
+	 * @param idx The Camera Index.
 	 */
 	void init(int set_fps=100, bool set_async=false, int idx=0);
 
 	/**
 	 * Resizes the image
+	 *
+	 * @param width The new image width
+	 * @param height The new image height
+	 * @param copy Whether to create a new image with the new size
+	 * 				or replace the current one.
+	 * @return the resized image.
+	 *
 	 */
 	IplImage *resize(int width, int height, bool copy=false);
 
 	/**
+	 * Returns the image and allows users to change the
+	 * current image pointer.
+	 */
+	IplImage *image();
+
+	/**
 	 * Synchronize the capture quering a new frame.
 	 */
 	bool sync();
 
 	/**
 	 * Starts/Stops the asynchronous calls to the sync method.
+	 *
+	 * @param set_fps The how frequently should be called the sync method.
+	 * @param set_async Whether to set or not the asynchronous calls
 	 */
 	void set_async(int set_fps=100, bool set_async=false);
 
+	/**
+	 * Gets and Returns the required rectangle of the image.
+	 *
+	 * @param rect A CvRect object with the new rectangle params.
+	 * @returns  The pointer to the CvMat rectangle.
+	 */
+	IplImage *rect(CvRect rect);
+
 private:
 	/**
 	 *Opencv Capture Structure
 	 */
-	IplImage* image;
+	IplImage* img;
 
 	/**
 	 * Camera Object
diff --git a/src/mousetrap/ocvfw/dev/PyMtpCapture.cpp b/src/mousetrap/ocvfw/dev/PyMtpCapture.cpp
index 2653a4d..de39b28 100644
--- a/src/mousetrap/ocvfw/dev/PyMtpCapture.cpp
+++ b/src/mousetrap/ocvfw/dev/PyMtpCapture.cpp
@@ -38,12 +38,69 @@ static void Capture_dealloc(Capture* self) {
 }
 
 static PyObject *
+Ipl2PyDict(IplImage *frame) {
+	PyObject *tmp;
+
+    if (!frame)
+        return Py_None;
+
+	tmp = PyDict_New();
+	PyDict_SetItemString( tmp, "width",     PyInt_FromLong( frame->width     ) );
+	PyDict_SetItemString( tmp, "height",    PyInt_FromLong( frame->height    ) );
+	PyDict_SetItemString( tmp, "widthStep", PyInt_FromLong( frame->widthStep ) );
+	PyDict_SetItemString( tmp, "depth",     PyInt_FromLong( frame->depth    ) );
+	PyDict_SetItemString( tmp, "imageData", PyString_FromStringAndSize(frame->imageData, frame->imageSize) );
+
+	return tmp;
+}
+
+static PyObject *
+Capture_image(Capture *self) {
+	//return Ipl2PyDict(cap.image());
+	return Ipl2PyDict(cap.image());
+}
+
+static PyObject *
+Capture_rect(Capture *self, PyObject *args, PyObject *keywds) {
+	int x, y, width, height;
+	PyObject *tmp;
+
+	static char *kwlist[] = { "x", "y", "width", "height", NULL };
+
+	PyArg_ParseTupleAndKeywords(args, keywds, "iiii", kwlist, &x, &y, &width, &height);
+
+	tmp = Ipl2PyDict(cap.rect(cvRect(x, y, width, height)));
+
+	return tmp;
+
+}
+
+static PyObject *
+Capture_resize(Capture *self, PyObject *args, PyObject *keywds) {
+	int width, height, copy = 0;
+
+	static char *kwlist[] = { "width", "height", "copy", NULL };
+
+	PyArg_ParseTupleAndKeywords(args, keywds, "ii|i", kwlist, &width, &height, &copy);
+
+	return Ipl2PyDict(cap.resize(width, height, (copy == 0) ? false : true));
+
+}
+
+static PyObject *
+Capture_sync(Capture *self) {
+	cap.sync();
+
+	return Py_None;
+}
+
+static PyObject *
 Capture_set_async(Capture *self, PyObject *args, PyObject *keywds) {
 	int fps = 100, async=0;
 
 	static char *kwlist[] = { "fps", "async", NULL };
 
-	PyArg_ParseTupleAndKeywords(args, keywds, "ii", kwlist, &fps, &async);
+	PyArg_ParseTupleAndKeywords(args, keywds, "|ii", kwlist, &fps, &async);
 
 	cap.set_async(fps, (async == 0) ? false : true);
 	return Py_None;
@@ -58,24 +115,28 @@ Capture_new(PyTypeObject *type, PyObject *args, PyObject *kwds) {
 }
 
 static int Capture_init(Capture *self, PyObject *args, PyObject *keywds) {
-	int fps = 100, idx = 0, async = 1;
+	int fps = 100, idx = 0, async = 0;
 
 	static char *kwlist[] = { "fps", "async", "idx", NULL };
 
 	PyArg_ParseTupleAndKeywords(args, keywds, "|iii", kwlist, &fps, &async, &idx);
 
-	cap.init(fps, (async == 0) ? true : false, idx);
+	cap.init(fps, (async == 0) ? false : true, idx);
 
 	return 0;
 }
 
-static PyMemberDef Capture_members[] = { { NULL } /* Sentinel */
+static PyMemberDef Capture_members[] = {
+		{ NULL } /* Sentinel */
 };
 
-static PyMethodDef Capture_methods[] = { { "set_async",
-		(PyCFunction) Capture_set_async, METH_VARARGS|METH_KEYWORDS,
-		PyDoc_STR("Enables disables asynchronous calls to sync method.") }, {
-		NULL } /* Sentinel */
+static PyMethodDef Capture_methods[] = {
+		{ "set_async", (PyCFunction) Capture_set_async, METH_KEYWORDS, PyDoc_STR("Enables disables asynchronous calls to sync method.") },
+		{ "sync", (PyCFunction) Capture_sync, METH_NOARGS, PyDoc_STR("The Sync method that queries new frames.") },
+		{ "image", (PyCFunction) Capture_image, METH_NOARGS, PyDoc_STR("Returns the current IplDict object.") },
+		{ "resize", (PyCFunction) Capture_resize, METH_KEYWORDS, PyDoc_STR("Resizes the current image and returns it.") },
+		{ "rect", (PyCFunction) Capture_rect, METH_KEYWORDS, PyDoc_STR("Returns the required rectangle of the image.") },
+		{NULL } /* Sentinel */
 };
 
 static PyTypeObject CaptureType = { PyObject_HEAD_INIT(NULL)0, /*ob_size*/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]