No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

opengl_opencv.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. # window dimensions
  8. width = 1280
  9. height = 720
  10. nRange = 1.0
  11. global capture
  12. capture = None
  13. def cv2array(im):
  14. h, w, c = im.shape
  15. a = np.fromstring(
  16. im.tostring(),
  17. dtype=im.dtype,
  18. count=w * h * c)
  19. a.shape = (h, w, c)
  20. return a
  21. def init():
  22. # glclearcolor (r, g, b, alpha)
  23. glClearColor(0.0, 0.0, 0.0, 1.0)
  24. glutDisplayFunc(display)
  25. glutReshapeFunc(reshape)
  26. glutKeyboardFunc(keyboard)
  27. glutIdleFunc(idle)
  28. def idle():
  29. # capture next frame
  30. global capture
  31. _, image = capture.read()
  32. cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  33. # you must convert the image to array for glTexImage2D to work
  34. # maybe there is a faster way that I don't know about yet...
  35. # print image_arr
  36. # Create Texture
  37. glTexImage2D(GL_TEXTURE_2D,
  38. 0,
  39. GL_RGB,
  40. 1280, 720,
  41. 0,
  42. GL_RGB,
  43. GL_UNSIGNED_BYTE,
  44. image)
  45. cv2.imshow('frame', image)
  46. glutPostRedisplay()
  47. def display():
  48. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  49. glEnable(GL_TEXTURE_2D)
  50. # glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
  51. # glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
  52. # glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
  53. # this one is necessary with texture2d for some reason
  54. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  55. # Set Projection Matrix
  56. glMatrixMode(GL_PROJECTION)
  57. glLoadIdentity()
  58. gluOrtho2D(0, width, 0, height)
  59. # Switch to Model View Matrix
  60. glMatrixMode(GL_MODELVIEW)
  61. glLoadIdentity()
  62. # Draw textured Quads
  63. glBegin(GL_QUADS)
  64. glTexCoord2f(0.0, 0.0)
  65. glVertex2f(0.0, 0.0)
  66. glTexCoord2f(1.0, 0.0)
  67. glVertex2f(width, 0.0)
  68. glTexCoord2f(1.0, 1.0)
  69. glVertex2f(width, height)
  70. glTexCoord2f(0.0, 1.0)
  71. glVertex2f(0.0, height)
  72. glEnd()
  73. glFlush()
  74. glutSwapBuffers()
  75. def reshape(w, h):
  76. if h == 0:
  77. h = 1
  78. glViewport(0, 0, w, h)
  79. glMatrixMode(GL_PROJECTION)
  80. glLoadIdentity()
  81. # allows for reshaping the window without distoring shape
  82. if w <= h:
  83. glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange, nRange)
  84. else:
  85. glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange, nRange)
  86. glMatrixMode(GL_MODELVIEW)
  87. glLoadIdentity()
  88. def keyboard(key, x, y):
  89. global anim
  90. if key == chr(27):
  91. sys.exit()
  92. def main():
  93. global capture
  94. # start openCV capturefromCAM
  95. capture = cv2.VideoCapture(0)
  96. print(capture)
  97. capture.set(3, 1280)
  98. capture.set(4, 720)
  99. glutInit(sys.argv)
  100. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
  101. glutInitWindowSize(width, height)
  102. glutInitWindowPosition(100, 100)
  103. glutCreateWindow("OpenGL + OpenCV")
  104. init()
  105. glutMainLoop()
  106. main()