5

这是我当前的代码(语言是 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()))

我正在尝试在 wxPython 窗口中显示从我的网络摄像头捕获的 iplimage。问题是我不想先将图像存储在硬盘上。有没有办法将 iplimage 转换为内存中的另一种图像格式?还有其他解决方案吗?

我在其他语言中找到了这个问题的一些“解决方案”,但我仍然遇到这个问题。

谢谢。

4

3 回答 3

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

通过查看Python OpenCV 食谱wxPython wiki,我想出了如何做到这一点。

于 2010-07-27T14:35:48.587 回答
3

是的,这个问题很老,但我像其他人一样来到这里寻找答案。在上述解决方案之后的几个版本的 wx、numpy 和 opencv 我想我会分享一个使用 cv2 和 numpy 图像的快速解决方案。

这是如何将 OpenCV2 中使用的 NumPy 数组样式图像转换为位图,然后您可以将其设置为 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 缓冲区接口,因此我们所要做的就是交换颜色以获得预期顺序的颜色。

于 2013-05-31T22:17:54.840 回答
1

你可以用 StringIO 做

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

您可以在 \wx\lib\embeddedimage.py 中查看更多详细信息

只是我的 2 美分。

于 2009-11-19T06:25:17.083 回答