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