Python Image Processing (3): Video Display and python Image Processing

Source: Internet
Author: User

Python Image Processing (3): Video Display and python Image Processing

Happy shrimp

Http://blog.csdn.net/lights_joy/ (QQ group: Visual EmbedLinux Tools 375515651)

Reprinted, but keep the author information



1. Use OpenCV for video display


Previously, we used OpenCV to make a simple video display:


#-*-Coding: UTF-8-*-# Use OpenCV to play the Video import cv2wnd = 'opencv video' # obtain the Video format videoCapture = cv2.VideoCapture ('f: \ tmp \ cotton.mp4 ') # obtain the bit rate and size fps = videoCapture. get (cv2.cv. CV_CAP_PROP_FPS) size = (int (videoCapture. get (cv2.cv. CV_CAP_PROP_FRAME_WIDTH), int (videoCapture. get (cv2.cv. CV_CAP_PROP_FRAME_HEIGHT) cv2.namedWindow (wnd, flags = 0) cv2.resizeWindow (wnd, size [0]/2, size [1]/2) # Read frame success, frame = videoCapture. read () while success: cv2.imshow (wnd, frame) # Show cv2.waitKey (1000/int (fps) # delay success, frame = videoCapture. read () # obtain the next frame

Cv2 is used completely for video reading and display. This program has two problems: one is that the cv2 Window Scaling Problem leads to video image deformation, and the other is that the interface control function provided by opencv is weak, even it does not provide the ability to display buttons on the interface, so we can try to replace the display part of the interface.


2. Build the interface with wxFormBuilder


WxFormBuilder is a UI design tool that uses wxWidgets. It can intuitively design the UI and generate code related to C ++/Python/Lua/Xrc. We use it to build the video player interface:



Leave a Panel control on the main window to embed the matplotlib window.


WxFormBuilder automatically generates Python code:


#-*-Coding: UTF-8 -*-#################################### ######################################## # Python code generated with wxFormBuilder (version Jun 5 2014) # http://www.wxformbuilder.org/#### please do "NOT" edit this file! ######################################## ################################### Import wximport wx. xrc ####################################### ###################################### Class MainFrame ######################################## ################################### class MainFrame (wx. frame): def _ init _ (self, parent): wx. frame. _ init _ (self, parent, id = wx. ID_ANY, title = u "Video Player", pos = wx. defaultPosition, size = wx. size (757,562), style = wx. DEFAULT_FRAME_STYLE | wx. TAB_TRAVERSAL) self. setSizeHintsSz (wx. defaultSize, wx. defaultSize) self. m_timer = wx. timer () self. m_timer.SetOwner (self, wx. ID_ANY) self. m_timer.Start (40) MainSizer = wx. boxSizer (wx. VERTICAL) ButtonSizer = wx. boxSizer (wx. HORIZONTAL) self. m_btnStart = wx. button (self, wx. ID_ANY, u "start", wx. defaultPosition, wx. defaultSize, 0) ButtonSizer. add (self. m_btnStart, 0, wx. ALL, 5) self. m_btnStop = wx. button (self, wx. ID_ANY, u "stop", wx. defaultPosition, wx. defaultSize, 0) ButtonSizer. add (self. m_btnStop, 0, wx. ALL, 5) MainSizer. add (ButtonSizer, 0, wx. EXPAND, 5) PanelSizer = wx. staticBoxSizer (wx. staticBox (self, wx. ID_ANY, u "Matplotlib control zone"), wx. VERTICAL) self. m_pnlMatplot = wx. panel (self, wx. ID_ANY, wx. defaultPosition, wx. size (-1,-1), 0) MatplotSizer = wx. boxSizer (wx. VERTICAL) self. m_pnlMatplot.SetSizer (MatplotSizer) self. m_pnlMatplot.Layout () MatplotSizer. fit (self. m_pnlMatplot) PanelSizer. add (self. m_pnlMatplot, 1, wx. EXPAND | wx. ALL, 5) MainSizer. add (PanelSizer, 1, wx. EXPAND, 5) self. setSizer (MainSizer) self. layout () self. centre (wx. BOTH) # Connect Eventsself. bind (wx. EVT_TIMER, self. onTimer, id = wx. ID_ANY) self. m_btnStart.Bind (wx. EVT_BUTTON, self. onStart) self. m_btnStop.Bind (wx. EVT_BUTTON, self. onStop) def _ del _ (self): pass # Virtual event handlers, overide them in your derived classdef OnTimer (self, event): event. skip () def OnStart (self, event): event. skip () def OnStop (self, event): event. skip ()

