Skip to content

Commit ea05e62

Browse files
committed
Added cvQTcameraGL to the project.
1 parent 1cf38ed commit ea05e62

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ An application that shows how to create a Qt window to display videos loaded wit
1414
cvImage
1515
--------------
1616
A simple Qt/OpenCV example that displays an image and the RGB values of a pixel based on the mouse coordinates.
17+
18+
cvQTcameraGL
19+
--------------
20+
This application uses OpenCV to retrieve frames from the camera and display them on a QGLWidget.

cvQTcameraGL/cvQTcameraGL.pro

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
QT += core gui opengl
2+
3+
contains(QT_VERSION, ^5\\.[0-8]\\..*) {
4+
message("* Using Qt $${QT_VERSION}.")
5+
QT += widgets
6+
7+
# On my system I have to specify g++ as compiler else it will use clang++ by default
8+
#QMAKE_CXX=g++
9+
#QMAKE_CC=gcc
10+
}
11+
12+
SOURCES += \
13+
main.cpp \
14+
widget.cpp
15+
16+
HEADERS += \
17+
widget.h
18+
19+
## OpenCV settings for Unix/Linux
20+
unix:!mac {
21+
message("* Using settings for Unix/Linux.")
22+
INCLUDEPATH += /usr/local/include/opencv
23+
24+
LIBS += -L/usr/local/lib/ \
25+
-lopencv_core \
26+
-lopencv_highgui \
27+
-lopencv_imgproc
28+
}
29+
30+
## OpenCV settings for Mac OS X
31+
macx {
32+
message("* Using settings for Mac OS X.")
33+
INCLUDEPATH += /usr/local/include/opencv
34+
35+
LIBS += -L/usr/local/lib/ \
36+
-lopencv_core \
37+
-lopencv_highgui \
38+
-lopencv_imgproc
39+
}
40+
41+
## OpenCV settings for Windows and OpenCV 2.4.2
42+
win32 {
43+
message("* Using settings for Windows.")
44+
INCLUDEPATH += "C:\\opencv\\build\\include" \
45+
"C:\\opencv\\build\\include\\opencv" \
46+
"C:\\opencv\\build\\include\\opencv2"
47+
48+
LIBS += -L"C:\\opencv\\build\\x86\\vc10\\lib" \
49+
-lopencv_core242 \
50+
-lopencv_highgui242 \
51+
-lopencv_imgproc242
52+
}
53+

cvQTcameraGL/main.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <QApplication>
2+
#include "widget.h"
3+
4+
int main(int argc, char* argv[])
5+
{
6+
QApplication app(argc, argv);
7+
8+
// For OpenGL Graphics
9+
GLWidget gl_widget;
10+
gl_widget.show();
11+
12+
return app.exec();
13+
}
14+

