Integrating OpenCV with pygtk

I am currently working on an undisclosed project with a senior. It requires a GUI, and bunch of OpenCV codes. We started using pygtk, and openCV. python wrapper for openCV is literally slow and we need a much better performance than that. So, after some tests, we found out that we could use swig bindings. At first, we thought to make swig bindings ourselves, but as we started doing it, we realized it isn’t that easy to bind complete OpenCV library in swig. Hence, we started looking at some other options. os.system() seemed useless.

After looking on the internet for couple of days, we realized that openCV 2.1 provides swig bindings for python in the package. We tried, and it worked. That gave us some relief. We still had a huge problem. We had to show two different windows, one for GUI, and one for OpenCV. I tried many things including multithreading, multiprocessing, etc, but nothing seemed to be working. We were stuck again. I once crashed my Ubuntu as I encountered an infinite loop in openCV.

After a session of brainstorming, and testing for performance, we decided to show images in GUI itself. This was quite new for me. I had never converted an IplImage object to gtk.Image object. We had another option. We could save the image and then load it in GUI, but it seemed pointless as it took more time and memory. We badly wanted to find a way to convert IplImage object to gtk.Image object. After looking at dozens of codes, I stumbled upon a code on stackoverflow, which converted IplImage* object to gtk.Image object in C++. I tried to implement that in python. It took me some time to understand C++ code, as I have never done anything in C++.

So here is how I implemented it. And it is working fine.

img_pixbuf = gtk.gdk.pixbuf_new_from_data(img.imageData,
image = gtk.image_new_from_pixbuf(img_pixbuf)

NOTE: IplImage has colorspace BGR, but there is no BGR colorspace in gtk. So you need to change colorspace of image from BGR to RGB.

new_img = cvCreateImage(cvGetSize(img), img.depth, img.nChannels)

You can find corresponding documents regarding pixbuf, gtk.gdk.pixbuf_new_from_data from pygtk tutorial page.

Using this, we have made some fine codes which use openCV and display is show on pygtk GUI.

We have also started working on openTLD. We are trying to port it to python. We spent complete day on it. At the end, we settled on os.system() for running openTLD in C++. We can’t port it to python in 3-4 days, so os.system() seemed the best option. We have added multiprocessing, and it is working really well.

P.S. openTLD is simply awesome.


2 thoughts on “Integrating OpenCV with pygtk

  1. Pingback: OpenCV with pygtk « Paranoid Android

  2. Pingback: Using OpenCV with pygtk

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s