views:

463

answers:

2

Here is my current code (language is Python):

newFrameImage = cv.QueryFrame(webcam)
newFrameImageFile = cv.SaveImage("temp.jpg",newFrameImage)
wxImage = wx.Image("temp.jpg", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
wx.StaticBitmap(self, -1, wxImage, (0,0), (wxImage.GetWidth(), wxImage.GetHeight()))

I'm trying to display an iplimage captured from my webcam in a wxPython window. The problem is I don't want to store the image on hard disk first. Is there any way to convert an iplimage into another image format in memory? Any other solution?

I found a few "solutions" to this problem in other languages, but I'm still having trouble with this issue.

Thanks.

A: 

You could do with StringIO

stream = cStringIO.StringIO(data)
wxImage = wx.ImageFromStream(stream)

you can check more detail in \wx\lib\embeddedimage.py

just my 2 cents.

S.Mark
Could you elaborate a little? Where does data come from?
Domenic
ok, let me try to test it. hold on for a while.
S.Mark
I have found that opencv does not have api to write ImageData into stream http://opencv.jp/opencv-1.0.0_org/docs/ref/opencvref_highgui.htm#highgui_func_indexso finding some other ways.
S.Mark
Thank you for even trying, I appreciate it
Domenic
Seems there is direct converter from ipl images to wxImage http://www.koders.com/cpp/fid4A270123EB2BF8B15C8F6F620770EA6D2821F509.aspx?s=opencvbut seems like we need to port to python ourselves because its c++, * IPL image structure is seems based on RGB format too. http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00041000000000000000
S.Mark
Thanks, that looks like it will work
Domenic
A: 

What you have to do is:

frame = cv.QueryFrame(self.cam) # Get the frame from the camera
cv.CvtColor(frame, frame, cv.CV_BGR2RGB) # Color correction
                         # if you don't do this your image will be greenish
wxImage = wx.EmptyImage(frame.width, frame.height) # If your camera doesn't give 
                         # you the stream size, you might have to use (640, 480)
wxImage.SetData(frame.tostring()) # convert from cv.iplimage to wxImage
wx.StaticBitmap(self, -1, wxImage, (0,0), 
                (wxImage.GetWidth(), wxImage.GetHeight()))

I figured oyt out how to do this by looking at the Python OpenCV cookbook and at the wxPython wiki.

voyager
I'm well aware that this post is a year old, but it's the current highest ranking SO question on Google for this problem.
voyager