1 |
jakw |
1 |
#include "SMScanner.h"
|
|
|
2 |
#include "ui_SMScanner.h"
|
|
|
3 |
|
4 |
jakw |
4 |
#include <QMetaObject>
|
|
|
5 |
|
27 |
jakw |
6 |
SMScanner::SMScanner(QWidget *parent) :QMainWindow(parent), ui(new Ui::SMScanner),
|
|
|
7 |
calibrationReviewMode(false), captureReviewMode(false){
|
1 |
jakw |
8 |
ui->setupUi(this);
|
4 |
jakw |
9 |
|
27 |
jakw |
10 |
// Register metatypes
|
|
|
11 |
qRegisterMetaType<cv::Mat>("cv::Mat");
|
|
|
12 |
qRegisterMetaType< std::vector<cv::Mat> >("std::vector<cv::Mat>");
|
|
|
13 |
qRegisterMetaType<SMCalibrationSet>("SMCalibrationSet");
|
|
|
14 |
qRegisterMetaType<SMCalibrationParameters>("SMCalibrationParameters");
|
|
|
15 |
qRegisterMetaTypeStreamOperators<SMCalibrationParameters>("SMCalibrationParameters");
|
|
|
16 |
qRegisterMetaType< std::vector<SMCalibrationSet> >("std::vector<SMCalibrationSet>");
|
|
|
17 |
qRegisterMetaType<SMFrameSequence>("SMFrameSequence");
|
|
|
18 |
|
4 |
jakw |
19 |
// Restore geometry
|
|
|
20 |
this->restoreGeometry(settings.value("geometry/mainwindow").toByteArray());
|
|
|
21 |
this->restoreState(settings.value("state/mainwindow").toByteArray());
|
|
|
22 |
|
|
|
23 |
// Set up threads
|
|
|
24 |
captureWorker = new SMCaptureWorker;
|
|
|
25 |
captureWorkerThread = new QThread(this);
|
|
|
26 |
captureWorkerThread->setObjectName("captureWorkerThread");
|
|
|
27 |
captureWorker->moveToThread(captureWorkerThread);
|
|
|
28 |
captureWorkerThread->start();
|
|
|
29 |
|
|
|
30 |
// Connections
|
23 |
jakw |
31 |
connect(captureWorker, SIGNAL(newFrame(unsigned int, cv::Mat)), this, SLOT(onReceiveFrame(unsigned int, cv::Mat)));
|
27 |
jakw |
32 |
connect(captureWorker, SIGNAL(newCalibrationSet(SMCalibrationSet)), this, SLOT(onReceiveCalibrationSet(SMCalibrationSet)));
|
|
|
33 |
connect(captureWorker, SIGNAL(newFrameSequence(SMFrameSequence)), this, SLOT(onReceiveFrameSequence(SMFrameSequence)));
|
4 |
jakw |
34 |
|
|
|
35 |
// Start capturing
|
|
|
36 |
QMetaObject::invokeMethod(captureWorker, "setup");
|
|
|
37 |
QMetaObject::invokeMethod(captureWorker, "doWork");
|
|
|
38 |
|
1 |
jakw |
39 |
}
|
|
|
40 |
|
23 |
jakw |
41 |
void SMScanner::onReceiveFrame(unsigned int camId, cv::Mat frame){
|
4 |
jakw |
42 |
|
23 |
jakw |
43 |
if(camId == 0){
|
25 |
jakw |
44 |
if(!calibrationReviewMode)
|
|
|
45 |
ui->calibrationCamera0Widget->showImageCV(frame);
|
27 |
jakw |
46 |
if(!captureReviewMode)
|
|
|
47 |
ui->captureCamera0Widget->showImageCV(frame);
|
23 |
jakw |
48 |
} else if(camId == 1){
|
25 |
jakw |
49 |
if(!calibrationReviewMode)
|
|
|
50 |
ui->calibrationCamera1Widget->showImageCV(frame);
|
27 |
jakw |
51 |
if(!captureReviewMode)
|
|
|
52 |
ui->captureCamera1Widget->showImageCV(frame);
|
23 |
jakw |
53 |
}
|
|
|
54 |
}
|
4 |
jakw |
55 |
|
23 |
jakw |
56 |
void SMScanner::on_actionPreferences_triggered(){
|
4 |
jakw |
57 |
|
|
|
58 |
preferenceDialog.show();
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
void SMScanner::closeEvent(QCloseEvent *event){
|
|
|
62 |
|
23 |
jakw |
63 |
// Stop capturing thread
|
|
|
64 |
connect(captureWorker, SIGNAL(finished()), captureWorker, SLOT(deleteLater()));
|
|
|
65 |
connect(captureWorker, SIGNAL(finished()), captureWorkerThread, SLOT(quit()));
|
|
|
66 |
QMetaObject::invokeMethod(captureWorker, "stopWork");
|
|
|
67 |
captureWorkerThread->quit();
|
|
|
68 |
captureWorkerThread->wait();
|
|
|
69 |
|
4 |
jakw |
70 |
// Save window geometry
|
|
|
71 |
settings.setValue("geometry/mainwindow", this->saveGeometry());
|
|
|
72 |
settings.setValue("state/mainwindow", this->saveState());
|
|
|
73 |
|
|
|
74 |
event->accept();
|
|
|
75 |
|
|
|
76 |
}
|
|
|
77 |
|
2 |
jakw |
78 |
SMScanner::~SMScanner(){
|
1 |
jakw |
79 |
delete ui;
|
|
|
80 |
}
|
4 |
jakw |
81 |
|
23 |
jakw |
82 |
void SMScanner::on_singleCalibrationButton_clicked(){
|
|
|
83 |
|
25 |
jakw |
84 |
// If in review mode, go back to aquisition mode
|
|
|
85 |
if(calibrationReviewMode){
|
26 |
jakw |
86 |
ui->calibrationListWidget->clearSelection();
|
25 |
jakw |
87 |
ui->singleCalibrationButton->setText("Single Aquisition");
|
|
|
88 |
ui->batchCalibrationButton->setText("Batch Aquisition");
|
|
|
89 |
calibrationReviewMode = false;
|
|
|
90 |
return;
|
|
|
91 |
}
|
|
|
92 |
|
27 |
jakw |
93 |
QMetaObject::invokeMethod(captureWorker, "acquireCalibrationSet", Q_ARG(float, -1.0));
|
23 |
jakw |
94 |
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
void SMScanner::on_calibrationRotationDial_sliderReleased(){
|
27 |
jakw |
98 |
|
23 |
jakw |
99 |
float angle = ui->calibrationRotationDial->value();
|
|
|
100 |
std::cout << "Rotation stage target: " << angle << std::endl;
|
|
|
101 |
QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
|
27 |
jakw |
102 |
|
|
|
103 |
ui->captureRotationDial->setValue(ui->calibrationRotationDial->value());
|
23 |
jakw |
104 |
}
|
24 |
jakw |
105 |
|
27 |
jakw |
106 |
void SMScanner::onReceiveCalibrationSet(SMCalibrationSet calibrationSet){
|
23 |
jakw |
107 |
calibrationData.push_back(calibrationSet);
|
25 |
jakw |
108 |
|
|
|
109 |
// Add identifier to list
|
27 |
jakw |
110 |
QListWidgetItem* item = new QListWidgetItem(QString("Set %1 -- %2 deg").arg(ui->calibrationListWidget->count()).arg(calibrationSet.rotationAngle), ui->calibrationListWidget);
|
25 |
jakw |
111 |
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
|
|
|
112 |
item->setCheckState(Qt::Checked);
|
|
|
113 |
ui->calibrationListWidget->addItem(item);
|
|
|
114 |
|
|
|
115 |
// Set enabled checkmark
|
|
|
116 |
if(calibrationData.size() >= 2)
|
|
|
117 |
ui->calibrateButton->setEnabled(true);
|
23 |
jakw |
118 |
}
|
25 |
jakw |
119 |
|
27 |
jakw |
120 |
void SMScanner::on_calibrateButton_clicked(){
|
25 |
jakw |
121 |
|
27 |
jakw |
122 |
|
|
|
123 |
// set checked flags
|
|
|
124 |
for(int i=0; i<calibrationData.size(); i++){
|
|
|
125 |
calibrationData[i].checked = (ui->calibrationListWidget->item(i)->checkState() == Qt::Checked);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
// SMCalibrationWorker calibrationWorker;
|
|
|
129 |
// calibrationWorker.performCalibration(calibrationData);
|
|
|
130 |
|
|
|
131 |
// Set up calibration thread
|
|
|
132 |
calibrationWorker = new SMCalibrationWorker;
|
|
|
133 |
calibrationWorkerThread = new QThread(this);
|
|
|
134 |
calibrationWorkerThread->setObjectName("calibrationWorkerThread");
|
|
|
135 |
calibrationWorker->moveToThread(captureWorkerThread);
|
|
|
136 |
calibrationWorkerThread->start();
|
|
|
137 |
|
|
|
138 |
// Connections
|
|
|
139 |
connect(calibrationWorker, SIGNAL(newSetProcessed(int)), this, SLOT(onSMCalibrationSetProcessed(int)));
|
|
|
140 |
connect(calibrationWorker, SIGNAL(newFrameResult(int,int,cv::Mat)), this, SLOT(onCalibrationFrameResult(int,int,cv::Mat)));
|
|
|
141 |
connect(calibrationWorker, SIGNAL(done()), this, SLOT(onCalibrationDone()));
|
|
|
142 |
connect(calibrationWorker, SIGNAL(done()), calibrationWorkerThread, SLOT(quit()));
|
|
|
143 |
connect(calibrationWorker, SIGNAL(done()), calibrationWorker, SLOT(deleteLater()));
|
|
|
144 |
|
|
|
145 |
// Start capturing
|
|
|
146 |
QMetaObject::invokeMethod(calibrationWorker, "performCalibration", Q_ARG(std::vector<SMCalibrationSet>, calibrationData));
|
|
|
147 |
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
void SMScanner::onCalibrationSetProcessed(int idx){
|
|
|
151 |
|
|
|
152 |
ui->calibrationListWidget->setCurrentRow(idx);
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
void SMScanner::onCalibrationDone(){
|
|
|
156 |
|
|
|
157 |
std::cout << "Calibration done!" << std::endl;
|
|
|
158 |
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
void SMScanner::on_calibrationListWidget_itemSelectionChanged(){
|
|
|
162 |
|
|
|
163 |
// if selection is just cleared
|
|
|
164 |
if(ui->calibrationListWidget->selectedItems().empty())
|
|
|
165 |
return;
|
|
|
166 |
|
|
|
167 |
int currentRow = ui->calibrationListWidget->currentRow();
|
|
|
168 |
|
25 |
jakw |
169 |
calibrationReviewMode = true;
|
|
|
170 |
ui->singleCalibrationButton->setText("Live View");
|
|
|
171 |
ui->batchCalibrationButton->setText("Live View");
|
|
|
172 |
|
|
|
173 |
if(!calibrationData[currentRow].frame0Result.empty())
|
|
|
174 |
ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0Result);
|
|
|
175 |
else
|
|
|
176 |
ui->calibrationCamera0Widget->showImageCV(calibrationData[currentRow].frame0);
|
|
|
177 |
|
|
|
178 |
if(!calibrationData[currentRow].frame1Result.empty())
|
|
|
179 |
ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1Result);
|
|
|
180 |
else
|
|
|
181 |
ui->calibrationCamera1Widget->showImageCV(calibrationData[currentRow].frame1);
|
27 |
jakw |
182 |
|
|
|
183 |
// std::cout << "on_calibrationListWidget_itemSelectionChanged" << std::endl;
|
25 |
jakw |
184 |
}
|
|
|
185 |
|
27 |
jakw |
186 |
void SMScanner::onCalibrationFrameResult(int idx, int camID, cv::Mat result){
|
25 |
jakw |
187 |
|
27 |
jakw |
188 |
std::cout << "onCalibrationFrameResult " << idx << camID << std::endl;
|
|
|
189 |
|
|
|
190 |
if(camID == 0)
|
|
|
191 |
calibrationData[idx].frame0Result = result;
|
|
|
192 |
else if(camID == 1)
|
|
|
193 |
calibrationData[idx].frame1Result = result;
|
|
|
194 |
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
void SMScanner::on_singleCaptureButton_clicked(){
|
|
|
198 |
|
|
|
199 |
// If in review mode, go back to aquisition mode
|
|
|
200 |
if(captureReviewMode){
|
|
|
201 |
ui->captureTreeWidget->clearSelection();
|
|
|
202 |
ui->singleCaptureButton->setText("Single Aquisition");
|
|
|
203 |
ui->batchCaptureButton->setText("Batch Aquisition");
|
|
|
204 |
captureReviewMode = false;
|
|
|
205 |
return;
|
25 |
jakw |
206 |
}
|
|
|
207 |
|
27 |
jakw |
208 |
QMetaObject::invokeMethod(captureWorker, "acquireFrameSequence", Q_ARG(float, -1.0));
|
25 |
jakw |
209 |
|
|
|
210 |
}
|
26 |
jakw |
211 |
|
27 |
jakw |
212 |
|
|
|
213 |
void SMScanner::on_captureRotationDial_sliderReleased(){
|
|
|
214 |
|
|
|
215 |
float angle = ui->captureRotationDial->value();
|
|
|
216 |
std::cout << "Rotation stage target: " << angle << std::endl;
|
|
|
217 |
QMetaObject::invokeMethod(captureWorker, "rotateTo", Q_ARG(float, angle));
|
|
|
218 |
|
|
|
219 |
ui->calibrationRotationDial->setValue(ui->captureRotationDial->value());
|
26 |
jakw |
220 |
}
|
27 |
jakw |
221 |
|
|
|
222 |
void SMScanner::onReceiveFrameSequence(SMFrameSequence frameSequence){
|
|
|
223 |
|
|
|
224 |
captureData.push_back(frameSequence);
|
|
|
225 |
|
|
|
226 |
int idx = captureData.size()-1;
|
|
|
227 |
|
|
|
228 |
// Add identifier to list
|
|
|
229 |
QTreeWidgetItem* item = new QTreeWidgetItem(ui->captureTreeWidget);
|
|
|
230 |
item->setText(0, QString("Frame Sequence %1 -- %2 deg").arg(idx).arg(frameSequence.rotationAngle));
|
|
|
231 |
item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
|
|
|
232 |
item->setData(0, Qt::UserRole, QPoint(idx, -1));
|
|
|
233 |
item->setCheckState(0, Qt::Checked);
|
|
|
234 |
//ui->captureTreeWidget->addItem(item);
|
|
|
235 |
|
|
|
236 |
for(int i=0; i<frameSequence.frames0.size(); i++){
|
|
|
237 |
QTreeWidgetItem* subItem = new QTreeWidgetItem(item);
|
|
|
238 |
subItem->setText(0, QString("frames %1").arg(i));
|
|
|
239 |
subItem->setData(0, Qt::UserRole, QPoint(idx, i));
|
|
|
240 |
}
|
|
|
241 |
}
|
|
|
242 |
|
|
|
243 |
void SMScanner::on_captureTreeWidget_itemSelectionChanged(){
|
|
|
244 |
|
|
|
245 |
// if selection is just cleared
|
|
|
246 |
if(ui->captureTreeWidget->selectedItems().empty())
|
|
|
247 |
return;
|
|
|
248 |
|
|
|
249 |
QTreeWidgetItem *item = ui->captureTreeWidget->currentItem();
|
|
|
250 |
|
|
|
251 |
captureReviewMode = true;
|
|
|
252 |
|
|
|
253 |
ui->singleCaptureButton->setText("Live View");
|
|
|
254 |
ui->batchCaptureButton->setText("Live View");
|
|
|
255 |
|
|
|
256 |
QPoint idx = item->data(0, Qt::UserRole).toPoint();
|
|
|
257 |
|
|
|
258 |
if(idx.y() != -1){
|
|
|
259 |
ui->captureCamera0Widget->showImageCV(captureData[idx.x()].frames0[idx.y()]);
|
|
|
260 |
ui->captureCamera1Widget->showImageCV(captureData[idx.x()].frames1[idx.y()]);
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
// std::cout << "on_captureTreeWidget_itemSelectionChanged" << std::endl;
|
|
|
264 |
}
|