|
| 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 | +} |
0 commit comments