您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

OpenCV与WXPython的集成

OpenCV与WXPython的集成

下面的示例代码在OS X上对我来说很好用,但是跨平台使用wx令我有些惊讶。它几乎是相同的代码,不同之处cvtColor在于重新分配了from的结果,并添加wx.Panel(是重要部分)的子类。

    import wx
    import cv, cv2

    class ShowCapture(wx.Panel):
        def __init__(self, parent, capture, fps=15):
            wx.Panel.__init__(self, parent)

            self.capture = capture
            ret, frame = self.capture.read()

            height, width = frame.shape[:2]
            parent.SetSize((width, height))
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

            self.bmp = wx.BitmapFromBuffer(width, height, frame)

            self.timer = wx.Timer(self)
            self.timer.Start(1000./fps)

            self.Bind(wx.EVT_PAINT, self.OnPaint)
            self.Bind(wx.EVT_TIMER, self.NextFrame)


        def OnPaint(self, evt):
            dc = wx.BufferedPaintDC(self)
            dc.DrawBitmap(self.bmp, 0, 0)

        def NextFrame(self, event):
            ret, frame = self.capture.read()
            if ret:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                self.bmp.CopyFromBuffer(frame)
                self.Refresh()


    capture = cv2.VideoCapture(0)
    capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, 320)
    capture.set(cv.CV_CAP_PROP_FRAME_HEIGHT, 240)

    app = wx.App()
    frame = wx.Frame(None)
    cap = ShowCapture(frame, capture)
    frame.Show()
    app.MainLoop()
python 2022/1/1 18:33:41 有349人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