Professional Documents
Culture Documents
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
1
1
2
2
Qt Images
QImage
Optimised for I/O, direct pixel access and manipulation.
QPixmap
Optimised for display on screen.
QBitmap
Inherited from QPixmap for black/white images.
QPicture
Paint device that records and replays QPainter commands.
3
3
#include <QApplication>
#include <QPixmap>
#include <QLabel>
int main(int argv, char **args)
{
QApplication app(argv, args);
QPixmap pic("butterfly.jpg"); // read image
QLabel label;
label.setPixmap(pic); // put image in label
label.show();
return app.exec();
}
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
4
4
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
5
5
Painting QWidget
6
6
Specification of ImageWidget
ImageWidget size
Initial size
Default depends on platform and screen geometry.
Set to (800, 600), width = 800, height = 600.
Minimum size is given by minimumSize().
Default is (0, 0), which causes widget to disappear.
Choose (10, 10).
Client class can change it by calling setMinimumSize().
Maximum size is given by maximumSize().
Default is (16777215, 16777215).
Keep this default.
Client class can change it by calling setMaximumSize().
7
7
setImage(fileName)
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
8
8
Zooming
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
9
9
// ImageWidget.h
#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H
#include <QWidget>
#include <QImage>
#include <QPixmap>
class ImageWidget: public QWidget
{
Q_OBJECT
Q_PROPERTY(float zoom READ getZoom WRITE setZoom)
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
10
10
Property
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
11
11
public:
ImageWidget(QWidget *parent = 0);
void setImage(const QString &fileName);
float getZoom() const { return zoom; }
void setZoom(float newZoom);
// inline
protected:
void paintEvent(QPaintEvent *event);
void wheelEvent(QWheelEvent *event);
private:
QPixmap input; // Input image.
QPixmap display; // Displayed image.
float zoom;
};
#endif
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
12
12
// ImageWidget.cpp
#include <QtGui>
#include "ImageWidget.h"
ImageWidget::ImageWidget(QWidget *parent): QWidget(parent)
{
// Set defaults
zoom = 0.0;
setMinimumSize(10, 10); // width, height
}
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
13
13
setZoom(initZoom);
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
14
14
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
15
15
input.scaled(...)
resize()
resizeEvent()
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
16
16
// Event handlers
void ImageWidget::paintEvent(QPaintEvent *event)
{
QRectF rect = QRectF(QPoint(), size());
QPainter painter(this);
painter.drawPixmap(rect, display, rect);
}
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
17
17
Caution
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
18
18
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
19
19
// main.cpp
#include <QApplication>
#include "ImageWidget.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
ImageWidget widget;
// widget.setMinimumSize(50, 50);
// widget.setMaximumSize(1100, 700);
if (argc > 1)
widget.setImage(argv[1]);
widget.show();
return app.exec();
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
20
20
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
22
22
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
23
23
24
24
QScrollArea
Subclass of QWidget.
Provides scrolling view onto another widget.
No change to ImageWidget.
Scroll bars appear if ImageWidget is too large.
25
25
// main.cpp
#include <QApplication>
#include <QScrollArea>
#include "ImageWidget.h"
Really easy!
There's usually
an easy way
to do things in Qt!
scroll->show();
return app.exec();
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
26
26
Interfacing Devices
29
29
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
30
30
31
31
// webcam.c
#include <core_c.h>
#include <highgui_c.h>
int main(int argc, char **args)
{
int dev = 0; // 0: default, > 0: other cameras.
cvNamedWindow("webcam", 1); // Create OpenCV window.
CvCapture* capture = cvCaptureFromCAM(dev);
cvSetCaptureProperty(capture,
CV_CAP_PROP_FRAME_WIDTH, 640);
cvSetCaptureProperty(capture,
CV_CAP_PROP_FRAME_HEIGHT, 480);
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
32
32
while(1)
{
IplImage *frame = cvQueryFrame(capture);
cvShowImage("webcam", frame);
char key = cvWaitKey(10);
if (key == 27) // If key is ESC,
break;
// break out of loop.
}
cvReleaseCapture(&capture);
cvDestroyWindow("webcam");
return 0;
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
33
33
Integrating OpenCV in Qt
Qt is event-driven.
35
35
Native
screen
interface
display
output
Webcam
(QWidget)
QTimer
OpenCV
video capture
functions
init
get frame
OpenCV
interface
timer
event
frame
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
36
36
// Webcam.h
#ifndef WEBCAM_H
#define WEBCAM_H
#include
#include
#include
#include
<QWidget>
<QTime>
<core_c.h>
<highgui_c.h>
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
37
37
protected:
void showEvent(QShowEvent *event);
void hideEvent(QHideEvent *event);
void timerEvent(QTimerEvent *event);
void paintEvent(QPaintEvent *event);
private:
CvCapture* capture;
int timerId;
int frameRate; // input frame rate
QPixmap pixmap;
int nframes;
// used to calculate actual frame rate
QTime time;
// used to calculate actual frame rate
};
#endif
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
38
38
// Webcam.cpp
#include <QtGui>
#include "Webcam.h"
Webcam::Webcam(int dev, int fps)
{
timerId = 0;
frameRate = fps;
capture = cvCaptureFromCAM(dev);
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
39
39
Webcam::~Webcam()
{
cvReleaseCapture(&capture);
}
void Webcam::showEvent(QShowEvent *event)
{
nframes = 0; // init
timerId = startTimer(1000 / frameRate);
time.start(); // start time
}
// in msec
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
40
40
}
else
}
QWidget::timerEvent(event);
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
41
41
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
42
42
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
43
43
}
else
{
qWarning() << "Image cannot be converted.";
return QImage();
}
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
44
44
// main.cpp
#include <QApplication>
#include "Webcam.h"
#include "stdio.h"
int main(int argc, char **args)
{
QApplication app(argc, args);
int dev = 0; // default device
int fps = 25; // 25 frames per second
if (argc > 1)
sscanf(args[1], "%d", &dev);
if (argc > 2)
sscanf(args[2], "%d", &fps);
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
45
45
#
#
#
#
webcam.pro
Run qmake -project to generate file.
Then, add the include line.
Then, run qmake followed by make.
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# include additional compiler info for OpenCV
include(opencv.pro)
# Input
HEADERS += Webcam.h
SOURCES += main.cpp Webcam.cpp
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
46
46
# opencv.pro
QMAKE_CXXFLAGS += -Wno-deprecated
INCLUDEPATH += /usr/include/opencv2/core
/usr/include/opencv2/highgui
LIBS += -L/usr/lib -lopencv_core -lopencv_imgproc
-lopencv_highgui
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
47
47
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
48
48
Native
screen
interface
display
output
Webcam
(QWidget)
processed
image
QTimer
OpenCV
video capture
functions
init
get frame
OpenCV
interface
timer
event
frame
CS3249 (SoC, NUS)
49
49
// ImageProc.h
#ifndef IMAGEPROC_H
#define IMAGEPROC_H
#include <core_c.h>
#include <highgui_c.h>
class ImageProc
{
public:
IplImage *process(IplImage *source, int type);
};
#endif
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
50
50
// ImageProc.cpp
#include "ImageProc.h"
#include <imgproc_c.h>
IplImage *ImageProc::process(IplImage *source, int type)
{
static IplImage *target = NULL;
if (target)
cvReleaseImage(&target);
target = cvCreateImage(
cvSize(source->width, source->height),
source->depth, source->nChannels);
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
51
51
switch(type)
{
case 0: // do nothing
return source;
case 1: // demo fast algo: fast smoothing
cvSmooth(source, target, CV_GAUSSIAN, 5);
return target;
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
52
52
default: // do nothing
return source;
}
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
53
53
// Webcam.h
class Webcam: public QWidget
{
Q_OBJECT
public:
Webcam(int dev, int fps, int type);
~Webcam();
...
protected:
...
void timerEvent(QTimerEvent *event);
private:
...
ImageProc *imageProc;
int demoType;
};
CS3249 (SoC, NUS)
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
54
54
// Webcam.cpp
Webcam::Webcam(int dev, int fps, int type)
{
timerId = 0;
frameRate = fps;
demoType = type;
capture = cvCaptureFromCAM(dev);
int width = 640;
int height = 480;
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH,
width);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT,
height);
setFixedSize(width, height);
}
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
55
55
// Webcam.cpp
Webcam::~Webcam()
{
cvReleaseCapture(&capture);
delete imageProc;
}
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
56
56
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
57
57
Response Time
Timer event
Video capture
Frame processing
58
58
Response time
Want rk .
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
59
59
Fast processing
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
60
60
Slower processing
Response time r 2.
Actual frame rate 1/2 desired frame rate.
Slower rate but still responsive.
Question:
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
61
61
display
output
Webcam
(QWidget)
QTimer
OpenCV
video capture
functions
init
get frame
OpenCV
interface
timer
event
frame
input
CS3249 (SoC, NUS)
63
63
Native
screen
interface
Main
Window
display
output
QTimer
generic
camera
functions
init
init
OpenCV
video capture
functions
init
get frame
get
frame
processed
image
Image
Processor
timer
event
frame
Webcam
(camera
interface)
frame
CS3249 (SoC, NUS)
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
64
64
Main
Window
display
output
QTimer
generic
camera
functions
init
simplified
diagram
init
OpenCV
video capture
functions
init
get frame
get
frame
processed
image
Image
Processor
timer
event
frame
Webcam
(camera
interface)
frame
CS3249 (SoC, NUS)
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
65
65
Main
Window
display
output
generic
camera
functions
Which pattern
is this?
init
OpenCV
video capture
functions
init
get frame
get
frame
processed
image
Image
Processor
frame
Webcam
(camera
interface)
frame
CS3249 (SoC, NUS)
CS3249 (SoC,
NUS) (SoC, NUS)
CS3249
66
66
Summary
Integrating devices
67
67
Further Reading
68
68
Exercise
69
69
References
70
70