-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdrawlabel.cpp
182 lines (167 loc) · 5.01 KB
/
drawlabel.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
#include "drawlabel.h"
#include <QMouseEvent>
#include <QPaintEvent>
#include <QWheelEvent>
#include <QDebug>
#include <QPainter>
#include <QSet>
DrawLabel::DrawLabel(QWidget *parent) :
QLabel(parent)
{
mw = NULL;
}
void DrawLabel::mousePressEvent(QMouseEvent *ev)
{
if(mw==NULL) {
return;
}
if(mw->isDrawMode()) {
switch (ev->button()) {
case Qt::LeftButton:
if(ev->modifiers().testFlag(Qt::ControlModifier)) {
qDebug() << "Qt::LeftButton + Ctrl";
appendClusterOnPosition(ev->x(), ev->y());
} else {
qDebug() << "Qt::LeftButton";
listPointsOCV.push_back(cv::Point(ev->x(), ev->y()));
listLabelsOCV.push_back(mw->currentClassIdx);
}
update();
break;
case Qt::RightButton:
qDebug() << "Qt::RightButton";
tryToRemovePoints(ev->x(), ev->y());
update();
break;
case Qt::MiddleButton:
qDebug() << "Qt::MiddleButton";
appendClusterOnPosition(ev->x(), ev->y());
update();
break;
default:
break;
}
}
}
void DrawLabel::wheelEvent(QWheelEvent *ev)
{
if(ev->delta()>0) {
qDebug() << "+";
mw->goToPrevClassIdx();
} else {
qDebug() << "-";
mw->goToNextClassIdx();
}
}
void DrawLabel::paintEvent(QPaintEvent *ev)
{
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
if(mw!=NULL) {
if(!mw->isDrawMode()) {
}
}
drawPoints(p);
}
void DrawLabel::resizeEvent(QResizeEvent *ev)
{
// std::vector<cv::Point> tmpListPointsOCV;
// std::vector<int> tmpListLabelsOCV;
}
void DrawLabel::setMainWindowPtr(MainWindow *mw)
{
this->mw = mw;
}
void DrawLabel::setPixmap(const QPixmap &pxm)
{
this->pxm = pxm;
}
void DrawLabel::clearPoints()
{
listPointsOCV.clear();
listLabelsOCV.clear();
listExtPoints.clear();
pxm.fill(Qt::white);
update();
}
void DrawLabel::drawPoints(QPainter& pnt)
{
if(mw!=NULL) {
if(mw->isDrawMode() || (pxm.width()<1) ) {
pnt.fillRect(rect(), QBrush(Qt::white));
} else {
pnt.drawPixmap(0,0, pxm);
}
QPen pen(Qt::black);
QBrush brush(Qt::SolidPattern);
pnt.setPen(pen);
for(uint ii=0; ii<listPointsOCV.size(); ii++) {
const cv::Point& p = listPointsOCV.at(ii);
const int cIdx = listLabelsOCV.at(ii);
brush.setColor(mw->listClassColors.at(cIdx));
pnt.setBrush(brush);
pnt.drawEllipse(QRect(p.x-DEF_POINT_RADIUS, p.y-DEF_POINT_RADIUS, 2*DEF_POINT_RADIUS, 2*DEF_POINT_RADIUS));
}
// draw ext-points:
int extRadMax = DEF_POINT_RADIUS-2;
int extRadMin = DEF_POINT_RADIUS-4;
for(uint ii=0; ii<listExtPoints.size(); ii++) {
const cv::Point& p = listExtPoints.at(ii);
brush.setColor(Qt::white);
pnt.setBrush(brush);
pnt.drawEllipse(QRect(p.x-extRadMax, p.y-extRadMax, 2*extRadMax, 2*extRadMax));
brush.setColor(Qt::black);
pnt.setBrush(brush);
pnt.drawEllipse(QRect(p.x-extRadMin, p.y-extRadMin, 2*extRadMin, 2*extRadMin));
}
}
}
void DrawLabel::tryToRemovePoints(int x, int y)
{
std::vector<cv::Point> newListPoints;
std::vector<int> newListLabels;
double r2 = DEF_POINT_RADIUS_RM*DEF_POINT_RADIUS_RM;
for(int ii=0; ii<listPointsOCV.size(); ii++) {
const cv::Point& p = listPointsOCV.at(ii);
double dx = (double)(x-p.x);
double dy = (double)(y-p.y);
if(r2<(dx*dx+dy*dy)) {
newListPoints.push_back(listPointsOCV.at(ii));
newListLabels.push_back(listLabelsOCV.at(ii));
}
}
listPointsOCV = newListPoints;
listLabelsOCV = newListLabels;
}
void DrawLabel::appendClusterOnPosition(int x, int y)
{
int numCluster = mw->getClusterSize();
int sizCluster = mw->getClusterRadius();
int cIdx = mw->currentClassIdx;
for(int ii=0; ii<numCluster; ii++) {
int px = x + cv::theRNG().gaussian(sizCluster);
int py = y + cv::theRNG().gaussian(sizCluster);
listPointsOCV.push_back(cv::Point(px, py));
listLabelsOCV.push_back(cIdx);
}
}
void DrawLabel::refreshValidClasses()
{
if(mw!=NULL) {
int numOfClass = mw->getNumberOfClasses();
std::vector<cv::Point> newListPoints;
std::vector<int> newListLabels;
for(int ii=0; ii<listPointsOCV.size(); ii++) {
const cv::Point& pnt = listPointsOCV.at(ii);
int cIdx = listLabelsOCV.at(ii);
if(cIdx<numOfClass) {
newListPoints.push_back(pnt);
newListLabels.push_back(cIdx);
}
}
listPointsOCV.clear();
listLabelsOCV.clear();
listPointsOCV = newListPoints;
listLabelsOCV = newListLabels;
}
}