2009-11-19 12 views
5

Ecco il mio codice corrente (la lingua è Python):Visualizzazione OpenCV strutture dati iplimage con wxPython

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

Sto cercando di visualizzare un iplimage catturata dalla webcam in una finestra wxPython. Il problema è che non voglio prima salvare l'immagine sul disco rigido. C'è un modo per convertire una iplimage in un altro formato di immagine in memoria? Qualche altra soluzione?

Ho trovato alcune "soluzioni" a questo problema in altre lingue, ma ho ancora problemi con questo problema.

Grazie.

risposta

1

si potrebbe fare con StringIO

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

è possibile controllare più in dettaglio nel \ wx \ lib \ embeddedimage.py

solo i miei 2 centesimi.

+0

Potresti elaborare un po '? Da dove vengono i dati? – Domenic

+0

ok, fammi provare a provarlo. resisti per un po '. – YOU

+0

Ho trovato che opencv non ha API per scrivere ImageData nello stream http://opencv.jp/opencv-1.0.0_org/docs/ref/opencvref_highgui.htm#highgui_func_index quindi trovare altri modi. – YOU

6

Quello che devi fare è:

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

ho capito OYT come fare questo guardando il Python OpenCV cookbook e al wxPython wiki.

+1

Sono consapevole che questo post ha un anno, ma è la più attuale domanda SO su Google per questo problema. – voyager

3

Sì, questa domanda è vecchia ma sono venuto qui come tutti gli altri che cercano la risposta. Diverse versioni di wx, numpy e opencv dopo le soluzioni di cui sopra ho pensato di condividere una soluzione veloce usando le immagini cv2 e numpy.

Questo è come convertire un'immagine in stile serie NumPy usati in OpenCV2 in una bitmap è quindi possibile impostare ad un elemento di visualizzazione in wxPython (ad oggi):

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) 

Testato 10 minuti fa :)

Questo utilizza la funzione integrata wx BitmapFromBuffer e sfrutta l'interfaccia del buffer NumPy in modo che tutto ciò che dobbiamo fare sia scambiare i colori per ottenere quelli nell'ordine previsto.