Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 41af3f1

Browse files
committed
Added cvVideo to the repository.
1 parent 25cbd33 commit 41af3f1

File tree

5 files changed

+368
-1
lines changed

5 files changed

+368
-1
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
GraphicsProgramming
22
===================
33

4-
Demos related to OpenGL, Qt and other X technologies.
4+
Demos related to **OpenGL**, **Qt**, **OpenCV** and other X technologies.
5+
6+
IngressLogoGL
7+
--------------
8+
This is a OpenGL/Qt demo that renders the Ingress logo on a texture and performs a blur to add a *cool* effect to the logo.
9+
10+
cvVideo
11+
--------------
12+
An application that shows how to create a Qt window to display videos loaded with OpenCV.

cvVideo/cvVideo.pro

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
TEMPLATE = app
2+
3+
QT += core gui
4+
5+
HEADERS += \
6+
cvWindow.h
7+
8+
SOURCES += \
9+
main.cpp \
10+
cvWindow.cpp
11+
12+
## OpenCV settings for Mac OS X
13+
macx {
14+
INCLUDEPATH += /usr/local/include/opencv
15+
16+
LIBS += -L/usr/local/lib/ \
17+
-lopencv_core \
18+
-lopencv_highgui \
19+
-lopencv_imgproc
20+
}
21+
22+
# Runs this app automatically after the building has succeeded
23+
#QMAKE_POST_LINK=./$$TARGET

