OpenCV Python OpenGL纹理 [英] OpenCV Python OpenGL Texture

查看:388
本文介绍了OpenCV Python OpenGL纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在OpenCV提要上叠加3d对象.我在此处找到了一个示例,该示例在OpenGL中使用摄像头视频作为纹理.我更改了某些部分,使其可以与cv2一起使用.现在的输出有些奇怪

I want to overlay 3d objects on OpenCV feed. I found an example here That uses webcam video as texture in OpenGL. I changed some part so that it works with cv2. Now the output is something strange

代码

  import cv2
  from OpenGL.GL import *
  from OpenGL.GLU import *
  from OpenGL.GLUT import *
  import numpy as np
  import sys


  #window dimensions
  width = 1280
  height = 720
  nRange = 1.0

  global capture
  capture = None

  def cv2array(im): 
    h,w,c=im.shape
    a = np.fromstring( 
       im.tostring(), 
       dtype=im.dtype, 
       count=w*h*c) 
    a.shape = (h,w,c) 
    return a

  def init():
    #glclearcolor (r, g, b, alpha)
    glClearColor(0.0, 0.0, 0.0, 1.0)

    glutDisplayFunc(display)
    glutReshapeFunc(reshape)
    glutKeyboardFunc(keyboard)
    glutIdleFunc(idle)  

  def idle():
    #capture next frame

    global capture
    _,image = capture.read()


    cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    #you must convert the image to array for glTexImage2D to work
    #maybe there is a faster way that I don't know about yet...

    #print image_arr


    # Create Texture
    glTexImage2D(GL_TEXTURE_2D, 
      0, 
      GL_RGB, 
      720,1280,
      0,
      GL_RGB, 
      GL_UNSIGNED_BYTE, 
      image)
    cv2.imshow('frame',image)
    glutPostRedisplay()

  def display():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glEnable(GL_TEXTURE_2D)
    #glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    #glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    #glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
    #this one is necessary with texture2d for some reason
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)

    # Set Projection Matrix
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluOrtho2D(0, width, 0, height)

    # Switch to Model View Matrix
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    # Draw textured Quads
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 0.0)
    glVertex2f(0.0, 0.0)
    glTexCoord2f(1.0, 0.0)
    glVertex2f(width, 0.0)
    glTexCoord2f(1.0, 1.0)
    glVertex2f(width, height)
    glTexCoord2f(0.0, 1.0)
    glVertex2f(0.0, height)
    glEnd()

    glFlush()
    glutSwapBuffers()

  def reshape(w, h):
    if h == 0:
      h = 1

    glViewport(0, 0, w, h)
    glMatrixMode(GL_PROJECTION)

    glLoadIdentity()
    # allows for reshaping the window without distoring shape

    if w <= h:
      glOrtho(-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange)
    else:
      glOrtho(-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

  def keyboard(key, x, y):
    global anim
    if key == chr(27):
      sys.exit()

  def main():
    global capture
    #start openCV capturefromCAM
    capture = cv2.VideoCapture(0)
    print capture
    capture.set(3,1280)
    capture.set(4,720)
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
    glutInitWindowSize(width, height)
    glutInitWindowPosition(100, 100)
    glutCreateWindow("OpenGL + OpenCV")

    init()
    glutMainLoop()

  main()

推荐答案

我发现您的相机不太可能提供720像素宽和1280像素高的图像,就像您在此处告诉OpenGL的那样:

I find it very unlikely that your camera is providing a 720 pixel wide and 1280 pixel high image as you tell to OpenGL here:

glTexImage2D(GL_TEXTURE_2D, 
      0, 
      GL_RGB, 
      720,1280,    // <---- HERE
      0,
      GL_RGB, 
      GL_UNSIGNED_BYTE, 
      image)

似乎您只是将这两个参数混合在一起,因此数据将被重新解释,从而得到您所得到的输出.

It seems you just mixed up those two parametes, so the data is reinterpreted as such, resulting in the output you got.

这篇关于OpenCV Python OpenGL纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