This repository was archived by the owner on Apr 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
473 lines (383 loc) · 17.7 KB
/
Copy pathmainwindow.cpp
File metadata and controls
473 lines (383 loc) · 17.7 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "mainwindow.h"
#include <QDebug>
#define STANDARD_RES "320x240"
#define X_TRIG_PERCENTAGE 15
#define Y_TRIG_PERCENTAGE 10
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setGeometry(200, 200, 780, 500); //x 1500
surface = new VideoSurface();
connect(surface, SIGNAL(frameAvailable(QImage)), this, SLOT(convertFrame(QImage)));
camera = new QCamera;
camera->setCaptureMode(QCamera::CaptureVideo);
camera->setViewfinder(surface);
frameInfoLabel = new QLabel(this);
frameInfoLabel->setStyleSheet("QLabel { color: red; } ");
frameInfoLabel->setGeometry(140, 11, 300, 12);
resLabel = new QLabel(this);
resLabel->setGeometry(10, 5, 100, 12);
resLabel->setText("Camera resolution");
cmbResolution = new QComboBox(this);
cmbResolution->setGeometry(10, 20, 100, 20);
connect(cmbResolution, SIGNAL(currentTextChanged(QString)), this, SLOT(updateRes()));
brightnessLabel = new QLabel(this);
brightnessLabel->setGeometry(10, 50, 100, 12);
brightnessLabel->setText("Brightness separator");
sldBrightness = new QSlider(Qt::Horizontal, this);
sldBrightness->setGeometry(10, 65, 100, 20);
sldBrightness->setMaximum(255);
sldBrightness->setMinimum(0);
sldBrightness->setValue(50);
sldBrightness->setToolTip(QString::number(sldBrightness->value()));
ckbB910Fix = new QCheckBox(this);
ckbB910Fix->move(10, 90);
ckbB910Fix->setText("B910 Fix");
btnStartCamera = new QPushButton(this);
btnStartCamera->setGeometry(10, 130, 100, 30);
btnStartCamera->setText("Start Camera");
connect(btnStartCamera, SIGNAL(clicked()), this, SLOT(startCamera()));
btnStopCamera = new QPushButton(this);
btnStopCamera->setGeometry(10, 130, 100, 30);
btnStopCamera->setText("Stop Camera");
btnStopCamera->hide();
connect(btnStopCamera, SIGNAL(clicked()), camera, SLOT(stop()));
connect(btnStopCamera, SIGNAL(clicked()), frameInfoLabel, SLOT(hide()));
connect(btnStopCamera, SIGNAL(clicked()), btnStartCamera, SLOT(show()));
connect(btnStopCamera, SIGNAL(clicked()), btnStopCamera, SLOT(hide()));
connect(btnStartCamera, SIGNAL(clicked()), btnStopCamera, SLOT(show())); //switch buttons since they cant do anything at the same time
connect(btnStartCamera, SIGNAL(clicked()), btnStartCamera, SLOT(hide()));
worker = new Worker;
workerThread = new QThread;
worker->moveToThread(workerThread);
connect(workerThread, SIGNAL(finished()), worker, SLOT(deleteLater()), Qt::QueuedConnection);
connect(this, SIGNAL(startThread(QImage,int,bool)), worker, SLOT(convertFrame(QImage,int,bool)), Qt::QueuedConnection);
connect(worker, SIGNAL(convertFinished(QPixmap,QPixmap,int,int,int,int)), this, SLOT(updateUI(QPixmap,QPixmap,int,int,int,int)), Qt::QueuedConnection);
connect(this, SIGNAL(resChanged(double,double)), worker, SLOT(resChanged(double,double)));
connect(worker, SIGNAL(fieldLocked(int,int,int,int,int,int,int,int)), this, SLOT(fieldLocked(int,int,int,int,int,int,int,int)));
connect(worker, SIGNAL(fieldUnLocked()), this, SLOT(fieldUnLocked()));
workerThread->start();
btnLockField = new QPushButton(this);
btnLockField->setGeometry(10, 165, 100, 30);
btnLockField->setText("Un-/Lock Field");
connect(btnLockField, SIGNAL(clicked()), worker, SLOT(switchFieldLock()));
btnRestartCalib = new QPushButton(this);
btnRestartCalib->setGeometry(10, 200, 100, 30);
btnRestartCalib->setText("Restart Calibration");
connect(btnRestartCalib, SIGNAL(clicked()), this, SLOT(restartCalib()));
connect(btnRestartCalib, SIGNAL(clicked()), btnStartCamera, SLOT(show()));
connect(btnRestartCalib, SIGNAL(clicked()), btnStopCamera, SLOT(hide()));
btnTestLaunchL = new QPushButton(this);
btnTestLaunchL->setGeometry(10, 460, 48, 30);
btnTestLaunchL->setText("Test L");
connect(btnTestLaunchL, SIGNAL(clicked()), this, SLOT(launchApplicationL()));
btnTestLaunchR = new QPushButton(this);
btnTestLaunchR->setGeometry(62, 460, 48, 30);
btnTestLaunchR->setText("Test R");
connect(btnTestLaunchR, SIGNAL(clicked()), this, SLOT(launchApplicationR()));
camera->load();
for (int i = 0; i < camera->supportedViewfinderResolutions().size(); i++)
{
QString resolutionW = QString::number(camera->supportedViewfinderResolutions()[i].width());
QString resolutionH = QString::number(camera->supportedViewfinderResolutions()[i].height());
cmbResolution->addItem(resolutionW + "x" + resolutionH, camera->supportedViewfinderResolutions()[i]);
if (resolutionW + "x" + resolutionH == STANDARD_RES)
cmbResolution->setCurrentIndex(i);
}
camera->unload();
checkCameras();
Calibration* calib = new Calibration();
calib->show();
QEventLoop evLoop;
connect(calib, SIGNAL(formClosing()), &evLoop, SLOT(quit()));
evLoop.exec();
}
void MainWindow::checkCameras()
{
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
if (cameras.isEmpty())
qDebug() << "No cameras available!";
else
{
qDebug() << "Available cameras:";
foreach (const QCameraInfo &cameraInfo, cameras)
{
qDebug() << cameraInfo.description() << " ID:" << cameraInfo.deviceName();
if ( cameraInfo.description() == "Logitech B910 HD Webcam")
ckbB910Fix->setChecked(true);
}
}
}
void MainWindow::startCamera()
{
updateRes();
camera->start();
qDebug() << "Camera started with resolution:" << viewfinderSettings.resolution();
frameInfoLabel->show();
convertFinished = true;
}
void MainWindow::updateRes()
{
int oldX = viewfinderSettings.resolution().width(); //needed to recalculate position of button field when locked and resolution changes
int oldY = viewfinderSettings.resolution().height();
int newX = cmbResolution->currentData().toSize().width();
int newY = cmbResolution->currentData().toSize().height();
double resRatioX = (double)newX / (double)oldX;
double resRatioY = (double)newY / (double)oldY;
emit resChanged(resRatioX, resRatioY);
viewfinderSettings.setResolution(cmbResolution->currentData().toSize());
camera->setViewfinderSettings(viewfinderSettings);
qDebug() << "New resolution:" << viewfinderSettings.resolution() << " Resolution ratio to old resolution:" << resRatioX << "X" << resRatioY << "Y";
}
void MainWindow::convertFrame(QImage frame)
{
if (convertFinished) //handle this in main thread so no delay builds up
{
convertFinished = false;
timer.restart();
frame = frame.mirrored(false, true);
emit startThread(frame, sldBrightness->value(), ckbB910Fix->isChecked());
}
}
void MainWindow::updateUI(QPixmap frame, QPixmap overlay, int xAvgR_t, int yAvgR_t, int xAvgL_t, int yAvgL_t)
{
cFrame = frame; //copy pixmaps from worker to temporary global pixmaps
cOverlay = overlay;
repaint(); //use qpainter instead of qlabels for images since its faster and keeps ui more responsive
_ms = timer.elapsed();
fps3 = fps2;
fps2 = fps1;
fps1 = 1 / (double)_ms * 1000;
double fps = (fps1 + fps2 + fps3) / 3;
if (!pressedR && isFieldLocked && (((xAvgR != 0) && ((xAvgR_t - xAvgR >= (widthR / 100.0 * X_TRIG_PERCENTAGE)) || (xAvgR_t - xAvgR <= (widthR / 100.0 * -X_TRIG_PERCENTAGE)))) ||
((yAvgR != 0) && ((yAvgR_t - yAvgR >= (heightR / 100.0 * Y_TRIG_PERCENTAGE)) || (yAvgR_t - yAvgR <= (heightR / 100.0 * -Y_TRIG_PERCENTAGE))))))
{
qDebug() << "RIGHT average coordinates differed more than " << widthR / 100.0 * X_TRIG_PERCENTAGE << "X and" << heightR / 100.0 * Y_TRIG_PERCENTAGE << "Y at " << xAvgR_t << "X" << yAvgR_t << "Y";
qDebug() << "Launch triggered!";
launchApplicationR(); //launch application when average values differ more than the given percentage
pressedR = true;
}
else if ((xAvgR_t >= xAvgR - 2 && xAvgR_t <= xAvgR + 2) && (yAvgR_t >= yAvgR - 2 && yAvgR_t <= yAvgR + 2)) //reset when average coordinates go back to initial averages +/-1
pressedR = false;
if (!pressedL && isFieldLocked && (((xAvgL != 0) && ((xAvgL_t - xAvgL >= (widthL / 100.0 * X_TRIG_PERCENTAGE)) || (xAvgL_t - xAvgL <= (widthL / 100.0 * -X_TRIG_PERCENTAGE)))) ||
((yAvgL != 0) && ((yAvgL_t - yAvgL >= (heightL / 100.0 * Y_TRIG_PERCENTAGE)) || (yAvgL_t - yAvgL <= (heightL / 100.0 * -Y_TRIG_PERCENTAGE))))))
{
qDebug() << "LEFT average coordinates differed more than " << widthL / 100.0 * X_TRIG_PERCENTAGE << "X and" << heightL / 100.0 * Y_TRIG_PERCENTAGE << "Y at " << xAvgL_t << "X" << yAvgL_t << "Y";
qDebug() << "Launch triggered!";
launchApplicationL();
pressedL = true;
}
else if ((xAvgL_t >= xAvgL - 1 && xAvgL_t <= xAvgL + 1) && (yAvgL_t >= yAvgL - 1 && yAvgL_t <= yAvgL + 1))
pressedL = false;
frameInfoLabel->setText("Frametime: " + QString::number(_ms) + "ms" + " FPS: " + QString::number((int)fps));
convertFinished = true;
}
void MainWindow::restartCalib()
{
camera->stop();
frameInfoLabel->hide();
Calibration* calib = new Calibration();
calib->show();
QEventLoop evLoop;
connect(calib, SIGNAL(formClosing()), &evLoop, SLOT(quit()));
evLoop.exec();
}
void MainWindow::launchApplicationL()
{
}
void MainWindow::launchApplicationR()
{
//QProcess::startDetached("mspaint");
}
void MainWindow::fieldLocked(int xAvgR_t, int yAvgR_t, int xAvgL_t, int yAvgL_t, int widthR_t, int heightR_t, int widthL_t, int heightL_t)
{
xAvgR = xAvgR_t; //assign final average values to average variables
yAvgR = yAvgR_t;
xAvgL = xAvgL_t;
yAvgL = yAvgL_t;
widthR = widthR_t;
heightR = heightR_t;
widthL = widthL_t;
heightL = heightL_t;
isFieldLocked = true;
qDebug() << "Fields locked with size" << widthR << "x" << heightR << "R and " << widthL << "x" << heightL << "L";
}
void MainWindow::fieldUnLocked()
{
isFieldLocked = false;
pressedL = false;
pressedR = false;
}
void MainWindow::paintEvent(QPaintEvent *event)
{
p.begin(this);
p.drawPixmap(130, 10, 640, 480, cFrame, 0, 0, cFrame.width(), cFrame.height());
p.drawPixmap(130, 10, 640, 480, cOverlay, 0, 0, cOverlay.width(), cOverlay.height());
if (pressedL)
p.setBrush(Qt::green);
else
p.setBrush(Qt::gray);
p.drawRect(10, 240, 100, 100);
if (pressedR)
p.setBrush(Qt::yellow);
else
p.setBrush(Qt::gray);
p.drawRect(10, 345, 100, 100);
p.drawText(QRect(10, 240, 100, 100), Qt::AlignCenter, "LEFT Button");
p.drawText(QRect(10, 345, 100, 100), Qt::AlignCenter, "RIGHT Button");
p.end();
}
void MainWindow::closeEvent(QCloseEvent *event)
{
workerThread->exit(); //end worker thread so no error message occurs
}
// ====================================
// == Old Code ==
// ====================================
//bwImageLabel->setPixmap(frame);
//overlayLabel->setPixmap(overlay);
//averageLabel->setPixmap(averageImage);
//averageLabelOverlay->setPixmap(overlay);
/* for (int y = 0; y < grayscale.height(); y++)
{
for (int x = 0; x < grayscale.width(); x++)
{
if (grayscale.pixelColor(x, y).red() >= separator)
grayscale.setPixelColor(x, y, Qt::white);
else if (grayscale.pixelColor(x, y).red() < separator)
grayscale.setPixelColor(x, y, Qt::black);
}
}*/
/*QPixmap MainWindow::captureGrayscale(int separator)
{
timer.restart();
QImage grayscale = QPixmap(this->grab(QRect(130, 10, 640, 480))).toImage();
for (int y = 0; y < grayscale.height(); y++)
{
QRgb* row = (QRgb*)grayscale.scanLine(y);
//QRgb* rowU = (QRgb*)grayscale.scanLine(y-1); implement later
//QRgb* rowD = (QRgb*)grayscale.scanLine(y+1);
for (int x = 0; x < grayscale.width(); x++)
{
if ((qGray(row[x]) > separator && qGray(row[x-1]) > separator) || (qGray(row[x]) > separator && qGray(row[x+1]) > separator)// && qGray(rowU[x]) > separator && qGray(rowD[x]) > separator)
row[x] = qRgb(255, 255, 255);
else if ((x == 0) && (qGray(row[x]) > separator && qGray(row[x+1]) > separator))
row[x] = qRgb(255, 255, 255);
else if ((x == grayscale.width() - 1) && (qGray(row[x]) > separator && qGray(row[x-1]) > separator))
row[x] = qRgb(255, 255, 255);
else
row[x] = qRgb(0, 0, 0);
}
}
return(QPixmap::fromImage(grayscale));
}*/
/* for (int y = 0; y < frame.height(); y++)
{
QRgb* row = (QRgb*)frame.scanLine(y);
if (y > 0)
QRgb* rowU = (QRgb*)frame.scanLine(y-1);
if (y < (frame.height() - 1))
QRgb* rowD = (QRgb*)frame.scanLine(y+1);
for (int x = 0; x < frame.width(); x++)
{
if (qGray(row[x]) > separator && qGray(row[x+1]) > separator && qGray(rowD[x]) > separator) //B BWW W
row[x] = qRgb(255, 255, 255);
else if (qGray(rowU[x]) > separator && qGray(row[x]) > separator && qGray(row[x+1]) > separator) //W BWW B
row[x] = qRgb(255, 255, 255);
else if (qGray(rowU[x]) > separator && qGray(row[x-1]) > separator && qGray(row[x]) > separator) //W WWB B
row[x] = qRgb(255, 255, 255);
else if (qGray(row[x-1]) > separator && qGray(row[x]) > separator && qGray(rowD[x]) > separator) //B WWB W
row[x] = qRgb(255, 255, 255);
else if (qGray(rowU[x]) > separator && qGray(row[x]) > separator && qGray(row[x+1]) > separator && qGray(rowD[x]) > separator) //W BWW W
row[x] = qRgb(255, 255, 255);
else if (qGray(rowU[x]) > separator && qGray(row[x-1]) > separator && qGray(row[x]) > separator && qGray(row[x+1]) > separator) //W WWW B
row[x] = qRgb(255, 255, 255);
else if (qGray(rowU[x]) > separator && qGray(row[x-1]) > separator && qGray(row[x]) > separator && qGray(rowD[x]) > separator) //W WWB W
row[x] = qRgb(255, 255, 255);
else if (qGray(row[x-1]) > separator && qGray(row[x]) > separator && qGray(row[x+1]) > separator && qGray(rowD[x]) > separator) //B WWW W
row[x] = qRgb(255, 255, 255);
else
row[x] = qRgb(0, 0, 0);
}
}*/
/*void MainWindow::captureTmr(QImage bwImage)
{
QPixmap overlay = QPixmap::fromImage(bwImage);
p->begin(&overlay);
p->setPen(Qt::red);
for (int y = 0; y < bwImage.height(); y++)
{
QRgb* row = (QRgb*)bwImage.scanLine(y);
for (int x = 0; x < bwImage.width(); x++)
{
if (((x != 0 || x != bwImage.width()) && (y != 0 || y != bwImage.height())) &&
((qGray(row[x]) == 255 && qGray(row[x-1]) == 255 && qGray(row[x+1]) == 0) ||
(qGray(row[x]) == 255 && qGray(row[x-1]) == 0 && qGray(row[x+1]) == 255)))
p->drawPoint(x, y);
}
}
p->end();
overlayLabel->setPixmap(overlay);
_ms = timer.elapsed();
fps3 = fps2;
fps2 = fps1;
fps1 = 1 / (double)_ms * 1000;
double fps = (fps1 + fps2 + fps3) / 3;
qDebug() << "Frametime:" << _ms << "ms" << "FPS:" << fps;
frameInfoLabel->setText("Frametime: " + QString::number(_ms) + "ms" + " FPS: " + QString::number((int)fps));
convertFinished = true;
//grayscaleLabel->setPixmap(QPixmap::fromImage(bwImage));
}*/
/*void MainWindow::convertFrame(QImage frame)
{
if (convertFinished)
{
convertFinished = false;
timer.restart();
int separator = sldBrightness->value();
frame = frame.mirrored(false, true);
for (int y = 0; y < frame.height(); y++)
{
QRgb* row = (QRgb*)frame.scanLine(y);
if (y > 0)
QRgb* rowU = (QRgb*)grayscale.scanLine(y-1);
if (y < (frame.height() - 1))
QRgb* rowD = (QRgb*)grayscale.scanLine(y+1);
for (int x = 0; x < frame.width(); x++)
{
if ((qGray(row[x]) > separator && qGray(row[x-1]) > separator) ||(qGray(row[x]) > separator && qGray(row[x+1]) > separator))
row[x] = qRgb(255, 255, 255);
else if (x == 0 && qGray(row[x]) > separator && qGray(row[x+1]) > separator)
row[x] = qRgb(255, 255, 255);
else if (x == frame.width() && qGray(row[x]) > separator && qGray(row[x-1]) > separator)
row[x] = qRgb(255, 255, 255);
else
row[x] = qRgb(0, 0, 0);
}
}
QPixmap overlay = QPixmap::fromImage(frame);
p->begin(&overlay);
p->setPen(Qt::red);
for (int y = 0; y < frame.height(); y++)
{
QRgb* row = (QRgb*)frame.scanLine(y);
for (int x = 0; x < frame.width(); x++)
{
if (((x != 0 && x != frame.width()) && (y != 0 && y != frame.height())) &&
((qGray(row[x]) == 255 && qGray(row[x-1]) == 255 && qGray(row[x+1]) == 0) ||
(qGray(row[x]) == 255 && qGray(row[x-1]) == 0 && qGray(row[x+1]) == 255)))
p->drawPoint(x, y);
}
}
p->end();
overlayLabel->setPixmap(overlay);
_ms = timer.elapsed();
fps3 = fps2;
fps2 = fps1;
fps1 = 1 / (double)_ms * 1000;
double fps = (fps1 + fps2 + fps3) / 3;
//qDebug() << "Frametime:" << _ms << "ms" << "FPS:" << fps;
frameInfoLabel->setText("Frametime: " + QString::number(_ms) + "ms" + " FPS: " + QString::number((int)fps));
convertFinished = true;
}
}*/