2009-11-19 78 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缓冲区接口,以便我们所要做的就是交换颜色以获得预期顺序的颜色。