Monthly Archives: March 2011

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.

3D Scanner Mk 3 – Lasering Time!

The 3D scanner hasn’t made much progress in the past month or so, due mainly to me putting off doing the last bit of boring work to get it ready for laser cutting. After drinking a load of Tesco own brand energy drink last night I decided to get all the fiddly bits done, and so here is the final 3D model and the layout of the parts for cutting:

3D Scanner Model & Parts

The parts have now flown through the intertubes over to cutlasercut to get a quote for the cutting, and hopefully, if it doesn’t bankrupt me, I’ll be getting all the parts cut soon! wooo!

On a side note, after getting some good feedback on the introduction to markov chains I wrote I’m hoping to do another one that covers another interesting area of artificial intelligence in the near future.