Category Archives: image processing

Python – OpenCV IplImage To PyQt QImage

This is a quick post which will hopefully save someone some time. I spent far too long trying to figure this out.
If you’re using Qt and OpenCV with python and want to show an opencv iplimage within a Qt Widget here’s a quick hack to convert the iplimage to a qimage.


class IplQImage(QtGui.QImage):
"""A class for converting iplimages to qimages"""

     def __init__(self,iplimage):
         #Rough-n-ready but it works dammit
         alpha = cv.CreateMat(iplimage.height,iplimage.width, cv.CV_8UC1)
         cv.Rectangle(alpha,(0,0),(iplimage.width,iplimage.height),cv.ScalarAll(255),-1)
         rgba = cv.CreateMat(iplimage.height,iplimage.width, cv.CV_8UC4)
         cv.Set(rgba, (1,2,3,4))
         cv.MixChannels([iplimage, alpha],[rgba], [
         (0, 0), # rgba[0] -> bgr[2]
         (1, 1), # rgba[1] -> bgr[1]
         (2, 2), # rgba[2] -> bgr[0]
         (3, 3) # rgba[3] -> alpha[0]
         ])
         self.__imagedata = rgba.tostring()
         super(IplQImage,self).__init__(self.__imagedata, iplimage.width,iplimage.height, QtGui.QImage.Format_RGB32)

All in all it’s fairly straight forward, an example case is something like the following:


#Create a 3 channel RGB iplimage (could be from a webcam ect.)
iplimage = cv.CreateMat(iplimage.height,iplimage.width, cv.CV_8UC3)
#Turn it into a qimage
qimage = IplQImage(iplimage)

It works by sub-classing QImage and overloading the constructor with a new one that accepts an IplImage, this then has an extra alapha channel added (to make it compatible with the QImage pixel packing) and finally the __init__ method of the superclass (QImage) is called with the data from the iplimage passed into it as a string.

The important part is:


self.__imagedata = rgba.tostring()

This keeps a reference to the image data so it doesn’t go out of scope when the __init__ returns. (the QImage constructor that accepts image data doesn’t keep a local reference to the data, so you have to make sure it isn’t lost.[at least i think that’s right])

Open3DScanner – ***Insert pun about cutting (final cut?, making the cut?,something mustard related?)***

Spent the weekend in St Neots visiting my friend Pete where we drank heavily and sellotaped mobile phones to kites (A blog post on that later). Took the day off on Monday and we both travelled down to London and did some laser cutting on a machine rented at blueprint model shop. Here are some photos of the cut parts:

Unassembled parts

Push Fit Assembled

Ps3 Eye Mounting

As kind of expected a bunch of stuff is slightly wrong with the parts, but nothing that can’t be easily fixed.

It’s bloody awesome seeing something I’ve spent so long designing on a computer in actual-real life. Alas there’s plenty of work left to do on the software and electronics side of things, but it’s nice to have made this milestone.

Kinect & Qt

Impulsed bought a kinect and decided to write a Qt wrapper for it. A fork of the libfreenect git repo with the wrapper included is available here at my spangly new github repository (Not quite figured this whole git malarky out so it’s probably gonna get broken at some point).
Heres a screen shot of the output from the RGB and Depth cameras (note: depth has had it’s dynamic range reduced from 11 to 8bits).

3D Scanner Software – First Results

Heres one of the first results of the new and improved 3D scanner software:

Theres still plenty of bugs to iron out in the software, but when thats all done i’ll make an “offical” first release of the software which will be available here at the google code repo.
I’m going to try and get a number of different builds available as well (64bit GNU/Linux, 32bit GNU/Linux, and a Windows release).

Also here’s a quick look at the new portable 3D scanner being made:

Quick Update-3D Scanner mk2

Just a quick update to say the 3D scanner project is starting it’s second phase. Gonna throw up some photos of it in a bit. The google code repo is broken at the moment (hell its been broken for the past few months), but within the week a new cross-platform version of the software will be available.

3D Scanner – Software Overhaul

So I’ve been having a tinker with the 3D scanner again and I’ve got it working with my Cannon SX200 + a custom CHDK remote trigger script. This means the actual scan time is now about 3/4 of an hour and I only get about 2 scans per camera charge, but! (and this is a big but) I get glorious 12 mega-pixel resolution and awesome image quality.

This has meant I’ve had to have a rather massive overhaul of the model generating software to reduce memory usage, add more controls to the GUI, remove the OpenCV dependency, and generally make it more usable. It’s not finished yet, but here’s is a look at an early version of the new GUI:

Still need to beautify the layout a bit and write a considerable chunk of code. But it hopefully should be worth it.

As always the code is open source and available from the repository, but be warned the current committed version is massively broken, so if you want a working version checkout revision 2 (yeah, I know it’s probably terrible practice to break the main repo’s code, but what the hell, it’s not like anyone besides me is actually using this stuff).

Timeslice Code Available

Set up a google code svn repo for the time slicing code for anyone interested:
https://code.google.com/p/videotimeslice/