-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
193 lines (177 loc) · 5.5 KB
/
mainwindow.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
187
188
189
190
191
192
193
/*
* author : fang
* date : 2022-3
*/
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QFileDialog>
#include <QDebug>
#define cout qDebug()
#include <QDir>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
pointMark = QVector<QPoint>(21);
connect(ui->pictureLabel,&MyLabel::sendPoint,this,&MainWindow::receivePoint);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
openFileName = QFileDialog::getExistingDirectory(
this, "choose src Directory",
"F:/hands");
QDir dir(openFileName); //获取文件夹下所有文件
//获取文件夹下所有文件(文件夹+文件)
QFileInfoList list = dir.entryInfoList();
QString filter;
//必须保证图片和数据集名字相同
for(int i = 0; i < list.size(); i++)
{
filter = list[i].suffix(); //后缀名
if(filter != "jpg")
continue;
QStringList lt = list[i].fileName().split(".");
allImageName.append(lt[0]);
}
if(allImageName.empty()) {
return ;
}
pictureNum = 0;
show_picture(); //加载图片
}
void MainWindow::show_picture()
{
if(pictureNum < 0 || pictureNum >= allImageName.size())
return ;
updateNameLabel();
image = QPixmap(QString("%1/%2.jpg").arg(openFileName).arg(allImageName[pictureNum]));
image_size = image.size(); //获取图片尺寸
ui->pictureLabel->mySetPixmap(image); //显示图片
// ui->pictureLabel->setPixmap(image);
// ui->pictureLabel->setScaledContents(true); //自适应
readJsonFile(); //读json文件
ui->pictureLabel->getPointMark(pointMark, image_size);
updateListWidget(); //更新显示
}
void MainWindow::readJsonFile()
{
//读json文件
QFile file(QString("%1/%2.json").arg(openFileName).arg(allImageName[pictureNum]));
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
file.close();
//使用json文件对象加载字符串
QJsonDocument doc = QJsonDocument::fromJson(data);
for(int i = 0; i < 21; ++i) {
QPoint point;
point.setX(doc["info"][0]["pts"][QString::number(i)]["x"].toInt());
point.setY(doc["info"][0]["pts"][QString::number(i)]["y"].toInt());
pointMark[i] = point;
}
}
void MainWindow::writeJsonFile()
{
QString path = QString("%1/%2.json").arg(openFileName).arg(allImageName[pictureNum]);
//写json文件
QFile file(path);
file.open(QFile::ReadOnly | QFile::Text);
QByteArray data = file.readAll();
file.close();
//使用json文件对象加载字符串
QJsonDocument doc = QJsonDocument::fromJson(data);
QJsonObject obj = doc.object();
QJsonArray infoArr = obj.value("info").toArray();
QJsonObject infoObj = infoArr[0].toObject();
QJsonObject ptsObj = infoObj.value("pts").toObject();
for(int i = 0; i < 21; ++i) {
QJsonObject numObj = ptsObj.value(QString::number(i)).toObject();
numObj["x"] = pointMark[i].rx();
numObj["y"] = pointMark[i].ry();
ptsObj[QString::number(i)] = numObj;
}
infoObj["pts"] = ptsObj;
infoArr[0] = infoObj;
obj["info"] = infoArr;
doc.setObject(obj);
file.setFileName(path);
file.open(QFile::WriteOnly | QFile::Text);
file.write(doc.toJson());
file.close();
ui->nameLabel->setText("保存成功!");
}
void MainWindow::updateListWidget()
{
//更新ListWidget
ui->listWidget->clear();
for(int i = 0; i < 21; ++i) {
ui->listWidget->addItem(QString("%1 : %2 %3").arg(QString::number(i + 1))
.arg(QString::number(pointMark[i].rx())).arg(QString::number(pointMark[i].ry())));
}
// drawPointOnPicture();
}
void MainWindow::drawPointOnPicture()
{
QPixmap img(image);
QPainter painter(&img); // 创建QPainter一个对象
QPen pen;
pen.setColor(Qt::red); // 设置画笔颜色
painter.setPen(pen); // 设置画笔
painter.setBrush(QColor(255,0,0)); //设置画刷
// 画多个点
for (int i = 0; i < 21; ++i)
{
painter.drawEllipse(pointMark[i], 3, 3);
}
// 连线
QPoint last;
// 拇指
for(int i = 0; i < 21; ++i) {
if(i != 0) {
QPoint point = pointMark[i];
QLineF line(last, point);
painter.drawLine(line);
last = point;
}
if(i % 4 == 0) {
if(i != 20) pen.setColor(lineColor[i / 4]);
painter.setPen(pen);
last = pointMark[0];
}
}
ui->pictureLabel->setPixmap(img);
}
void MainWindow::on_nextButton_clicked()
{
if(pictureNum >= allImageName.size() - 1)
return;
pictureNum++;
show_picture();
}
void MainWindow::receivePoint(QVector<QPoint> p)
{
pointMark = p;
updateListWidget();
// writeJsonFile();
}
void MainWindow::on_preButton_clicked()
{
if(pictureNum <= 0)
return;
pictureNum--;
show_picture();
}
void MainWindow::updateNameLabel()
{
ui->nameLabel->setText(QString("%1 / %2 , %3").arg(QString::number(pictureNum + 1))
.arg(QString::number(allImageName.size())).arg(allImageName[pictureNum] + ".jpg"));
}
void MainWindow::on_saveButton_clicked()
{
writeJsonFile();
}