Skip to content

Commit e8c89b1

Browse files
committed
Added cvImage to the repository.
1 parent 13298e8 commit e8c89b1

File tree

4 files changed

+230
-0
lines changed

4 files changed

+230
-0
lines changed

cvImage/cvImage.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
}

cvImage/cvImage.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#pragma once
2+
#include <QMainWindow>
3+
#include <QMenu>
4+
#include <QMenuBar>
5+
#include <QImage>
6+
#include <QKeyEvent>
7+
8+
#include <highgui.h>
9+
10+
11+
class cvImage : public QMainWindow
12+
{
13+
Q_OBJECT
14+
15+
public:
16+
cvImage();
17+
~cvImage();
18+
19+
void paintEvent(QPaintEvent* e);
20+
void keyPressEvent(QKeyEvent* event);
21+
void mouseMoveEvent(QMouseEvent *event);
22+
23+
private:
24+
QMenuBar _menu_bar;
25+
QMenu* _menu;
26+
QImage* _image;
27+
28+
private slots:
29+
void _open();
30+
31+
protected:
32+
void _close();
33+
34+
signals:
35+
void closed();
36+
};

cvImage/cvImage.pro

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
TEMPLATE = app
2+
3+
QT += core gui
4+
5+
SOURCES += \
6+
main.cpp \
7+
cvImage.cpp
8+
9+
HEADERS += \
10+
cvImage.h
11+
12+
## OpenCV settings for Mac OS X
13+
macx {
14+
INCLUDEPATH += /usr/local/include/opencv
15+
16+
LIBS += -L/usr/local/lib/ \
17+
-lopencv_core \
18+
-lopencv_highgui \
19+
-lopencv_imgproc
20+
}
21+
22+
## OpenCV settings for Windows and OpenCV 2.4.2
23+
win32 {
24+
INCLUDEPATH += "C:\\opencv\\build\\include" \
25+
"C:\\opencv\\build\\include\\opencv" \
26+
"C:\\opencv\\build\\include\\opencv2"
27+
28+
LIBS += -L"C:\\opencv\\build\\x86\\vc10\\lib" \
29+
-lopencv_core242 \
30+
-lopencv_highgui242 \
31+
-lopencv_imgproc242
32+
}
33+
34+
# Runs this app automatically after the building has succeeded
35+
#QMAKE_POST_LINK=./$$TARGET

cvImage/main.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include "cvImage.h"
2+
3+
#include <QApplication>
4+
5+
int main(int argc, char* argv[])
6+
{
7+
QApplication app(argc, argv);
8+
9+
cvImage win;
10+
win.show();
11+
12+
QObject::connect(&win, SIGNAL(closed()),
13+
&app, SLOT(quit()) );
14+
15+
return app.exec();
16+
}
17+

0 commit comments

Comments
 (0)