Advertisement
dan-masek

OpenCV + PyOpenGL

Apr 24th, 2018
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. import cv2
  2. from OpenGL.GL import *
  3. from OpenGL.GLU import *
  4. from OpenGL.GLUT import *
  5. import numpy as np
  6. import sys
  7.  
  8. # ============================================================================
  9.  
  10. VIDEO_SOURCE = "d:/jenkins/plants/output_640-400_1.mp4"
  11.  
  12. # ============================================================================
  13.  
  14. class OGLVideoView(object):
  15.     def __init__(self, capture):
  16.         self.capture = capture
  17.         self.width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
  18.         self.height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
  19.        
  20.         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
  21.         glutInitWindowSize(self.width, self.height)
  22.         glutInitWindowPosition(100, 100)
  23.         glutCreateWindow("OpenGL + OpenCV")
  24.        
  25.         self.init()
  26.        
  27.     def init(self):
  28.         glClearColor(0.0, 0.0, 0.0, 1.0)
  29.  
  30.         glutDisplayFunc(self.display)
  31.         glutReshapeFunc(self.reshape)
  32.         glutKeyboardFunc(self.keyboard)
  33.         glutIdleFunc(self.idle)
  34.        
  35.     def display(self):
  36.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  37.         glEnable(GL_TEXTURE_2D)
  38.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  39.  
  40.         # Set Projection Matrix
  41.         glMatrixMode(GL_PROJECTION)
  42.         glLoadIdentity()
  43.         gluOrtho2D(0, self.width, 0, self.height)
  44.  
  45.         # Switch to Model View Matrix
  46.         glMatrixMode(GL_MODELVIEW)
  47.         glLoadIdentity()
  48.  
  49.         # Draw textured Quads
  50.         glBegin(GL_QUADS)
  51.         glTexCoord2f(0.0, 0.0)
  52.         glVertex2f(0.0, 0.0)
  53.         glTexCoord2f(1.0, 0.0)
  54.         glVertex2f(self.width, 0.0)
  55.         glTexCoord2f(1.0, 1.0)
  56.         glVertex2f(self.width, self.height)
  57.         glTexCoord2f(0.0, 1.0)
  58.         glVertex2f(0.0, self.height)
  59.         glEnd()
  60.  
  61.         glFlush()
  62.         glutSwapBuffers()
  63.  
  64.     def reshape(self, w, h):
  65.         if h == 0:
  66.             h = 1
  67.  
  68.         glViewport(0, 0, w, h)
  69.         glMatrixMode(GL_PROJECTION)
  70.  
  71.         glLoadIdentity()
  72.         # allows for reshaping the window without distoring shape
  73.  
  74.         nRange = 1.0
  75.         if w <= h:
  76.             glOrtho(-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange)
  77.         else:
  78.             glOrtho(-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange)
  79.  
  80.         glMatrixMode(GL_MODELVIEW)
  81.         glLoadIdentity()
  82.        
  83.     def keyboard(self, key, x, y):
  84.         if key == chr(27):
  85.             sys.exit()
  86.        
  87.     def idle(self):
  88.         ret, image = self.capture.read()
  89.         if not ret:
  90.             print "Failed to read image"
  91.             sys.exit(-1)
  92.  
  93.         # Create Texture
  94.         glTexImage2D(GL_TEXTURE_2D ,0, GL_RGB
  95.             , self.width, self.height
  96.             , 0, GL_BGR, GL_UNSIGNED_BYTE
  97.             , np.flip(image, 0))
  98.         glutPostRedisplay()
  99.            
  100.     def main(self):
  101.         glutMainLoop()
  102.  
  103. # ============================================================================
  104.        
  105. if __name__ == "__main__":
  106.     capture = cv2.VideoCapture(VIDEO_SOURCE)
  107.     if not capture.isOpened():
  108.         sys.exit(-1)
  109.  
  110.     glutInit(sys.argv)
  111.    
  112.     view = OGLVideoView(capture)
  113.     view.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement