|
| 1 | +#include "cvImage.h" |
| 2 | + |
| 3 | +#include <QDebug> |
| 4 | +#include <QLabel> |
| 5 | +#include <QPainter> |
| 6 | +#include <QFileDialog> |
| 7 | +#include <QApplication> |
| 8 | +#include <QStatusBar> |
| 9 | + |
| 10 | +#include <cv.h> |
| 11 | + |
| 12 | + |
| 13 | +cvImage::cvImage() |
| 14 | +: _menu(NULL), _image(NULL) |
| 15 | +{ |
| 16 | + setWindowTitle(tr("QT Image demo with OpenCV")); |
| 17 | + resize(480, 240); |
| 18 | + |
| 19 | + _menu = new QMenu("File"); |
| 20 | + _menu->addAction("Op3n", this, SLOT(_open())); |
| 21 | + _menu->addSeparator(); |
| 22 | + _menu->addAction("Ex1t", this, SLOT(close())); |
| 23 | + _menu_bar.addMenu(_menu); |
| 24 | + |
| 25 | + setMenuBar(&_menu_bar); |
| 26 | +} |
| 27 | + |
| 28 | +cvImage::~cvImage() |
| 29 | +{ |
| 30 | + if (_menu) |
| 31 | + delete _menu; |
| 32 | + |
| 33 | + if (_image) |
| 34 | + delete _image; |
| 35 | +} |
| 36 | + |
| 37 | +void cvImage::paintEvent(QPaintEvent* e) |
| 38 | +{ |
| 39 | + QPainter painter(this); |
| 40 | + |
| 41 | + // When no image has been loaded, there's nothing to draw. |
| 42 | + if (!_image) |
| 43 | + return; |
| 44 | + |
| 45 | + painter.drawImage(QPoint(0, 0), *_image); |
| 46 | + |
| 47 | + QWidget::paintEvent(e); |
| 48 | +} |
| 49 | + |
| 50 | +void cvImage::_open() |
| 51 | +{ |
| 52 | + // Display dialog so the user can select a file |
| 53 | + QString filename = QFileDialog::getOpenFileName(this, |
| 54 | + tr("Open Image"), |
| 55 | + QDir::currentPath(), |
| 56 | + tr("Files (*.png *.jpg *.tiff *.bmp)")); |
| 57 | + |
| 58 | + if (filename.isEmpty()) // Do nothing if filename is empty |
| 59 | + return; |
| 60 | + |
| 61 | + cv::Mat img = cv::imread(filename.toStdString()); |
| 62 | + if (img.empty()) |
| 63 | + return; |
| 64 | + |
| 65 | + // Since OpenCV uses BGR order, we need to convert it to RGB |
| 66 | + cv::cvtColor(img, img, CV_BGR2RGB); |
| 67 | + |
| 68 | + // _image is created according to video dimensions |
| 69 | + if (_image) |
| 70 | + { |
| 71 | + delete _image; |
| 72 | + } |
| 73 | + _image = new QImage(img.size().width, img.size().height, QImage::Format_RGB888); |
| 74 | + |
| 75 | + // Copy cv::Mat to QImage |
| 76 | + qMemCopy(_image->scanLine(0), (unsigned char*)img.data, _image->width() * _image->height() * img.channels()); |
| 77 | + |
| 78 | + // Set the filename as the window title |
| 79 | + setWindowTitle(filename); |
| 80 | + |
| 81 | + // Resize the window to fit video dimensions |
| 82 | + resize(img.size().width, img.size().height); |
| 83 | + |
| 84 | + // Mouse move events will occur only when a mouse button is pressed down, |
| 85 | + // unless mouse tracking has been enabled: |
| 86 | + QWidget::setMouseTracking(true); |
| 87 | + |
| 88 | + // Trigger paint event to redraw the window |
| 89 | + update(); |
| 90 | +} |
| 91 | + |
| 92 | +void cvImage::keyPressEvent(QKeyEvent* event) |
| 93 | +{ |
| 94 | + switch (event->key()) |
| 95 | + { |
| 96 | + // ESC: exit application |
| 97 | + case Qt::Key_Escape: |
| 98 | + { |
| 99 | + close(); |
| 100 | + } |
| 101 | + break; |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +void cvImage::mouseMoveEvent(QMouseEvent *event) |
| 106 | +{ |
| 107 | + if (!_image) |
| 108 | + return; |
| 109 | + |
| 110 | + QPoint pos = event->pos(); |
| 111 | + if (pos.x() < 0 || pos.x() >= _image->width()) |
| 112 | + return; |
| 113 | + if (pos.y() < 0 || pos.y() >= _image->height()) |
| 114 | + return; |
| 115 | + |
| 116 | + QString pos_x; |
| 117 | + pos_x = pos_x.setNum(pos.x()); |
| 118 | + |
| 119 | + QString pos_y; |
| 120 | + pos_y = pos_y.setNum(pos.y()); |
| 121 | + |
| 122 | + QColor pixel(_image->pixel(pos)); |
| 123 | + int r = pixel.red(); |
| 124 | + int g = pixel.green(); |
| 125 | + int b = pixel.blue(); |
| 126 | + |
| 127 | + QString pixel_info = pos_x + "," + pos_y + " R:" + QString().setNum(r) + |
| 128 | + " G:" + QString().setNum(g) + |
| 129 | + " B:" + QString().setNum(b); |
| 130 | + |
| 131 | + // TODO: set pixel info on status bar instead of window title |
| 132 | + //this->statusBar()->setStatusTip(pixel_info); |
| 133 | + |
| 134 | + setWindowTitle(pixel_info); |
| 135 | + |
| 136 | + qDebug() << "cvImage::mouseMoveEvent " << pixel_info; |
| 137 | +} |
| 138 | + |
| 139 | +void cvImage::_close() |
| 140 | +{ |
| 141 | + emit closed(); |
| 142 | +} |
0 commit comments