Professional Documents
Culture Documents
QPixmap
Optimised for display on screen.
QBitmap
Inherited from QPixmap for black/white images.
QPicture
Paint device that records and replays QPainter commands.
#include <QApplication>
#include <QPixmap>
#include <QLabel>
setImage(fileName)
Loads image with file name fileName.
Initial zoom should be as large as possible but ≤ 1.0
such that displayed image size ≤ initial widget size.
For image ≤ initial widget size, initial zoom = 1.0.
For image > initial widget size, initial zoom < 1.0.
#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H
#include <QWidget>
#include <QImage>
#include <QPixmap>
Q_PROPERTY(type name
READ getFunction
[WRITE setFunction]
[RESET resetFunction]
[NOTIFY notifySignal]
...
)
protected:
void paintEvent(QPaintEvent *event);
void wheelEvent(QWheelEvent *event);
private:
QPixmap input; // Input image.
QPixmap display; // Displayed image.
float zoom;
};
#endif
#include <QtGui>
#include "ImageWidget.h"
setZoom(initZoom);
}
zoom = newZoom;
display = input.scaled(zoom * input.size(),
Qt::KeepAspectRatio);
qDebug("zoom %f", zoom); // for debugging
resize(display.size());
}
char string[100];
sprintf(string, "%.1f", newZoom);
sscanf(string, "%f", &newZoom); // rounding
setZoom(newZoom);
}
#include <QApplication>
#include "ImageWidget.h"
if (argc > 1)
widget.setImage(argv[1]);
widget.show();
return app.exec();
}
scroll->show();
return app.exec();
}
Very difficult.
// webcam.c
#include <core_c.h>
#include <highgui_c.h>
cvShowImage("webcam", frame);
cvReleaseCapture(&capture);
cvDestroyWindow("webcam");
return 0;
}
Native Webcam
screen display (QWidget)
interface output
QTimer
init timer
OpenCV
video capture OpenCV event
get frame
functions interface
frame
#ifndef WEBCAM_H
#define WEBCAM_H
#include <QWidget>
#include <QTime>
#include <core_c.h>
#include <highgui_c.h>
public:
Webcam(int dev, int fps);
~Webcam();
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
#include <QtGui>
#include "Webcam.h"
if (++nframes == 50)
{
qDebug("frame rate: %f", // actual frame rate
(float) nframes * 1000 / time.elapsed());
nframes = 0;
time.restart();
}
}
else
QWidget::timerEvent(event);
}
img.setColorTable(colorTable);
return img;
}
else
{
qWarning() << "Image cannot be converted.";
return QImage();
}
}
CS3249 (SoC, NUS) Images and Video Capture 44
CS3249
CS3249 (SoC, NUS) (SoC, NUS) Images and Video Capture 44
// main.cpp
#include <QApplication>
#include "Webcam.h"
#include "stdio.h"
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += Webcam.h
SOURCES += main.cpp Webcam.cpp
QMAKE_CXXFLAGS += -Wno-deprecated
INCLUDEPATH += /usr/include/opencv2/core
/usr/include/opencv2/highgui
LIBS += -L/usr/lib -lopencv_core -lopencv_imgproc
-lopencv_highgui
Image
Processor
Native Webcam
screen display (QWidget)
interface output processed
image
QTimer
init timer
OpenCV
video capture OpenCV event
get frame
functions interface
frame
CS3249 (SoC, NUS) Images and Video Capture 49
CS3249 (SoC, NUS) Images and Video Capture 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
#include "ImageProc.h"
#include <imgproc_c.h>
if (target)
cvReleaseImage(&target);
target = cvCreateImage(
cvSize(source->width, source->height),
source->depth, source->nChannels);
default: // do nothing
return source;
}
}
public:
Webcam(int dev, int fps, int type);
~Webcam();
...
protected:
...
void timerEvent(QTimerEvent *event);
private:
...
ImageProc *imageProc;
int demoType;
};
capture = cvCaptureFromCAM(dev);
Webcam::~Webcam()
{
cvReleaseCapture(&capture);
delete imageProc;
}
...
}
}
Question:
Timer events can fire at desired frame rate.
What happen to missing timer events (dotted arrows)?
output
Native Webcam
screen display (QWidget)
interface output
QTimer
init timer
OpenCV
video capture OpenCV event
get frame
functions interface
frame
input
CS3249 (SoC, NUS) Images and Video Capture 63
CS3249 (SoC, NUS) Images and Video Capture 63
processed
image
Native Main
display
screen Window
output
interface
QTimer Image
Processor
generic
init camera timer
functions event
init get frame
frame
init
OpenCV Webcam
video capture get frame (camera
functions interface)
frame
init
OpenCV Webcam
video capture get frame (camera
functions interface)
frame
init
OpenCV Webcam
video capture get frame (camera
functions interface)
frame