opengl 缩放 预览摄像头

目录

opengl安装:

有的报错:

opengl pyqt渲染缩放

opengl 和控件分屏

opengl旋转

android预览摄像头:


opengl安装:

pip install -U pyopengl

有的报错:

OpenGL.error.NullFunctionError: Attempt to call an undefined function glutIn

问题:使用 pip 安装了 PyOpenGL 包,然后运行程序,结果提示:OpenGL.error.NullFunctionError: Attempt to call an undefined function glutInit

原因:使用 pip 安装的 OpenGL 包是 32 位,与 64 位电脑不匹配,故出现此错误。

解决办法:pip 不能在线安装 64 位的 OpenGL,只能手动下载后安装。
下载地址:
https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl

opengl pyqt渲染缩放

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *

class OpenGLWidget(QGLWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(200, 200)
        self.scale_factor = 1.0

    def initializeGL(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        glScalef(self.scale_factor, self.scale_factor, 1.0)
        # 绘制一个简单的三角形
        glBegin(GL_TRIANGLES)
        glColor3f(1.0, 0.0, 0.0)
        glVertex2f(0.0, 1.0)
        glColor3f(0.0, 1.0, 0.0)
        glVertex2f(-1.0, -1.0)
        glColor3f(0.0, 0.0, 1.0)
        glVertex2f(1.0, -1.0)
        glEnd()

    def resizeGL(self, w, h):
        glViewport(0, 0, w, h)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0)
        glMatrixMode(GL_MODELVIEW)

    def wheelEvent(self, event):
        # 获取滚轮滚动的距离
        num_degrees = event.angleDelta().y() / 8
        num_steps = num_degrees / 15.0  # 每次缩放15度
        self.scale_factor *= pow(1.1, num_steps)  # 乘以一个缩放因子
        self.update()

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("OpenGL Widget Example")
        self.setGeometry(100, 100, 400, 400)
        self.gl_widget = OpenGLWidget(self)
        self.setCentralWidget(self.gl_widget)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

opengl 和控件分屏

import sys

from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QSplitter, QLabel
from PyQt5.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *


class OpenGLWidget(QGLWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(200, 200)
        self.scale_factor = 1.0

    def initializeGL(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        glScalef(self.scale_factor, self.scale_factor, 1.0)
        # 绘制一个简单的三角形
        glBegin(GL_TRIANGLES)
        glColor3f(1.0, 0.0, 0.0)
        glVertex2f(0.0, 1.0)
        glColor3f(0.0, 1.0, 0.0)
        glVertex2f(-1.0, -1.0)
        glColor3f(0.0, 0.0, 1.0)
        glVertex2f(1.0, -1.0)
        glEnd()

    def resizeGL(self, w, h):
        glViewport(0, 0, w, h)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0)
        glMatrixMode(GL_MODELVIEW)

    def wheelEvent(self, event):
        # 获取滚轮滚动的距离
        num_degrees = event.angleDelta().y() / 8
        num_steps = num_degrees / 15.0  # 每次缩放15度
        self.scale_factor *= pow(1.1, num_steps)  # 乘以一个缩放因子
        self.update()


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("OpenGL Widget Example")
        self.setGeometry(100, 100, 400, 400)
        self.gl_widget = OpenGLWidget(self)

        splitter = QSplitter()
        self.setCentralWidget(splitter)

        splitter.addWidget(self.gl_widget)
        self.image_label = QLabel()
        pixmap = QPixmap(r"C:\Users\Administrator\Pictures\mm\pics\005953_4.jpg")  # 替换为你的图片路径
        self.image_label.setPixmap(pixmap)
        splitter.addWidget(self.image_label)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

opengl旋转

import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QMouseEvent
from PyQt5.QtWidgets import QApplication, QMainWindow, QSplitter, QLabel
from PyQt5.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *


class OpenGLWidget(QGLWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setMinimumSize(200, 200)
        self.scale_factor = 1.0

        self.x_rotation = 0.0
        self.y_rotation = 0.0
        self.z_rotation = 0.0
        self.last_pos = None

    def initializeGL(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)

    def paintGL(self):
        glClear(GL_COLOR_BUFFER_BIT)
        glLoadIdentity()
        glScalef(self.scale_factor, self.scale_factor, 1.0)
        # glTranslatef(0.0, 0.0, -5.0)
        glRotatef(self.x_rotation, 1.0, 0.0, 0.0)
        glRotatef(self.y_rotation, 0.0, 1.0, 0.0)
        glRotatef(self.z_rotation, 0.0, 0.0, 1.0)

        glBegin(GL_TRIANGLES)
        glColor3f(1.0, 0.0, 0.0)
        glVertex2f(0.0, 1.0)
        glColor3f(0.0, 1.0, 0.0)
        glVertex2f(-1.0, -1.0)
        glColor3f(0.0, 0.0, 1.0)
        glVertex2f(1.0, -1.0)
        glEnd()

    def resizeGL(self, w, h):
        glViewport(0, 0, w, h)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 1.0)
        glMatrixMode(GL_MODELVIEW)

    def wheelEvent(self, event):
        num_degrees = event.angleDelta().y() / 8
        num_steps = num_degrees / 15.0  # 每次缩放15度
        self.scale_factor *= pow(1.1, num_steps)  # 乘以一个缩放因子
        self.update()
    def mousePressEvent(self, event: QMouseEvent):
        self.last_pos = event.pos()

    def mouseMoveEvent(self, event: QMouseEvent):
        if self.last_pos is None:
            return
        dx = event.x() - self.last_pos.x()
        dy = event.y() - self.last_pos.y()
        if event.buttons() & Qt.LeftButton:
            self.x_rotation += dy * 0.5
            self.y_rotation += dx * 0.5
        elif event.buttons() & Qt.RightButton:
            self.x_rotation += dy * 0.5
            self.z_rotation += dx * 0.5
        self.last_pos = event.pos()
        self.update()

    def mouseReleaseEvent(self, event: QMouseEvent):
        self.last_pos = None

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("OpenGL Widget Example")
        self.setGeometry(100, 100, 800, 400)
        self.gl_widget = OpenGLWidget(self)

        splitter = QSplitter()
        self.setCentralWidget(splitter)

        splitter.addWidget(self.gl_widget)
        self.image_label = QLabel()
        self.image_label.setFixedSize(400, 400)
        pixmap = QPixmap(r"C:\Users\Administrator\Pictures\mm\pics\005953_4.jpg")  # 替换为你的图片路径
        self.image_label.setPixmap(pixmap)
        splitter.addWidget(self.image_label)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

android预览摄像头:

GitHub - ChinaZeng/OpenGLESCameraDemo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在OpenGL中显示摄像头视频,你需要完成以下步骤: 1. 打开摄像头并捕获视频帧 2. 将捕获的帧上传到纹理中 3. 在OpenGL中绘制纹理 下面是一个示例代码片段,可以帮助你开始: ```c++ #include <opencv2/opencv.hpp> #include <GL/glut.h> using namespace cv; GLuint textureID; // 纹理ID Mat frame; // 视频帧 void display() { glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, textureID); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0); glTexCoord2f(1.0, 0.0); glVertex2f(1.0, -1.0); glTexCoord2f(1.0, 1.0); glVertex2f(1.0, 1.0); glTexCoord2f(0.0, 1.0); glVertex2f(-1.0, 1.0); glEnd(); glFlush(); glutSwapBuffers(); } void init() { glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); // 设置纹理参数 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // 加载空白纹理 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, frame.cols, frame.rows, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, frame.data); } void captureFrame() { VideoCapture cap(0); // 打开默认摄像头 cap >> frame; } void timer(int) { captureFrame(); // 将帧上传到纹理中 glBindTexture(GL_TEXTURE_2D, textureID); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, frame.cols, frame.rows, GL_BGR_EXT, GL_UNSIGNED_BYTE, frame.data); glutPostRedisplay(); glutTimerFunc(1000/60, timer, 0); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); glutInitWindowSize(640, 480); glutCreateWindow("Camera Viewer"); init(); glutDisplayFunc(display); glutTimerFunc(0, timer, 0); glutMainLoop(); return 0; } ``` 这段代码使用OpenCV捕获默认摄像头的视频帧,并将其上传到OpenGL中的纹理中。在 `display` 函数中,我们使用纹理来绘制一个矩形,并在每一帧结束时交换缓冲区。在 `timer` 函数中,我们使用定时器来捕获新的视频帧并更新纹理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值