-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
277 lines (257 loc) · 11.9 KB
/
mainwindow.cpp
File metadata and controls
277 lines (257 loc) · 11.9 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "mainwindow.h"
#include <fftw3.h>
#include <QTime>
#include <QVector>
#include "./ui_mainwindow.h"
#include "ImageProcess/ImageProcess.h"
#include "MatQueue/SingletonMatQueue.h"
#include "Project_Config.h"
#include "settingconfig.h"
/* 2023/11/19
* 线程池的设计
* 1.此项目涉及到 图像采集 图像处理 图像展示 数据表格展示
* 在其中,图像采集和图像展示分别对应队列的enqueue和dequeue,因而选择通过图像队列进行缓存操作
* 2.设计思路,主线程负责界面维持和子线程的调度任务
* 子线程(1)负责图像数据的采集,不断地进行enqueue操作。
* 子线程(2)负责图像数据的处理,在初始图像队列进行dequeue操作,对处理后的图像队列进行enqueue因为耗时长比较长,工作量多,使用多个线程进行维持
* 子线程(3)负责图像数据的显示,不断的对(2)维持的处理后图像队列进行dequeue操作进行显示。
* 子线程(4)负责接受图像数据处理后的结果,使用vector动态数组进行pushback存储,并实时显示在图像窗口上。
* 3.设计细节
* 1.队列操作需要进行加锁设计,否则会导致ThreadUnsafeProblem
* 2.子线程(1)入队操作后,队列为按时间排序的有序队列,子线程(2)如果有多个线程进行处理图像算法的话,会导致图像顺序错位,
* 因而选择针对一张照片的多线程处理办法。
* 3.子线程(3)负责对图像的显示,因而对(2)输出的图像有可排序、可索引或者为按照时间的正序排列
* 4.子线程(4)为子线程(3)获得的同步线程操作,因而针对子线程(3)和(4)需要进行线程同步。
* 4.实现路径
* 通过QThreadPool单例类,创建多个子处理类,在主线程安排入池逻辑进行Start操作。
*
* 2023/11/20
* 线程池的优化
* 1.考虑采用C++ priority_queue进行数据排序,在队列操作中采用struct
* {cv::mat ,QTime}进行数据标识,并通过id进行排序
* 2.线程池目前测试无任何非期望运行情况,但是任有一下需要优化的细节
* 2.1 线程同步设计
* 线程池在运行任务的过程中往往是从队列中获取一个完整的处理任务进行测试,因而不符合提速的基本要求,期望可以拆分处理任务以达到线程高效利用的目的
* 线程池在运行的过程尽管已经进行了优化设计,但仍然只有单个线程加入池中
* 2.2 待定....
* */
#define MESSAGESHOW(str) ui->textBrowser->append('[' + QTime::currentTime().toString() + "] >>" + (str));
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
// int Initialization
qDebug() << "MainThread" << QThread::currentThreadId();
qDebug() << fftw_version;
m_captureNumber = RefreshCameraNum(&m_init_size);
RefreshCaptureSelect();
// object Initialization
threadVideoShowTask = new QThread;
threadVideoTask = new QThread;
// axesFreshTask = new AxesFreshTask(ui->chartView);
m_captureTask = new CaptureTask;
m_captureShowTask = new CaptureShowTask(ui->VideoShow);
m_captureShowTask->moveToThread(threadVideoShowTask);
m_captureTask->moveToThread(threadVideoTask);
initializeMemberMap();
try {
auto file = QFile(CONFIG_PATH + "setting.json");
ansys_setting = new AnsysSetting(file);
ansys_setting->ansys(ui->tools_widget);
} catch (char *e) {
qDebug() << "Char Exception: " << e;
exit(-1);
} catch (const std::exception &e) {
qDebug() << "Standard Exception: " << e.what();
exit(-1);
} catch (...) {
qDebug() << "Unknown Exception";
exit(-1);
}
Ui_Init(m_init_size);
MESSAGESHOW(tr("测试"));
m_settingDialog = new SettingConfig(this);
SingletonMatQueue::GetInstance();
m_wait_send_rect = new cv::Rect();
// connect
connect(this, QOverload<int>::of(&MainWindow::thisCapture), m_captureTask, &CaptureTask::getCaptureNumber);
connect(this, &MainWindow::alg_selected, m_captureShowTask, &CaptureShowTask::algChanged);
connect(this, &MainWindow::switchCapture, m_captureTask, &CaptureTask::getCaptureStatus);
connect(this, &MainWindow::switchCapture, m_captureShowTask, &CaptureShowTask::getCaptureStatus, Qt::QueuedConnection);
connect(this, QOverload<QString>::of(&MainWindow::SendVideoFileName), m_captureTask, &CaptureTask::SetVideo);
connect(this, &MainWindow::SendFpsNumber, m_captureShowTask, &CaptureShowTask::GetFpsNumber, Qt::QueuedConnection);
connect(this, &MainWindow::SendFpsNumber, m_captureTask, &CaptureTask::GetFpsNumber, Qt::QueuedConnection);
connect(m_captureTask, &CaptureTask::videoFPS, m_captureShowTask, &CaptureShowTask::getVideoFPS);
// connect(m_captureShowTask, &CaptureShowTask::EmitDoubleArg,
// axesFreshTask,
// &AxesFreshTask::axesFreshByDouble, Qt::QueuedConnection);
connect(this, &MainWindow::send_rect, m_captureShowTask, &CaptureShowTask::showTaskGetRect);
connect(this, &MainWindow::send_rect, m_captureTask, &CaptureTask::taskGetRect);
connect(m_captureTask, &CaptureTask::VideoOver, this, [this]() { on_pushButtonClose_clicked(); });
connect(m_captureShowTask, &CaptureShowTask::SingletonMatError, this, [this]() {
QMessageBox::warning(this, tr("singleton mat crushed"), tr("restart the application"));
SingletonMatQueue::GetInstance()->ClearAllQueue();
});
connect(ansys_setting->m_button, &QPushButton::clicked, this, [this]() { set_video_show(this->m_init_size, true); });
threadVideoTask->start(QThread::HighestPriority);
threadVideoShowTask->start(QThread::HighPriority);
for (int i = 0; i < axesThreadListLength; i++) {
axesThreadList.append(new QThread(this));
}
}
void MainWindow::Ui_Init(QSize size) {
set_video_show(size);
for (auto &name : ImageProcess::GetInstance().ImageProcessList) {
auto action = new QAction(ui->menuSelect);
action->setText(tr(name.toUtf8()));
ui->menuSelect->addAction(action);
connect(action, QOverload<bool>::of(&::QAction::triggered), m_captureShowTask, [action, this](bool) {
emit alg_selected(action->text());
on_pushButtonClose_clicked();
});
if (action->text() != ImageProcess::GetInstance().getName())
connect(action, &QAction::triggered, ansys_setting, &AnsysSetting::reDraw);
}
}
void MainWindow::initializeMemberMap() {
memberMap["ansys_setting"] = ansys_setting;
memberMap["threadVideoShowTask"] = threadVideoShowTask;
memberMap["threadVideoTask"] = threadVideoTask;
memberMap["m_captureShowTask"] = m_captureShowTask;
memberMap["m_captureTask"] = m_captureTask;
memberMap["m_settingDialog"] = m_settingDialog;
}
QObject *MainWindow::get_member(QString memberName) {
if (memberMap.contains(memberName)) {
return memberMap[memberName];
}
throw std::runtime_error("noMember");
}
void MainWindow::set_video_show(QSize size, bool is_signal) {
QImage image;
cv::Mat mat = cv::Mat::zeros(cv::Size(size.width(), size.height()), CV_8UC1);
if (!is_signal) {
image = ImageProcess::GetInstance().cvMatToQImage(mat);
} else {
QVector<QString> strs{"x", "y", "width", "height"};
auto args = this->ansys_setting->get_config(strs);
qDebug() << args;
*m_wait_send_rect = cv::Rect(args[0], args[1], args[2], args[3]);
cv::rectangle(mat, *m_wait_send_rect, cv::Scalar(255, 255, 255));
image = ImageProcess::GetInstance().cvMatToQImage(mat);
emit send_rect(m_wait_send_rect);
}
this->ui->VideoShow->setPixmap(QPixmap::fromImage(image));
this->ui->VideoShow->setFixedSize(size);
}
MainWindow::~MainWindow() {
on_pushButtonClose_clicked();
threadVideoShowTask->quit();
threadVideoShowTask->wait();
threadVideoShowTask->deleteLater();
threadVideoTask->quit();
threadVideoTask->wait();
threadVideoTask->deleteLater();
delete ansys_setting;
delete m_captureShowTask;
delete m_captureTask;
delete m_wait_send_rect;
delete ui;
}
void MainWindow::on_pushButtonOpen_clicked() {
if (m_captureOpenFlag) return;
axesTaskList = ansys_setting->chart_map->value(ImageProcess::GetInstance().getName());
foreach (auto &axes, axesTaskList) {
if (!axes.second->getOnTime()) continue;
axes.second->moveToThread(axesThreadList[axesThreadListCount]);
axesThreadList[axesThreadListCount++]->start();
}
m_captureOpenFlag = true;
SingletonMatQueue::GetInstance()->ClearAllQueue();
emit switchCapture(m_captureOpenFlag);
emit is_open(true);
MESSAGESHOW(tr("摄像头开启"));
}
void MainWindow::on_pushButtonClose_clicked() {
if (!m_captureOpenFlag) return;
for (auto &thread : axesThreadList) {
if (thread->isRunning()) thread->quit();
}
m_captureOpenFlag = false;
emit switchCapture(m_captureOpenFlag);
m_videoFileFlag = false;
set_video_show(m_init_size);
MESSAGESHOW(tr("摄像头关闭"));
emit ShowFFT();
emit is_open(false);
}
void MainWindow::on_actionFPS_triggered() {
bool checkConfig = false;
m_fpsConfig = QInputDialog::getInt(this, tr("输入FPS"), tr("输入Int"), m_fpsConfig, 1, 1000, 1, &checkConfig);
if (!checkConfig) QMessageBox::warning(this, tr("非法输入"), tr("输入值无效!"));
MESSAGESHOW("FPS:" + QString::number(m_fpsConfig));
emit SendFpsNumber(m_fpsConfig);
on_pushButtonClose_clicked();
}
void MainWindow::on_actionImportVideos_triggered() {
m_videoFileName = QFileDialog::getOpenFileName(this, tr("打开视频文件"), "C:/", tr("video files(*.avi *mp4);;All files(*.*)"));
if (m_videoFileName.isEmpty()) {
QMessageBox::warning(this, "Warning", "Failed to find");
return;
}
MESSAGESHOW(m_videoFileName);
auto tmp = cv::VideoCapture(m_videoFileName.toStdString());
if (tmp.isOpened()) {
cv::Mat mat;
tmp.read(mat);
m_init_size = ImageProcess::GetInstance().cvMatToQImage(mat).size();
set_video_show(m_init_size);
tmp.release();
} else {
tmp.release();
QMessageBox::warning(this, "Warning", "Failed to Open the video");
return;
}
emit SendVideoFileName(m_videoFileName);
m_videoFileFlag = true;
}
std::vector<int> MainWindow::RefreshCameraNum(QSize *size) {
auto *tmp_capture = new cv::VideoCapture;
int _count;
bool is_default = true;
std::vector<int> __v;
for (_count = 0; _count < MAX_CAPTURE_NUM; _count++) {
tmp_capture->open(_count);
if (!tmp_capture->isOpened()) {
tmp_capture->release();
continue;
}
if (is_default) {
cv::Mat mat;
tmp_capture->read(mat);
auto s = ImageProcess::GetInstance().cvMatToQImage(mat).size();
size->setHeight(s.height());
size->setWidth(s.width());
is_default = ~is_default;
}
__v.push_back(_count);
tmp_capture->release(); // 一定要释放 否则程序进程不能完全退出
}
delete tmp_capture;
return __v;
}
void MainWindow::RefreshCaptureSelect() {
for (int _index : m_captureNumber) {
auto *CAPTURE_ACTION_CREATE(_index) = new QAction(QString::number(_index), ui->menuCaptureSelect);
ui->menuCaptureSelect->addAction(CAPTURE_ACTION_CREATE(_index));
connect(CAPTURE_ACTION_CREATE(_index), QOverload<bool>::of(&::QAction::triggered), this, [=](bool) {
MainWindow::m_selectedCapture = _index;
MESSAGESHOW(tr("摄像头") + QString::number(m_selectedCapture) + tr("启用"));
on_pushButtonClose_clicked();
emit thisCapture(m_selectedCapture);
});
}
}
void MainWindow::on_actionSetting_triggered() {
on_pushButtonClose_clicked();
m_settingDialog->show();
m_settingDialog->setModal(true);
}