cvQTcameraGL/widget.cpp

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include "widget.h"
2+
3+
#include <iostream>
4+
#include <QKeyEvent>
5+
#include <QTimer>
6+
7+
8+
GLWidget::GLWidget(QWidget *parent)
9+
: QGLWidget(parent)
10+
{
11+
_width = 0;
12+
_height = 0;
13+
_texture = 0;
14+
_fps = 0;
15+
}
16+
17+
GLWidget::~GLWidget()
18+
{
19+
glDeleteTextures(1, &_texture);
20+
}
21+
22+
void GLWidget::_tick()
23+
{
24+
// triggers paintGL()
25+
update();
26+
27+
// Set timer according to FPS
28+
QTimer::singleShot(1000/_fps, this, SLOT(_tick()));
29+
}
30+
31+
void GLWidget::initializeGL()
32+
{
33+
// Set clear color as black
34+
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
35+
36+
// Select pixel storage mode used by glTexImage2D
37+
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
38+
39+
// Create the texture
40+
glGenTextures(1, &_texture);
41+
42+
/* Open default camera device */
43+
44+
cv_capture.open(0);
45+
if (!cv_capture.isOpened())
46+
{
47+
std::cout << "GLWidget::initializeGL: !!! Failed to open camera" << std::endl;
48+
return;
49+
}
50+
51+
// Retrieve FPS from the camera
52+
_fps = cv_capture.get(CV_CAP_PROP_FPS);
53+
if (!_fps) // if the function fails, fps is set to 15
54+
_fps = 15;
55+
56+
std::cout << "GLWidget::initializeGL: " << _fps << " fps" << std::endl;
57+
58+
/* Start the timer */
59+
60+
_tick();
61+
}
62+
63+
void GLWidget::paintGL()
64+
{
65+
// Clear the screen and depth buffer (with black)
66+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
67+
68+
// Select the model view matrix and reset it
69+
glMatrixMode(GL_MODELVIEW);
70+
glLoadIdentity();
71+
72+
glTranslatef(0.0f, 0.0f, 0.0f);
73+
74+
// Abort drawing if OpenCV was unable to open the camera
75+
if (!cv_capture.isOpened())
76+
{
77+
std::cout << "GLWidget::paintGL: !!! Failed to open camera" << std::endl;
78+
return;
79+
}
80+
81+
// Note: trying to retrieve more frames than the camera can give you
82+
// will make the output video blink a lot.
83+
cv_capture >> cv_frame;
84+
if (cv_frame.empty())
85+
{
86+
std::cout << "GLWidget::paintGL: !!! Failed to retrieve frame" << std::endl;
87+
return;
88+
}
89+
cv::cvtColor(cv_frame, cv_frame, CV_BGR2RGBA);
90+
91+
glEnable(GL_TEXTURE_RECTANGLE_ARB);
92+
93+
// Typical texture generation using data from the bitmap
94+
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, _texture);
95+
96+
// Transfer image data to the GPU
97+
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0,
98+
GL_RGBA, cv_frame.cols, cv_frame.rows, 0,
99+
GL_RGBA, GL_UNSIGNED_BYTE, cv_frame.data);
100+
if (glGetError() != GL_NO_ERROR)
101+
{
102+
std::cout << "GLWidget::paintGL: !!! Failed glTexImage2D" << std::endl;
103+
}
104+
105+
// Draw a 2D face with texture
106+
glBegin(GL_QUADS);
107+
glTexCoord2f(0, 0); glVertex2f(1.0, 1.0);
108+
glTexCoord2f(cv_frame.cols, 0); glVertex2f(-1.0, 1.0);
109+
glTexCoord2f(cv_frame.cols, cv_frame.rows); glVertex2f(-1.0, -1.0);
110+
glTexCoord2f(0, cv_frame.rows); glVertex2f(1.0, -1.0);
111+
glEnd();
112+
113+
glDisable(GL_TEXTURE_RECTANGLE_ARB);
114+
}
115+
116+
void GLWidget::resizeGL( int w, int h)
117+
{
118+
_width = w;
119+
_height = h;
120+
121+
glViewport(0, 0, w, h);
122+
glMatrixMode(GL_PROJECTION); // Select the projection matrix
123+
glLoadIdentity(); // Reset the projection matrix
124+
if (h == 0) // Calculate aspect ratio of the window
125+
gluPerspective(60, (float) w, 1.0, 50.0);
126+
else
127+
gluPerspective(60, (float) w / (float) h, 1.0, 50.0);
128+
129+
gluLookAt(0.0, 0.0, 2.0, // eye
130+
0.0, 0.0, 0.0, // center
131+
0.0, 1.0, 0.0); // up
132+
133+
glMatrixMode(GL_MODELVIEW); // Select the model view matrix
134+
glLoadIdentity(); // Reset the model view matrix
135+
}

cvQTcameraGL/widget.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
#include <cv.h>
3+
#include <highgui.h>
4+
#include <QGLWidget>
5+
#include <QImage>
6+
7+
class GLWidget : public QGLWidget
8+
{
9+
Q_OBJECT
10+
public:
11+
explicit GLWidget(QWidget* parent = 0);
12+
virtual ~GLWidget();
13+
14+
/* OpenGL initialization, viewport resizing, and painting */
15+
16+
void initializeGL();
17+
void paintGL();
18+
void resizeGL( int width, int height);
19+
20+
private:
21+
int _width;
22+
int _height;
23+
cv::Mat cv_frame;
24+
cv::VideoCapture cv_capture;
25+
int _fps;
26+
GLuint _texture;
27+
28+
protected slots:
29+
void _tick();
30+
};

0 commit comments

Comments
 (0)