Directly create a new py file in this player project, and then copy this code to our player project, plus App and event loop:

app = wx.App()frame = MainFrame(None)frame.Show()app.MainLoop()

3. Encoding Problems


In the previous section, a new py file is directly created in the python project and the code generated by wxFormBuilder is copied. The result is miserable:



Change the file encoding to UTF-8:


Run it again:


4. Add matplotlib to display


Add the matplotlib display function.


First, create the canvas required by matplotlib during Frame Initialization:


        self.mpl_control = FigureCanvas(self.m_pnlMatplot, -1, plt.gcf())        MatplotSizer.Add(self.mpl_control, 1, wx.LEFT | wx.TOP | wx.GROW)

Open the file and timer at the beginning of the Press:

    def OnStart( self, event ):        self.videoCapture = cv2.VideoCapture('f:\\tmp\\cotton.mp4')        if(self.videoCapture == None):            wx.SafeShowMessage('start', 'Open Failed')            return        self.fps = self.videoCapture.get(cv2.cv.CV_CAP_PROP_FPS)        self.m_timer.Start(100)

Stop the timer when you press stop:

    def OnStop( self, event ):        self.m_timer.Stop()

Drawing in a timer event:

    def OnTimer( self, event ):        success, self.frame = self.videoCapture.read()        if(success) :            plt.imshow(self.frame)            self.mpl_control.draw_idle()

The running result is as follows:



5. Memory leakage


In the above Code, there is a serious memory leakage during running, and the memory usage keeps increasing during video playback.


Check the imshow code:

    @docstring.dedent_interpd    def imshow(self, X, cmap=None, norm=None, aspect=None,               interpolation=None, alpha=None, vmin=None, vmax=None,               origin=None, extent=None, shape=None, filternorm=1,               filterrad=4.0, imlim=None, resample=None, url=None, **kwargs):        """        Display an image on the axes.......        """......        im = mimage.AxesImage(self, cmap, norm, interpolation, origin, extent,                       filternorm=filternorm,                       filterrad=filterrad, resample=resample, **kwargs)        im.set_data(X)        im.set_alpha(alpha)        if im.get_clip_path() is None:            # image does not already have clipping set, clip to axes patch            im.set_clip_path(self.patch)        #if norm is None and shape is None:        #    im.set_clim(vmin, vmax)        if vmin is not None or vmax is not None:            im.set_clim(vmin, vmax)        else:            im.autoscale_None()        im.set_url(url)        # update ax.dataLim, and, if autoscaling, set viewLim        # to tightly fit the image, regardless of dataLim.        im.set_extent(im.get_extent())        self.add_image(im)        return im

Use


Self. add_image (im)


Add the created image to the image list, instead of replacing the existing image, which leads to increasing memory! After knowing the cause, it is easy to handle. modify our code and delete the previous image before displaying the image:

    # Virtual event handlers, overide them in your derived class    def OnTimer( self, event ):        success, self.frame = self.videoCapture.read()        if(success) :            if(self.image != None) :                self.image.remove()            (b, g, r) = cv2.split(self.frame)            self.frame = cv2.merge([r,g,b])            self.image = plt.imshow(self.frame)            self.mpl_control.draw_idle()

Done!

















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.