cvVideo/cvWindow.cpp

Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
#include "cvWindow.h"
2+
3+
#include <QDebug>
4+
#include <QPainter>
5+
#include <QFileDialog>
6+
#include <QTimer>
7+
#include <QApplication>
8+
9+
#include <cv.h>
10+
11+
12+
cvWindow::cvWindow()
13+
: _menu(NULL), _image(NULL), _capture(NULL), _tick_ms(33), _ar_mode(Qt::IgnoreAspectRatio),
14+
_fps(0), _video_width(0), _video_height(0), _timer(NULL)
15+
{
16+
setWindowTitle(tr("QT Video demo with OpenCV"));
17+
resize(480, 240);
18+
19+
_menu = new QMenu("File");
20+
_menu->addAction("Open", this, SLOT(_open()));
21+
_menu->addSeparator();
22+
_menu->addAction("Exit");
23+
_menu->addAction("Exit", this, SLOT(close()));
24+
_menu_bar.addMenu(_menu);
25+
}
26+
27+
cvWindow::~cvWindow()
28+
{
29+
if (_timer)
30+
{
31+
_timer->stop();
32+
delete _timer;
33+
}
34+
35+
if (_menu)
36+
delete _menu;
37+
38+
if (_image)
39+
delete _image;
40+
41+
if (_capture)
42+
{
43+
_capture->release();
44+
delete _capture;
45+
}
46+
}
47+
48+
void cvWindow::_tick()
49+
{
50+
/* This method is called every couple of milliseconds.
51+
* It reads from the OpenCV's capture interface and saves a frame as QImage
52+
*/
53+
cv::Mat frame;
54+
if (!_capture->read(frame))
55+
{
56+
qDebug() << "cvWindow::_tick !!! Failed to read frame from the capture interface";
57+
}
58+
59+
// Since OpenCV uses BGR order, we need to convert it to RGB
60+
cv::cvtColor(frame, frame, CV_BGR2RGB);
61+
62+
// Copy cv::Mat to QImage
63+
qMemCopy(_image->scanLine(0), (unsigned char*)frame.data, _image->width() * _image->height() * frame.channels());
64+
65+
// The same as above, but much worst.
66+
//QImage img = QImage((uchar*) frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
67+
//*_image = img.copy();
68+
69+
// Trigger paint event to redraw the window
70+
update();
71+
}
72+
73+
void cvWindow::paintEvent(QPaintEvent* e)
74+
{
75+
QPainter painter(this);
76+
77+
// When no image is loaded, paint the window with black
78+
if (!_image)
79+
{
80+
painter.fillRect(QRectF(QPoint(0, 0), QSize(width(), height())), Qt::black);
81+
QWidget::paintEvent(e);
82+
return;
83+
}
84+
85+
// Draw a frame from the video
86+
_draw_video_frame(painter);
87+
88+
// Draw text with info about the video
89+
_draw_info_text(painter);
90+
91+
QWidget::paintEvent(e);
92+
}
93+
94+
void cvWindow::_draw_video_frame(QPainter& painter)
95+
{
96+
// To simply draw on the window, execute the code below.
97+
//painter.drawImage(QPoint(0, 0), *_image);
98+
99+
// But we are drawing the image according to AR settings:
100+
// http://doc.qt.digia.com/stable/qt.html#AspectRatioMode-enum
101+
switch (_ar_mode)
102+
{
103+
default:
104+
case Qt::IgnoreAspectRatio:
105+
{
106+
painter.drawImage(QRectF(QPoint(0, 0), QSize(width(), height())), *_image, QRectF(QPoint(0, 0), _image->size()));
107+
}
108+
break;
109+
110+
case Qt::KeepAspectRatio:
111+
{
112+
painter.fillRect(QRectF(QPoint(0, 0), QSize(width(), height())), Qt::black);
113+
114+
QImage scaled_img = _image->scaled(QSize(width(), height()), Qt::KeepAspectRatio, Qt::FastTransformation);
115+
116+
painter.drawImage(qRound(width()/2) - qRound(scaled_img.size().width()/2),
117+
qRound(height()/2) - qRound(scaled_img.size().height()/2),
118+
scaled_img);
119+
}
120+
break;
121+
122+
case Qt::KeepAspectRatioByExpanding:
123+
{
124+
QImage scaled_img = _image->scaled(QSize(width(), height()), Qt::KeepAspectRatioByExpanding, Qt::FastTransformation);
125+
126+
painter.drawImage(QRectF(QPoint(0, 0), QSize(width(), height())),
127+
scaled_img,
128+
QRectF(QPoint(qRound(scaled_img.size().width()/2) - qRound(width()/2),
129+
qRound(scaled_img.size().height()/2) - qRound(height()/2)),
130+
QSize(width(), height())));
131+
}
132+
break;
133+
}
134+
}
135+
136+
void cvWindow::_draw_info_text(QPainter& painter)
137+
{
138+
// Setup font properties
139+
painter.setFont(QFont("Arial", 16));
140+
painter.setPen(QPen(QColor("#2e8b57")));
141+
142+
// Draw info about video dimensions
143+
QString video_size = QString::number(_video_width) + "x" + QString::number(_video_height) ;
144+
painter.drawText(QRect(10, 6, 100, 100), Qt::AlignLeft, video_size);
145+
146+
// Draw info about FPS
147+
QString fps = "FPS: " + QString::number(_fps);
148+
painter.drawText(QRect(10, 22, 100, 100), Qt::AlignLeft, fps);
149+
150+
// Draw selected AR mode
151+
switch (_ar_mode)
152+
{
153+
case Qt::IgnoreAspectRatio:
154+
painter.drawText(QRect(10, 37, 300, 100), Qt::AlignLeft, "IgnoreAspectRatio");
155+
break;
156+
157+
case Qt::KeepAspectRatio:
158+
painter.drawText(QRect(10, 37, 300, 100), Qt::AlignLeft, "KeepAspectRatio");
159+
break;
160+
161+
case Qt::KeepAspectRatioByExpanding:
162+
painter.drawText(QRect(10, 37, 300, 100), Qt::AlignLeft, "KeepAspectRatioByExpanding");
163+
break;
164+
}
165+
}
166+
167+
void cvWindow::keyPressEvent(QKeyEvent* event)
168+
{
169+
switch (event->key())
170+
{
171+
// M: changes aspect ratio mode
172+
case Qt::Key_M:
173+
{
174+
if (_ar_mode == Qt::IgnoreAspectRatio)
175+
{
176+
_ar_mode = Qt::KeepAspectRatio;
177+
}
178+
else if (_ar_mode == Qt::KeepAspectRatio)
179+
{
180+
_ar_mode = Qt::KeepAspectRatioByExpanding;
181+
}
182+
else
183+
{
184+
_ar_mode = Qt::IgnoreAspectRatio;
185+
}
186+
}
187+
188+
// ESC: exit application
189+
case Qt::Key_Escape:
190+
{
191+
close();
192+
}
193+
break;
194+
}
195+
}
196+
197+
void cvWindow::_open()
198+
{
199+
// Display dialog so the user can select a file
200+
QString filename = QFileDialog::getOpenFileName(this,
201+
tr("Open Video"),
202+
QDir::currentPath(),
203+
tr("Files (*.*)"));
204+
205+
if (filename.isEmpty()) // Do nothing if filename is empty
206+
return;
207+
208+
// If its already capturing, stop and release it!
209+
if (_capture)
210+
{
211+
_timer->stop();
212+
delete _timer;
213+
214+
_capture->release();
215+
delete _capture;
216+
}
217+
218+
// Create a new capture interface based on the filename
219+
_capture = new cv::VideoCapture(filename.toStdString());
220+
if (!_capture->isOpened())
221+
{
222+
qDebug() << "cvWindow::_open !!! Unable to open " << filename;
223+
setWindowTitle(tr("QT Video demo with OpenCV"));
224+
return;
225+
}
226+
227+
// Set the filename as the window title
228+
setWindowTitle(filename);
229+
230+
// Retrieve the width/height of the video. If not possible, then use the current size of the window
231+
_video_width = 0;
232+
_video_width = _capture->get(CV_CAP_PROP_FRAME_WIDTH);
233+
_video_height = 0;
234+
_video_height = _capture->get(CV_CAP_PROP_FRAME_HEIGHT);
235+
qDebug() << "cvWindow::_open default size is " << _video_width << "x" << _video_height;
236+
237+
if (!_video_width || !_video_height)
238+
{
239+
_video_width = width();
240+
_video_height = height();
241+
}
242+
243+
// Resize the window to fit video dimensions
244+
resize(_video_width, _video_height);
245+
246+
// Retrieve fps from the video. If not available, default will be 25
247+
_fps = 0;
248+
_fps = _capture->get(CV_CAP_PROP_FPS);
249+
qDebug() << "cvWindow::_open default fps is " << _fps;
250+
251+
if (!_fps)
252+
_fps = 25;
253+
254+
// Set FPS playback
255+
_tick_ms = 1000/_fps;
256+
257+
// _image is created according to video dimensions
258+
if (_image)
259+
{
260+
delete _image;
261+
}
262+
_image = new QImage(_video_width, _video_height, QImage::Format_RGB888);
263+
264+
// Start timer to read frames from the capture interface
265+
_timer = new QTimer();
266+
_timer->start(_tick_ms);
267+
QObject::connect(_timer, SIGNAL(timeout()), this, SLOT(_tick()));
268+
}
269+
270+
void cvWindow::_close()
271+
{
272+
qDebug() << "cvWindow::_close";
273+
emit closed();
274+
}

