-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimagecontainer.cpp
186 lines (151 loc) · 5.07 KB
/
imagecontainer.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "imagecontainer.hh"
#include <QMouseEvent>
#include <QPainter>
#define FIXED_HEIGHT 500 // used to scale to large pictures
///////////////////////////////////////////////////////////
ImageContainer::ImageContainer(QWidget *parent, Qt::WindowFlags f) :
QLabel(parent, f),
m_selectionOn(false),
m_highlightOn(false)
{
}
///////////////////////////////////////////////////////////
void ImageContainer::releaseSelection()
{
m_selectionOn = false;
m_selectionRect = QRect(); // clearing selection area
this->repaint();
}
///////////////////////////////////////////////////////////
void ImageContainer::loadImage(QString filename)
{
m_pixmap.load(filename);
m_image = m_pixmap.toImage();
if (m_pixmap.height() > FIXED_HEIGHT)
m_pixmap = m_pixmap.scaledToHeight(FIXED_HEIGHT);
m_scale = QSize(m_image.width() / m_pixmap.width(),
m_image.height() / m_pixmap.height());
this->setPixmap(m_pixmap);
}
///////////////////////////////////////////////////////////
void ImageContainer::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::LeftButton)
{
emit onSelectionStarted();
m_selectionOn = true;
m_highlightOn = false;
m_selectionRect.setTopLeft(event->pos());
}
}
///////////////////////////////////////////////////////////
void ImageContainer::mouseReleaseEvent(QMouseEvent* event)
{
if (m_selectionOn)
{
// if selection was on, make the selected rect valid if not
if (m_selectionRect.width() < 0)
{
m_selectionRect = QRect(m_selectionRect.topRight(),
QSize(-m_selectionRect.width(),
m_selectionRect.height())
);
}
if (m_selectionRect.height() < 0)
{
m_selectionRect = QRect(m_selectionRect.bottomLeft(),
QSize(m_selectionRect.width(),
-m_selectionRect.height())
);
}
}
if (event->button() == Qt::LeftButton)
m_selectionOn = false;
}
///////////////////////////////////////////////////////////
void ImageContainer::mouseMoveEvent(QMouseEvent* event)
{
if (!m_selectionOn)
return;
m_selectionRect.setBottomRight(event->pos());
this->repaint();
}
///////////////////////////////////////////////////////////
cv::Mat* ImageContainer::toMat()
{
cv::Mat mat = cv::Mat(m_image.height(),
m_image.width(),
CV_8UC4,
(uchar*)m_image.bits(),
m_image.bytesPerLine());
cv::cvtColor(mat, mat, CV_RGB2BGR);
cv::Mat* mat2 = new cv::Mat(mat.rows, mat.cols, CV_8UC3);
int mixChannelsArray[] = { 0,0, 1,1, 2,2 };
cv::mixChannels( &mat, 1, mat2, 1, mixChannelsArray, 3);
return mat2;
}
///////////////////////////////////////////////////////////
void ImageContainer::setFromMat(const cv::Mat& mat)
{
m_pixmap = QPixmap::fromImage(QImage((unsigned char*) mat.data,
mat.cols,
mat.rows,
QImage::Format_RGB888));
m_image = m_pixmap.toImage();
if (m_pixmap.height() > FIXED_HEIGHT)
m_pixmap = m_pixmap.scaledToHeight(FIXED_HEIGHT);
this->repaint();
}
///////////////////////////////////////////////////////////
void ImageContainer::paintEvent(QPaintEvent* event)
{
if (m_pixmap.isNull())
return;
QPainter painter;
painter.begin(this);
painter.drawPixmap(0, 0, m_pixmap);
if (m_selectionOn)
painter.fillRect(m_selectionRect, QColor(0, 0, 255, 100));
else if (m_highlightOn)
painter.fillRect(m_selectionRect, QColor(0, 255, 0, 100));
painter.end();
}
///////////////////////////////////////////////////////////
QRect& ImageContainer::selection()
{
return m_selectionRect;
}
///////////////////////////////////////////////////////////
bool ImageContainer::isLoaded() const
{
return !m_pixmap.isNull();
}
///////////////////////////////////////////////////////////
QSize ImageContainer::size() const
{
return m_image.size();
}
///////////////////////////////////////////////////////////
cv::Mat* ImageContainer::selectionToMat()
{
if (m_selectionRect.isEmpty())
return NULL;
cv::Rect rect = cv::Rect(m_selectionRect.x() * m_scale.width(),
m_selectionRect.y() * m_scale.height(),
m_selectionRect.width() * m_scale.width(),
m_selectionRect.height() * m_scale.height());
cv::Mat& image = *toMat();
cv::Mat* mat = new cv::Mat(image.rows, image.cols, image.type());
cv::Mat cropped = image(rect);
cropped.copyTo(*mat);
return mat;
}
///////////////////////////////////////////////////////////
void ImageContainer::highlight(const QRect& selection, unsigned int x, unsigned int y)
{
m_selectionOn = false;
m_highlightOn = true;
QPoint p(x / m_scale.width(), y / m_scale.height());
m_selectionRect = QRect(p, selection.size());
repaint();
}