Mapping OpenCV iplimage data structures using wxPython

Here is my current code (Python language):

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 am trying to display iplimage taken from my webcam in a wxPython window. The problem is that I do not want to save the image on my hard drive first. Is there a way to convert iplimage to another image format in memory? Any other solution?

I found several β€œsolutions” for this problem in other languages, but I still have problems with this problem.

Thanks.

+5
source share
3 answers

You can do with StringIO

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

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

only my 2 cents.

+1
source

:

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()))

, , Python OpenCV wxPython wiki.

+6

, , , , . wx, numpy opencv , cv2 numpy.

NumPy, OpenCV2, , wxPython ( ):

import wx, cv2
import numpy as np

# Start with a numpy array style image I'll call "source"

# convert the colorspace to RGB from cv2 standard BGR, ensure input is uint8
img = cv2.cvtColor(np.uint8(source), cv2.cv.CV_BGR2RGB) 

# get the height and width of the source image for buffer construction
h, w = img.shape[:2]

# make a wx style bitmap using the buffer converter
wxbmp = wx.BitmapFromBuffer(w, h, img)

# Example of how to use this to set a static bitmap element called "bitmap_1"
self.bitmap_1.SetBitmap(wxbmp)

10 :)

wx BitmapFromBuffer NumPy, , , , .

+3

All Articles