cvVideo/cvWindow.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
#include <QGridLayout>
3+
#include <QMenu>
4+
#include <QMenuBar>
5+
#include <QImage>
6+
#include <QKeyEvent>
7+
8+
#include <highgui.h>
9+
10+
11+
class cvWindow : public QWidget
12+
{
13+
Q_OBJECT
14+
15+
public:
16+
cvWindow();
17+
~cvWindow();
18+
19+
void _draw_video_frame(QPainter& painter);
20+
void _draw_info_text(QPainter& painter);
21+
22+
void paintEvent(QPaintEvent* e);
23+
void keyPressEvent(QKeyEvent* event);
24+
25+
private:
26+
QMenuBar _menu_bar;
27+
QMenu* _menu;
28+
QImage* _image;
29+
cv::VideoCapture* _capture;
30+
int _tick_ms;
31+
Qt::AspectRatioMode _ar_mode;
32+
int _fps;
33+
int _video_width;
34+
int _video_height;
35+
QTimer* _timer;
36+
37+
private slots:
38+
void _open();
39+
void _tick();
40+
41+
protected:
42+
void _close();
43+
44+
signals:
45+
void closed();
46+
};

cvVideo/main.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "cvWindow.h"
2+
3+
#include <QApplication>
4+
5+
int main(int argc, char* argv[])
6+
{
7+
QApplication app(argc, argv);
8+
9+
cvWindow win;
10+
win.show();
11+
12+
QObject::connect(&win, SIGNAL(closed()),
13+
&app, SLOT(quit()) );
14+
15+
return app.exec();
16+
}

0 commit comments

Comments
 (0)