2009-11-19 77 views
5

的IplImage數據結構這是我當前的代碼(語言是Python):顯示的OpenCV與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())) 

我想顯示從我在wxPython的窗口的攝像頭拍攝的IplImage結構。問題是我不想先將圖像存儲在硬盤上。有沒有辦法將iplimage轉換成內存中的另一種圖像格式?其他解決方案?

我在其他語言中發現了這個問題的一些「解決方案」,但我仍然遇到了這個問題。

謝謝。

回答

1

你可以用StringIO

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

你可以檢查\ WX \ LIB \ embeddedimage.py

只是我的2美分的更多細節。

+0

你能詳細一點嗎?數據來自哪裏? – Domenic 2009-11-19 06:46:56

+0

好吧,讓我試着去測試它。堅持一會兒。 – YOU 2009-11-19 07:00:08

+0

我發現opencv沒有將ImageData寫入流http://opencv.jp/opencv-1.0.0_org/docs/ref/opencvref_highgui.htm#highgui_func_index ,所以找到其他方法。 – YOU 2009-11-19 07:33:59

6

你所要做的是:

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

我想通oyt如何通過看Python OpenCV cookbook並在wxPython wiki做到這一點。

+1

我很清楚,這篇文章已經有一年了,但它是目前Google上針對這個問題排名最高的SO問題。 – voyager 2010-07-27 14:37:58

3

是的,這個問題很老,但我像其他人一樣來到這裏尋找答案。上述解決方案之後,我認爲我會分享一個快速解決方案,使用cv2和numpy圖像的幾個版本的wx,numpy和opencv。

這是怎樣一個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) 

測試11分鐘前:)

這使用內置的wx函數BitmapFromBuffer並利用NumPy緩衝區接口,以便我們所要做的就是交換顏色以獲得預期順序的顏色。