-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscribblearea.cpp
More file actions
204 lines (171 loc) · 5.01 KB
/
Copy pathscribblearea.cpp
File metadata and controls
204 lines (171 loc) · 5.01 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "scribblearea.h"
#include "ui_scribblearea.h"
ScribbleArea::ScribbleArea(QWidget *parent) :
QWidget(parent),
ui(new Ui::ScribbleArea)
{
ui->setupUi(this);
isScribbling = false;
}
ScribbleArea::~ScribbleArea()
{
delete ui;
}
bool ScribbleArea::openImage(const QString &fileName)
{
isModified = false;
isImageOpend = true;
isScribbling = false;
tempBonePoint.setX(0);
tempBonePoint.setY(0);
qInfo() << "Open Image:" << fileName;
QImage loadedImage;
if (!loadedImage.load(fileName))
return false;
originalImage = loadedImage;
resizeOriginalImage();
update();
return true;
}
void ScribbleArea::setZoomScale(int scale)
{
zoomScale = scale;
}
void ScribbleArea::setIsImageOpened(bool value)
{
isImageOpend = value;
}
void ScribbleArea::resizeOriginalImage()
{
resizeImage(&originalImage, &displayImage, zoomScale);
drawBone();
update();
}
std::vector<std::vector<QPoint>> *ScribbleArea::getBoneVector()
{
return &boneVector;
}
cv::Mat ScribbleArea::getResultImage()
{
cv::Mat displayMat = qImageToMat(&displayImage);
return displayMat;
}
void ScribbleArea::paintEvent(QPaintEvent *event)
{
QRect dirtyRect = event->rect();
QPainter painter(this);
painter.drawImage(dirtyRect, displayImage, dirtyRect);
}
void ScribbleArea::mousePressEvent(QMouseEvent *event)
{
if (isImageOpend && isClickedOnImage(event->pos()))
{
if (event->button() == Qt::RightButton)
{
boneVector.push_back(std::vector<QPoint>());
boneVector.back().push_back(event->pos() / zoomScale);
isScribbling = true;
}
if (event->button() == Qt::LeftButton)
{
if (boneVector.size() > 0)
boneVector.back().push_back(event->pos() / zoomScale);
}
drawBone();
///
//test_ShowBoneList();
}
}
void ScribbleArea::mouseMoveEvent(QMouseEvent *event)
{
if (isImageOpend && isClickedOnImage(event->pos()) && isScribbling)
{
tempBonePoint = event->pos();
drawBone();
}
else
{
tempBonePoint.setX(0);
tempBonePoint.setY(0);
}
//qInfo() << "mouseMoveEvent:" << tempBonePoint << "isImageOpend =" << isImageOpend << "isClickedOnImage =" << isClickedOnImage(event->pos()) << "isScribbling =" << isScribbling;
}
void ScribbleArea::resizeImage(QImage *sourceImage, QImage *resizedImage, int scale)
{
cv::Mat sourceMat = qImageToMat(sourceImage);
cv::Mat resizedMat;
cv::resize(sourceMat, resizedMat, cv::Size(0, 0), scale, scale);
*resizedImage = matToQImage(&resizedMat);
}
cv::Mat ScribbleArea::qImageToMat(QImage *qImage)
{
return cv::Mat(qImage->height(), qImage->width(),
CV_8UC4, qImage->bits(), qImage->bytesPerLine());
}
QImage ScribbleArea::matToQImage(cv::Mat *mat)
{
return QImage(mat->data, mat->cols, mat->rows, mat->step, QImage::Format_RGB32);
}
bool ScribbleArea::isClickedOnImage(QPoint pos)
{
if ((pos.x() <= displayImage.width()) && (pos.y() <= displayImage.height()))
return true;
return false;
}
void ScribbleArea::drawBone()
{
resizeImage(&originalImage, &displayImage, zoomScale);
int lineWidth = 3 * zoomScale;
int pointWidth = 6 * zoomScale;
int hue = 0;
int rad;
QPainter painter(&displayImage);
QColor color;
QPoint startPoint;
QPoint endPoint;
isModified = true;
for (int i = 0; i < (int)boneVector.size(); i++)
for (int j = 0; j < (int)boneVector[i].size(); j++, hue += 15)
{
hue = std::min(hue, 344);
color.setAlpha(255);
painter.setPen(QPen(color.fromHsv(hue, 255, 255), lineWidth,
Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
if (j == 0)
{
startPoint = boneVector[i][j] * zoomScale;
endPoint = startPoint;
}
else
{
startPoint = boneVector[i][j - 1] * zoomScale;
endPoint = boneVector[i][j] * zoomScale;
painter.drawLine(startPoint, endPoint);
}
painter.setPen(QPen(color.fromHsv(hue, 255, 255), pointWidth,
Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
painter.drawPoint(endPoint);
rad = (pointWidth / 2) + 2;
}
if (!tempBonePoint.isNull())
{
color = color.fromHsv(hue, 255, 255);
color.setAlpha(50);
painter.setPen(QPen(color, lineWidth,
Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
//qInfo() << "endPoint: " << endPoint << "tempBonePoint: " << tempBonePoint;
painter.drawLine(endPoint, tempBonePoint);
}
update();
}
///
void ScribbleArea::test_ShowBoneList()
{
for (int i = 0; i < (int)boneVector.size(); i++)
{
for (int j = 0; j < (int)boneVector[i].size(); j++)
{
qInfo() << "boneVector" << i << j << "=" << boneVector[i][j];
}
}
}