-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDCVCameraMainWindow.cpp
More file actions
526 lines (394 loc) · 15.9 KB
/
DCVCameraMainWindow.cpp
File metadata and controls
526 lines (394 loc) · 15.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*****************************************************************************
****************************** I N C L U D E *******************************
*****************************************************************************/
#include "DCVCameraMainWindow.h"
#include "ui_DCVCameraMainWindow.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QMenuBar>
#include "DQCVImageUtils.h"
/*****************************************************************************
*** class DCVCameraMainWindow
*****************************************************************************/
/*****************************************************************************
*
*** DCVCameraMainWindow::DCVCameraMainWindow
*
*****************************************************************************/
DCVCameraMainWindow::DCVCameraMainWindow(QWidget *pParent) :
Base(pParent),
m_eOrientation(Qt::Horizontal),
m_ImageSize(320, 240),
m_pInputImageWidget(nullptr),
m_pOutputImageWidget(nullptr),
m_bOutputImageWidget(true),
m_pMainGrid(nullptr),
m_pActionOpenImage(nullptr),
m_pActionSaveInputImage(nullptr),
m_pActionSaveInputImageAs(nullptr),
m_pActionExit(nullptr),
m_pActionClearROIs(nullptr),
m_pActionProcessImage(nullptr),
m_pFileMenu(nullptr),
m_pToolMenu(nullptr),
m_pViewMenu(nullptr),
m_pUI(new Ui::DCVCameraMainWindow)
{
m_pUI->setupUi(this);
m_strAppName = "DruaiCameraApp";
return;
} // end of DCVCameraMainWindow::DCVCameraMainWindow
/*****************************************************************************
*
*** DCVCameraMainWindow::~DCVCameraMainWindow
*
*****************************************************************************/
DCVCameraMainWindow::~DCVCameraMainWindow()
{
delete m_pUI;
m_pUI = nullptr;
return;
} // end of DCVCameraMainWindow::~DCVCameraMainWindow
/*****************************************************************************
*
*** DCVCameraMainWindow::GetMenuBar
*
****************************************************************************/
QMenuBar* DCVCameraMainWindow::GetMenuBar()
{
return (m_pUI->menuBar);
} // end of method DCVCameraMainWindow::GetMenuBar
/*****************************************************************************
*
*** DCVCameraMainWindow::Initialize
*
*****************************************************************************/
void DCVCameraMainWindow::Initialize(
Qt::Orientation eOrientation /* = Qt::Horizontal */)
{
Base::Initialize();
CreateFileActions();
m_pFileMenu = AddFileMenu();
AddFileToolBar();
m_eOrientation = eOrientation;
m_pCameraHandler = new DCVCameraHandler(this, m_ImageSize);
m_pCameraHandler->AddCameraMenu(m_pUI->menuBar);
m_pUI->mainToolBar->addSeparator();
m_pCameraHandler->AddCameraButtons(m_pUI->mainToolBar);
m_pToolMenu = AddToolsMenu();
AddToolsToolBar();
SetupCentralWidget();
ConnectToolActions();
ConnectImageWidgets();
connect(m_pCameraHandler, SIGNAL(imageCaptured(DCVImage&)),
this, SLOT(ImageCaptured(DCVImage&)));
connect(m_pCameraHandler, SIGNAL(CameraStarted()),
this, SLOT(CameraStarted()));
connect(m_pCameraHandler, SIGNAL(CameraStopped()),
this, SLOT(CameraStopped()));
// Don't start camera here, descendant classes my not have finished initializing.
// m_pCameraHandler->StartCamera();
return;
} // end of method DCVCameraMainWindow::Initialize
/*****************************************************************************
*
*** DCVCameraMainWindow::SetupCentralWidget
*
* The central widget lays out its children in a grid pattern. Most of the
* time it will contain either one or two DImageWidgets. In the two case, one
* will display the input image (camera or loaded) and the other will display
* the processed image.
*
*****************************************************************************/
void DCVCameraMainWindow::SetupCentralWidget()
{
Base::SetupCentralWidget();
m_pInputImageWidget = new DImageWidget();
m_pInputImageWidget->setFixedSize(m_ImageSize);
m_pMainGrid = new QGridLayout();
m_pMainGrid->addWidget(m_pInputImageWidget, 0, 0, Qt::AlignHCenter);
if (m_bOutputImageWidget)
{
m_pOutputImageWidget = new DImageWidget();
m_pOutputImageWidget->setFixedSize(m_ImageSize);
m_ImageWidgets.push_back(m_pInputImageWidget);
m_ImageWidgets.push_back(m_pOutputImageWidget);
if (m_eOrientation == Qt::Horizontal)
{
m_pMainGrid->addWidget(m_pOutputImageWidget, 0, 1, Qt::AlignHCenter);
} // end if
else if (m_eOrientation == Qt::Vertical)
{
m_pMainGrid->addWidget(m_pOutputImageWidget, 1, 0, Qt::AlignHCenter);
} // end else if
} // end if
m_pUI->centralWidget->setLayout(m_pMainGrid);
return;
} // end of method DCVCameraMainWindow::SetupCentralWidget
/*****************************************************************************
*
*** DCVCameraMainWindow::CameraStarted
*
****************************************************************************/
void DCVCameraMainWindow::CameraStarted()
{
UpdateFileActions();
m_pInputImageWidget->DisableRubberBand();
return;
} // end of method DCVCameraMainWindow::CameraStarted
/*****************************************************************************
*
*** DCVCameraMainWindow::CameraStopped
*
****************************************************************************/
void DCVCameraMainWindow::CameraStopped()
{
UpdateFileActions();
m_pInputImageWidget->EnableRubberBand();
return;
} // end of method DCVCameraMainWindow::CameraStopped
/*****************************************************************************
*
*** DCVCameraMainWindow::CreateFileActions
*
****************************************************************************/
void DCVCameraMainWindow::CreateFileActions()
{
m_pActionOpenImage = new QAction(QIcon(":/images/fileopen.png"),
tr("Open Image"), this);
m_pActionOpenImage->setShortcut(tr("Ctrl+O"));
m_pActionOpenImage->setStatusTip(tr("Open an Image File"));
connect(m_pActionOpenImage, SIGNAL(triggered()), this, SLOT(OpenImage()));
m_pActionSaveInputImage = new QAction(QIcon(":/images/Save2.png"),
tr("Save Input Image"), this);
m_pActionSaveInputImage->setShortcut(tr("Ctrl+S"));
m_pActionSaveInputImage->setStatusTip(tr("Save the Input Image"));
connect(m_pActionSaveInputImage, SIGNAL(triggered()), this, SLOT(SaveInputImage()));
m_pActionSaveInputImageAs = new QAction(QIcon(":/images/Save_As.png"),
tr("Save Input Image As..."), this);
m_pActionSaveInputImageAs->setStatusTip(
tr("Save the Input Image with a different filename"));
connect(m_pActionSaveInputImageAs, SIGNAL(triggered()), this, SLOT(SaveInputImageAs()));
m_pActionExit = new QAction(QIcon(":/images/EXIT2.png"),
tr("Exit"), this);
m_pActionExit->setShortcut(tr("Ctrl+Q"));
m_pActionExit->setStatusTip(tr("Exit the Application"));
connect(m_pActionExit, SIGNAL(triggered()), this, SLOT(close()));
return;
} // end of method DCVCameraMainWindow::CreateFileActions
/*****************************************************************************
*
*** DCVCameraMainWindow::AddFileMenu
*
****************************************************************************/
QMenu* DCVCameraMainWindow::AddFileMenu()
{
QMenu* pFileMenu = m_pUI->menuBar->addMenu(tr("&File"));
pFileMenu->addAction(m_pActionOpenImage);
pFileMenu->addAction(m_pActionSaveInputImage);
pFileMenu->addAction(m_pActionSaveInputImageAs);
pFileMenu->addSeparator();
pFileMenu->addAction(m_pActionExit);
return (pFileMenu);
} // end of method DCVCameraMainWindow::AddFileMenu
/*****************************************************************************
*
*** DCVCameraMainWindow::AddToolsMenu
*
*****************************************************************************/
QMenu* DCVCameraMainWindow::AddToolsMenu()
{
QMenu* pToolsMenu = m_pUI->menuBar->addMenu(tr("&Tools"));
m_pActionClearROIs = new QAction(tr("Clear ROIs"), this);
m_pActionClearROIs->setStatusTip(tr("Clear Image Regions of Interest"));
pToolsMenu->addAction(m_pActionClearROIs);
m_pActionProcessImage = new QAction(QIcon(":/images/Redo.png"), tr("Process Imgage"), this);
m_pActionProcessImage->setStatusTip(
tr("Manually Trigger Processing the Current Image"));
pToolsMenu->addAction(m_pActionProcessImage);
return (pToolsMenu);
} // end of method DCVCameraMainWindow::AddToolsMenu
/*****************************************************************************
*
*** DCVCameraMainWindow::AddViewMenu
*
*****************************************************************************/
QMenu* DCVCameraMainWindow::AddViewMenu()
{
QMenu* pViewMenu = m_pUI->menuBar->addMenu(tr("&View"));
return (pViewMenu);
} // end of method DCVCameraMainWindow::AddViewMenu
/*****************************************************************************
*
*** DCVCameraMainWindow::AddFileToolBar
*
****************************************************************************/
QToolBar* DCVCameraMainWindow::AddFileToolBar()
{
QToolBar* pFileToolBar = m_pUI->mainToolBar;
pFileToolBar->addAction(m_pActionOpenImage);
pFileToolBar->addAction(m_pActionSaveInputImage);
pFileToolBar->addAction(m_pActionSaveInputImageAs);
pFileToolBar->addSeparator();
pFileToolBar->addAction(m_pActionExit);
return (pFileToolBar);
} // end of method DCVCameraMainWindow::AddFileToolBar
/******************************************************************************
*
*** DCVCameraMainWindow::AddToolsToolBar
*
******************************************************************************/
QToolBar* DCVCameraMainWindow::AddToolsToolBar()
{
QToolBar* pToolsToolBar = m_pUI->mainToolBar;
pToolsToolBar->addSeparator();
pToolsToolBar->addAction(m_pActionProcessImage);
return (pToolsToolBar);
} // end of method DCVCameraMainWindow::AddToolsToolBar
/*****************************************************************************
*
*** DCVCameraMainWindow::UpdateFileActions
*
****************************************************************************/
void DCVCameraMainWindow::UpdateFileActions()
{
bool bEnable = !m_pCameraHandler->IsRunning();
m_pActionOpenImage->setEnabled(bEnable);
m_pActionSaveInputImage->setEnabled(bEnable);
m_pActionSaveInputImageAs->setEnabled(bEnable);
m_pActionProcessImage->setEnabled(bEnable);
return;
} // end of method DCVCameraMainWindow::UpdateFileActions
/*****************************************************************************
*
*** DCVCameraMainWindow::ConnectToolActions
*
* Connect the Tool Menu Actions to their functions.
*
*****************************************************************************/
void DCVCameraMainWindow::ConnectToolActions()
{
// Connect the Clear ROI menu action to each of the image widgets
foreach(DImageWidget * pIW, m_ImageWidgets)
{
connect(m_pActionClearROIs, SIGNAL(triggered()), pIW, SLOT(ClearROI()));
} // end foreach
connect(m_pActionProcessImage, SIGNAL(triggered()), SLOT(ReprocessImage()));
return;
} // end of method DCVCameraMainWindow::ConnectToolActions
/*****************************************************************************
*
*** DCVCameraMainWindow::ConnectImageWidgets
*
* Connect the image widgets so when the ROI of one changes, they all change
* their ROI to match.
*
*****************************************************************************/
void DCVCameraMainWindow::ConnectImageWidgets()
{
foreach (DImageWidget* pSignaler, m_ImageWidgets)
{
foreach(DImageWidget* pSlot, m_ImageWidgets)
{
if (pSignaler != pSlot)
{
connect(pSignaler, SIGNAL(ROIChanged(const QRect&)), pSlot, SLOT(SetROI(const QRect&)));
} // end if
} // end foreach
} // end foreach
return;
} // end of method DCVCameraMainWindow::ConnectImageWidgets
/*****************************************************************************
*
*** DCVCameraMainWindow::ImageCaptured
*
*****************************************************************************/
void DCVCameraMainWindow::ImageCaptured(DCVImage &Image)
{
m_CapturedImage = Image;
ProcessImage(m_CapturedImage);
return;
} // end of method DCVCameraMainWindow::ImageCaptured
/******************************************************************************
*
*** DCVCameraMainWindow::ReprocessImage
*
******************************************************************************/
void DCVCameraMainWindow::ReprocessImage()
{
ProcessImage(m_CapturedImage);
return;
} // end of method DCVCameraMainWindow::ReprocessImage
/*****************************************************************************
*
*** DCVCameraMainWindow::ProcessImage
*
*****************************************************************************/
void DCVCameraMainWindow::ProcessImage(DCVImage& Image)
{
QImage Display = cvMatToQImage(Image);
m_pInputImageWidget->SetImage(Display);
m_pOutputImageWidget->SetImage(Display);
return;
} // end of method DCVCameraMainWindow::ProcessImage
/*****************************************************************************
*
*** DCVCameraMainWindow::OpenImage
*
****************************************************************************/
void DCVCameraMainWindow::OpenImage(
int nFlags /* = DCVImage::eReadUnchanged */)
{
QString strFileName = QFileDialog::getOpenFileName(this,
tr("Open Image"), QDir::currentPath(),
tr("Image files (*.jpg *.png *.bmp);;All files (*.*)"));
if (!strFileName.isEmpty())
{
if (m_CapturedImage.ReadImage(strFileName.toStdString(), nFlags))
{
m_strInputImage = strFileName;
ProcessImage(m_CapturedImage);
} // end if
else
{
QString strMsg(tr("Failed to load image "));
strMsg += strFileName;
QMessageBox::warning(this, tr("Load Image"), strMsg);
} // end else
} // end if
return;
} // end of method DCVCameraMainWindow::OpenImage
/*****************************************************************************
*
*** DCVCameraMainWindow::SaveInputImage
*
****************************************************************************/
void DCVCameraMainWindow::SaveInputImage()
{
if (!m_strInputImage.isEmpty())
{
m_CapturedImage.WriteImage(m_strInputImage.toStdString());
} // end if
else
{
SaveInputImageAs();
} // end else
return;
} // end of method DCVCameraMainWindow::SaveInputImage
/*****************************************************************************
*
*** DCVCameraMainWindow::SaveInputImageAs
*
****************************************************************************/
void DCVCameraMainWindow::SaveInputImageAs()
{
QString strFileName = QFileDialog::getSaveFileName(this,
tr("Save Input Image As"), QDir::currentPath(),
tr("Image files (*.jpg *.png *.bmp)"));
if (!strFileName.isEmpty())
{
m_strInputImage = strFileName;
SaveInputImage();
} // end if
return;
} // end of method DCVCameraMainWindow::SaveInputImageAs