OpenCVWidget
|
Widget to captures frames or single images using OpenCV and display them using OpenGL. More...
#include <opencvwidget.h>
Signals | |
void | frameCaptured () |
Emitted each time a new frame is captured. | |
void | mouseClicked (QMouseEvent *event) |
Emitted each time the mouse is clicked. | |
void | mouseMoved (QMouseEvent *event) |
Emitted when the mouse is moved and mouse tracking is on. | |
Public Member Functions | |
OpenCVWidget (QWidget *parent=0, QGLWidget *shareWidget=0) | |
Constructor. | |
~OpenCVWidget () | |
QSize | sizeHint () const |
Sets a default widget size of 400x300 px. | |
void | startCapture () |
Start a capture. | |
void | startCapture (QString source, int frameRate=-1, bool autoDisplay=true) |
Start a capture while setting the source and frame rate. | |
void | setSource (QString source) |
Set the video source. | |
QString | getSource () |
Return the capture source. | |
void | setFrameRate (int rate) |
Set desired frame rate. | |
int | getFrameRate () |
Retrive the set frame rate. | |
void | stopCapture () |
Stop the capture. | |
void | pauseCapture () |
Pause the capture. | |
void | resumeCapture () |
Restart the capture. | |
void | advanceCapture () |
Manually read next frame in a stream. | |
void | grab () |
Manually grab next frame. | |
void | loadImage (int loadFlag=1) |
Load a single image. | |
void | loadImage (QString source, bool autoDisplay=true, int loadFlag=1) |
Load a single image from the specified source. | |
cv::Mat | getImage () |
Return the current image. | |
void | enableAutoDisplay (bool value) |
Set the auto display setting. | |
bool | getAutoDisplay () |
Get the auto display setting. | |
void | updateDisplay () |
Update display. | |
double | getCaptureProperty (int propId) |
Get a property. | |
bool | setCaptureProperty (int propId, double value) |
Set a property. | |
bool | isOpened () |
Tell whether a video capture is opened. | |
cv::Point | mapPoint (cv::Point widgetCoords) |
Map a point in the widget to the image coordinates. | |
Protected Slots | |
void | captureFrame () |
Captures a new frame when the timer asks it to. | |
Protected Member Functions | |
void | initializeGL () |
Used for initializing the OpenGL session. | |
void | paintGL () |
Used everytime the display is refreshed. | |
void | resizeGL (int width, int height) |
Used when the widget is resized. | |
void | mousePressEvent (QMouseEvent *event) |
Called whenever a mousebutton is pressed. | |
void | mouseMoveEvent (QMouseEvent *event) |
Called when the mouse is moved and mouse tracking is on. | |
Protected Attributes | |
GLuint | texture |
The current OpenGL texture. Used for holding images. | |
QVector< QVector2D > | vertices |
The vertices of the texture surface. | |
QVector< QVector2D > | texCoords |
Texture mapping coordinates. | |
QTimer * | timer |
Timer used for setting capture frame rate. | |
cv::VideoCapture | capture |
Capture object for video. | |
cv::Mat | image |
Holds the most recently captured image. | |
cv::Mat | displayImage |
Holds the image to be displayed in the OpenGL-widget. | |
int | frameRate |
Holds the frame rate that controls the timer. | |
QString | captureSource |
Specifies the source identifier/file name. | |
bool | autoDisplay |
Determines if the most recent image is automatically displayed. |
Widget to captures frames or single images using OpenCV and display them using OpenGL.
This class integrates OpenCV and Qt. It is a widget that can be added to your GUI and it can handle both the capture and display of video and single images.
The class is implemented with a double buffer system: One cv::Mat holds the most recently captured image (hereafter called capture image) and another holds the image that is currently being displayed in the widget (the display image). This allows you to perform processing on the capture image before showing it, by first calling getImage(), then altring it, and finally calling updateDisplay(). If you simply want to show it right away, enable auto display (using enableAutoDisplay()).
Definition at line 31 of file opencvwidget.h.
OpenCVWidget::OpenCVWidget | ( | QWidget * | parent = 0 , |
QGLWidget * | shareWidget = 0 |
||
) |
Constructor.
Creates a new OpenCVWidget. Be aware that the capture source must be set before any capture can commence. See setSource().
Definition at line 8 of file opencvwidget.cpp.
Definition at line 16 of file opencvwidget.cpp.
void OpenCVWidget::advanceCapture | ( | ) |
Manually read next frame in a stream.
If no timer is set, this function can be used to manually read the next frame. This functions has no effect unless the frame rate is set to 0.
Definition at line 125 of file opencvwidget.cpp.
void OpenCVWidget::captureFrame | ( | ) | [protected, slot] |
Captures a new frame when the timer asks it to.
Definition at line 286 of file opencvwidget.cpp.
void OpenCVWidget::enableAutoDisplay | ( | bool | value | ) |
Set the auto display setting.
If auto display is enabled, the most recently captured frame is automatically displayed in the OpenGL-window. If not, updateDisplay() must be called.
value | set true to enable auto display, false to disable. |
Definition at line 168 of file opencvwidget.cpp.
void OpenCVWidget::frameCaptured | ( | ) | [signal] |
Emitted each time a new frame is captured.
Definition at line 86 of file moc_opencvwidget.cpp.
bool OpenCVWidget::getAutoDisplay | ( | ) |
Get the auto display setting.
Definition at line 173 of file opencvwidget.cpp.
double OpenCVWidget::getCaptureProperty | ( | int | propId | ) |
Get a property.
Get any property from the capture device/file, as specified in http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get
the | desired property. |
Definition at line 304 of file opencvwidget.cpp.
int OpenCVWidget::getFrameRate | ( | ) |
Retrive the set frame rate.
Note that this does not return the frame rate the camera or video source uses internally, only the frame rate this widget will try to obtain pictures with.
Definition at line 98 of file opencvwidget.cpp.
Mat OpenCVWidget::getImage | ( | ) |
Return the current image.
This function returns the current image for further processing elsewhere. Note that this is a shallow copy (as per OpenCV's memory management), so any alterations of the returned Mat will reflect in the original Mat being displayed in the OpenGL object.
Definition at line 163 of file opencvwidget.cpp.
QString OpenCVWidget::getSource | ( | ) |
Return the capture source.
Definition at line 85 of file opencvwidget.cpp.
void OpenCVWidget::grab | ( | ) |
Manually grab next frame.
Use to seek to a specific point in a video (when setProperty() cannot be used due to bug 1419 in OpenCV. Remember that this does not retrieve the frame, so be sure to call advanceCapture() after calling this. This functions has no effect unless the frame rate is set to 0.
Definition at line 132 of file opencvwidget.cpp.
void OpenCVWidget::initializeGL | ( | ) | [protected] |
Used for initializing the OpenGL session.
Definition at line 183 of file opencvwidget.cpp.
bool OpenCVWidget::isOpened | ( | ) |
Tell whether a video capture is opened.
Definition at line 314 of file opencvwidget.cpp.
void OpenCVWidget::loadImage | ( | int | loadFlag = 1 | ) |
Load a single image.
Instead of using OpenCV's VideoCapture to capture a stream of images, this method uses imread() to load a single image. The image stays in memory until overwritten by a new capture or a new
Definition at line 139 of file opencvwidget.cpp.
void OpenCVWidget::loadImage | ( | QString | source, |
bool | autoDisplay = true , |
||
int | loadFlag = 1 |
||
) |
Load a single image from the specified source.
Overloaded version of loadImage(). Allows you to pass the file name in and load it in a single step.
source | the name of the file to load. This will be set as the capture source. |
if | true, the loaded image is displayed immidiately. |
Definition at line 156 of file opencvwidget.cpp.
cv::Point OpenCVWidget::mapPoint | ( | cv::Point | widgetCoords | ) |
Map a point in the widget to the image coordinates.
the | point in the widget coordinate system. |
Definition at line 329 of file opencvwidget.cpp.
void OpenCVWidget::mouseClicked | ( | QMouseEvent * | event | ) | [signal] |
Emitted each time the mouse is clicked.
void OpenCVWidget::mouseMoved | ( | QMouseEvent * | event | ) | [signal] |
Emitted when the mouse is moved and mouse tracking is on.
void OpenCVWidget::mouseMoveEvent | ( | QMouseEvent * | event | ) | [protected] |
Called when the mouse is moved and mouse tracking is on.
Definition at line 324 of file opencvwidget.cpp.
void OpenCVWidget::mousePressEvent | ( | QMouseEvent * | event | ) | [protected] |
Called whenever a mousebutton is pressed.
Definition at line 319 of file opencvwidget.cpp.
void OpenCVWidget::paintGL | ( | ) | [protected] |
Used everytime the display is refreshed.
Definition at line 206 of file opencvwidget.cpp.
void OpenCVWidget::pauseCapture | ( | ) |
Pause the capture.
Pause the internal capture timer.
Definition at line 109 of file opencvwidget.cpp.
void OpenCVWidget::resizeGL | ( | int | width, |
int | height | ||
) | [protected] |
Used when the widget is resized.
Definition at line 267 of file opencvwidget.cpp.
void OpenCVWidget::resumeCapture | ( | ) |
Restart the capture.
Resume the internal capture timer. For video input, this resumes where the video was paused, for live camera input, this resumes at the current frame.
Definition at line 114 of file opencvwidget.cpp.
bool OpenCVWidget::setCaptureProperty | ( | int | propId, |
double | value | ||
) |
Set a property.
Set any property from the capture device/file, as specified in http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-set
the | desired property. |
the | desired value. |
Definition at line 309 of file opencvwidget.cpp.
void OpenCVWidget::setFrameRate | ( | int | rate | ) |
Set desired frame rate.
frameRate | the desired frame rate. If frameRate is < 0 the program will attempt to determine the frame rate automatically from the input file. |
Definition at line 90 of file opencvwidget.cpp.
void OpenCVWidget::setSource | ( | QString | source | ) |
Set the video source.
Usually a file name of the video you want to process. If only an integer is provided, the source is the connected camera with that number.
Definition at line 80 of file opencvwidget.cpp.
QSize OpenCVWidget::sizeHint | ( | ) | const |
Sets a default widget size of 400x300 px.
Definition at line 20 of file opencvwidget.cpp.
void OpenCVWidget::startCapture | ( | ) |
Start a capture.
When the capture source is set, use this method to begin capturing from a video source using OpenCV's VideoCapture. Whenever a new frame is captured, the signal frameCaptured() is emitted.
Definition at line 25 of file opencvwidget.cpp.
void OpenCVWidget::startCapture | ( | QString | source, |
int | frameRate = -1 , |
||
bool | autoDisplay = true |
||
) |
Start a capture while setting the source and frame rate.
An overloaded version of startCapture() that allows you to set the source and frame rate while starting the capture.
source | the source identifier (see setSource()). |
frameRate | the desired frame rate. If frameRate is < 0 the program will attempt to determine the frame rate automatically from the input file. If frameRate is == 0, no timer will be started. Instead, use advanceCapture() to manually frab the frames. |
autoDisplay | set true to automatically refresh the display, when a new frame is obtained. Set to false if you want to process the image before displaying it. |
Definition at line 72 of file opencvwidget.cpp.
void OpenCVWidget::stopCapture | ( | ) |
Stop the capture.
This stops the internal capture timer and releases the VideoCapture object. The current image will stay in memory until overwritten when a capture is started again.
Definition at line 103 of file opencvwidget.cpp.
void OpenCVWidget::updateDisplay | ( | ) |
Update display.
Copies the most recently captured image into the display image, in effect showing the most recently captures image at next render. This also overwrites the previous display image. See the class description for further detail.
Definition at line 178 of file opencvwidget.cpp.
bool OpenCVWidget::autoDisplay [protected] |
Determines if the most recent image is automatically displayed.
Definition at line 264 of file opencvwidget.h.
cv::VideoCapture OpenCVWidget::capture [protected] |
Capture object for video.
Definition at line 259 of file opencvwidget.h.
QString OpenCVWidget::captureSource [protected] |
Specifies the source identifier/file name.
Definition at line 263 of file opencvwidget.h.
cv::Mat OpenCVWidget::displayImage [protected] |
Holds the image to be displayed in the OpenGL-widget.
Definition at line 261 of file opencvwidget.h.
int OpenCVWidget::frameRate [protected] |
Holds the frame rate that controls the timer.
Definition at line 262 of file opencvwidget.h.
cv::Mat OpenCVWidget::image [protected] |
Holds the most recently captured image.
Definition at line 260 of file opencvwidget.h.
QVector<QVector2D> OpenCVWidget::texCoords [protected] |
Texture mapping coordinates.
Definition at line 257 of file opencvwidget.h.
GLuint OpenCVWidget::texture [protected] |
The current OpenGL texture. Used for holding images.
Definition at line 255 of file opencvwidget.h.
QTimer* OpenCVWidget::timer [protected] |
Timer used for setting capture frame rate.
Definition at line 258 of file opencvwidget.h.
QVector<QVector2D> OpenCVWidget::vertices [protected] |
The vertices of the texture surface.
Definition at line 256 of file opencvwidget.h.