-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKinectV2Recorder.cpp
More file actions
1622 lines (1396 loc) · 52 KB
/
KinectV2Recorder.cpp
File metadata and controls
1622 lines (1396 loc) · 52 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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// KinectV2Recorder.cpp
//
// Author: Po-Chen Wu (pcwu0329@gmail.com)
//
// These codes are written mainly based on codes from Kinect for Windows SDK 2.0
// https://www.microsoft.com/en-us/download/details.aspx?id=44561
#include "stdafx.h"
#include <strsafe.h>
#include "resource.h"
#include "KinectV2Recorder.h"
#include <algorithm>
#include <vector>
#include <queue>
#ifdef USE_IPP
#include <ippi.h>
#endif
/// <summary>
/// Entry point for the application
/// </summary>
/// <param name="hInstance">handle to the application instance</param>
/// <param name="hPrevInstance">always 0</param>
/// <param name="lpCmdLine">command line arguments</param>
/// <param name="nCmdShow">whether to display minimized, maximized, or normally</param>
/// <returns>status</returns>
int APIENTRY wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nShowCmd
)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
CKinectV2Recorder application;
application.Run(hInstance, nShowCmd);
}
/// <summary>
/// Constructor
/// </summary>
CKinectV2Recorder::CKinectV2Recorder() :
m_hWnd(NULL),
m_nStartTime(0),
m_nInfraredLastCounter(0),
m_nDepthLastCounter(0),
m_nColorLastCounter(0),
m_nInfraredFramesSinceUpdate(0),
m_nDepthFramesSinceUpdate(0),
m_nColorFramesSinceUpdate(0),
m_fFreq(0),
m_nNextStatusTime(0LL),
m_bRecord(false),
m_bShot(false),
m_bShotReady(false),
m_bSelect2D(true),
m_pKinectSensor(NULL),
m_pInfraredFrameReader(NULL),
m_pDepthFrameReader(NULL),
m_pColorFrameReader(NULL),
m_pD2DFactory(NULL),
m_pDrawInfrared(NULL),
m_pDrawDepth(NULL),
m_pDrawColor(NULL),
m_pInfraredRGBX(NULL),
m_pDepthRGBX(NULL),
m_pColorRGBX(NULL),
m_nInfraredIndex(0),
m_nDepthIndex(0),
m_nColorIndex(0),
m_nModel2DIndex(0),
m_nModel3DIndex(0),
m_nTypeIndex(0),
m_nLevelIndex(0),
m_nSideIndex(0),
m_tSaveThread(),
m_bStopThread(false)
{
LARGE_INTEGER qpf = { 0 };
if (QueryPerformanceFrequency(&qpf))
{
m_fFreq = double(qpf.QuadPart);
}
// create heap storage for infrared pixel data in RGBX format
m_pInfraredRGBX = new RGBQUAD[cInfraredWidth * cInfraredHeight];
// create heap storage for depth pixel data in RGBX & UINT16 format
m_pDepthRGBX = new RGBQUAD[cDepthWidth * cDepthHeight];
// create heap storage for color pixel data in RGBX format
m_pColorRGBX = new RGBQUAD[cColorWidth * cColorHeight];
for (int i = 0; i < BufferSize; ++i)
{
// create heap storage for infrared pixel data in UINT16 format
m_pInfraredUINT16[i] = new UINT16[cInfraredWidth * cInfraredHeight];
// create heap storage for depth pixel data in UINT16 format
m_pDepthUINT16[i] = new UINT16[cDepthWidth * cDepthHeight];
// create heap storage for color pixel data in RGB format
m_pColorRGB[i] = new RGBTRIPLE[cColorWidth * cColorHeight];
}
// create heap storage for file lists
m_vInfraredList.reserve(1800);
m_vDepthList.reserve(1800);
m_vColorList.reserve(1800);
}
/// <summary>
/// Destructor
/// </summary>
CKinectV2Recorder::~CKinectV2Recorder()
{
// clean up Direct2D renderer
if (m_pDrawInfrared)
{
delete m_pDrawInfrared;
m_pDrawInfrared = NULL;
}
if (m_pDrawDepth)
{
delete m_pDrawDepth;
m_pDrawDepth = NULL;
}
if (m_pDrawColor)
{
delete m_pDrawColor;
m_pDrawColor = NULL;
}
if (m_pInfraredRGBX)
{
delete[] m_pInfraredRGBX;
m_pInfraredRGBX = NULL;
}
if (m_pDepthRGBX)
{
delete[] m_pDepthRGBX;
m_pDepthRGBX = NULL;
}
if (m_pColorRGBX)
{
delete[] m_pColorRGBX;
m_pColorRGBX = NULL;
}
for (int i = 0; i < BufferSize; ++i)
{
if (m_pInfraredUINT16[i])
{
delete[] m_pInfraredUINT16[i];
m_pInfraredUINT16[i] = NULL;
}
if (m_pDepthUINT16[i])
{
delete[] m_pDepthUINT16[i];
m_pDepthUINT16[i] = NULL;
}
if (m_pColorRGB[i])
{
delete[] m_pColorRGB[i];
m_pColorRGB[i] = NULL;
}
}
// clean up Direct2D
SafeRelease(m_pD2DFactory);
// done with infrared frame reader
SafeRelease(m_pInfraredFrameReader);
// done with depth frame reader
SafeRelease(m_pDepthFrameReader);
// done with color frame reader
SafeRelease(m_pColorFrameReader);
// close the Kinect Sensor
if (m_pKinectSensor)
{
m_pKinectSensor->Close();
}
SafeRelease(m_pKinectSensor);
m_bStopThread = true;
if (m_tSaveThread.joinable()) m_tSaveThread.join();
}
/// <summary>
/// Creates the main window and begins processing
/// </summary>
/// <param name="hInstance">handle to the application instance</param>
/// <param name="nCmdShow">whether to display minimized, maximized, or normally</param>
int CKinectV2Recorder::Run(HINSTANCE hInstance, int nCmdShow)
{
MSG msg = { 0 };
WNDCLASS wc;
// Dialog custom window class
ZeroMemory(&wc, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCE(IDI_APP));
wc.lpfnWndProc = DefDlgProcW;
wc.lpszClassName = L"KinectV2RecorderAppDlgWndClass";
if (!RegisterClassW(&wc))
{
return 0;
}
// Create main application window
HWND hWndApp = CreateDialogParamW(
NULL,
MAKEINTRESOURCE(IDD_APP),
NULL,
(DLGPROC)CKinectV2Recorder::MessageRouter,
reinterpret_cast<LPARAM>(this));
// Show window
ShowWindow(hWndApp, nCmdShow);
// Main message loop
while (WM_QUIT != msg.message)
{
Update();
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
{
// If a dialog message will be taken care of by the dialog proc
if (hWndApp && IsDialogMessageW(hWndApp, &msg))
{
continue;
}
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
return static_cast<int>(msg.wParam);
}
/// <summary>
/// Start multithreading
/// </summary>
void CKinectV2Recorder::StartMultithreading()
{
m_tSaveThread = std::thread(&CKinectV2Recorder::SaveRecordImages, this);
}
/// <summary>
/// Main processing function
/// </summary>
void CKinectV2Recorder::Update()
{
if (!m_pInfraredFrameReader | !m_pDepthFrameReader | !m_pColorFrameReader)
{
return;
}
INT64 currentInfraredFrameTime = 0;
INT64 currentDepthFrameTime = 0;
INT64 currentColorFrameTime = 0;
IInfraredFrame* pInfraredFrame = NULL;
IDepthFrame* pDepthFrame = NULL;
IColorFrame* pColorFrame = NULL;
// Get an infrared frame from Kinect
HRESULT hrInfrared = m_pInfraredFrameReader->AcquireLatestFrame(&pInfraredFrame);
// Get a depth frame from Kinect
HRESULT hrDepth = m_pDepthFrameReader->AcquireLatestFrame(&pDepthFrame);
// Get a color frame from Kinect
HRESULT hrColor = m_pColorFrameReader->AcquireLatestFrame(&pColorFrame);
if (SUCCEEDED(hrInfrared))
{
INT64 currentInfraredFrameTime = 0;
IFrameDescription* pFrameDescription = NULL;
int nWidth = 0;
int nHeight = 0;
UINT nBufferSize = 0;
UINT16 *pBuffer = NULL;
// Unit: 100 ns
HRESULT hr = pInfraredFrame->get_RelativeTime(¤tInfraredFrameTime);
if (SUCCEEDED(hr))
{
hr = pInfraredFrame->get_FrameDescription(&pFrameDescription);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Width(&nWidth);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Height(&nHeight);
}
if (SUCCEEDED(hr))
{
hr = pInfraredFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer);
}
if (SUCCEEDED(hr))
{
ProcessInfrared(currentInfraredFrameTime, pBuffer, nWidth, nHeight);
}
SafeRelease(pFrameDescription);
}
SafeRelease(pInfraredFrame);
if (SUCCEEDED(hrDepth))
{
INT64 currentDepthFrameTime = 0;
IFrameDescription* pFrameDescription = NULL;
int nWidth = 0;
int nHeight = 0;
USHORT nDepthMinReliableDistance = 0;
USHORT nDepthMaxDistance = 0;
UINT nBufferSize = 0;
UINT16 *pBuffer = NULL;
// Unit: 100 ns
HRESULT hr = pDepthFrame->get_RelativeTime(¤tDepthFrameTime);
if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_FrameDescription(&pFrameDescription);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Width(&nWidth);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Height(&nHeight);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_DepthMinReliableDistance(&nDepthMinReliableDistance);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrame->get_DepthMaxReliableDistance(&nDepthMaxDistance);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrame->AccessUnderlyingBuffer(&nBufferSize, &pBuffer);
}
if (SUCCEEDED(hr))
{
ProcessDepth(currentDepthFrameTime, pBuffer, nWidth, nHeight, nDepthMinReliableDistance, nDepthMaxDistance);
}
SafeRelease(pFrameDescription);
}
SafeRelease(pDepthFrame);
if (SUCCEEDED(hrColor))
{
INT64 currentColorFrameTime = 0;
IFrameDescription* pFrameDescription = NULL;
int nWidth = 0;
int nHeight = 0;
ColorImageFormat imageFormat = ColorImageFormat_None;
UINT nBufferSize = 0;
RGBQUAD *pBuffer = NULL;
// Unit: 100 ns
HRESULT hr = pColorFrame->get_RelativeTime(¤tColorFrameTime);
if (SUCCEEDED(hr))
{
hr = pColorFrame->get_FrameDescription(&pFrameDescription);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Width(&nWidth);
}
if (SUCCEEDED(hr))
{
hr = pFrameDescription->get_Height(&nHeight);
}
if (SUCCEEDED(hr))
{
hr = pColorFrame->get_RawColorImageFormat(&imageFormat);
}
if (SUCCEEDED(hr))
{
if (imageFormat == ColorImageFormat_Bgra)
{
hr = pColorFrame->AccessRawUnderlyingBuffer(&nBufferSize, reinterpret_cast<BYTE**>(&pBuffer));
}
else if (m_pColorRGBX)
{
pBuffer = m_pColorRGBX;
nBufferSize = cColorWidth * cColorHeight * sizeof(RGBQUAD);
hr = pColorFrame->CopyConvertedFrameDataToArray(nBufferSize, reinterpret_cast<BYTE*>(pBuffer), ColorImageFormat_Bgra);
}
else
{
hr = E_FAIL;
}
}
if (SUCCEEDED(hr))
{
ProcessColor(currentColorFrameTime, pBuffer, nWidth, nHeight);
}
SafeRelease(pFrameDescription);
}
SafeRelease(pColorFrame);
}
/// <summary>
/// Initialize the UI
/// </summary>
void CKinectV2Recorder::InitializeUIControls()
{
const wchar_t *Models[] = { L"Wing", L"Duck", L"City", L"Beach", L"Firework", L"Maple" };
const wchar_t *Types[] = { L"Translation", L"Zoom", L"In-plane Rotation", L"Out-of-plane Rotation",
L"Flashing Light", L"Moving Light", L"Free Movement" };
const wchar_t *Levels[] = { L"1", L"2", L"3", L"4", L"5" };
const wchar_t *Sides[] = { L"Front", L"Left", L"Back", L"Right" };
// Set the radio button for selection between 2D and 3D
if (m_bSelect2D)
{
CheckDlgButton(m_hWnd, IDC_2D, BST_CHECKED);
}
else
{
CheckDlgButton(m_hWnd, IDC_3D, BST_CHECKED);
}
for (int i = 0; i < 6; i++)
{
SendDlgItemMessage(m_hWnd, IDC_MODEL_CBO, CB_ADDSTRING, 0, (LPARAM)Models[i]);
}
for (int i = 0; i < 7; i++)
{
SendDlgItemMessage(m_hWnd, IDC_TYPE_CBO, CB_ADDSTRING, 0, (LPARAM)Types[i]);
}
for (int i = 0; i < 5; i++)
{
SendDlgItemMessage(m_hWnd, IDC_LEVEL_CBO, CB_ADDSTRING, 0, (LPARAM)Levels[i]);
}
for (int i = 0; i < 4; i++)
{
SendDlgItemMessage(m_hWnd, IDC_SIDE_CBO, CB_ADDSTRING, 0, (LPARAM)Sides[i]);
}
// Set combo box index
SendDlgItemMessage(m_hWnd, IDC_MODEL_CBO, CB_SETCURSEL, m_nModel2DIndex, 0);
SendDlgItemMessage(m_hWnd, IDC_TYPE_CBO, CB_SETCURSEL, m_nTypeIndex, 0);
SendDlgItemMessage(m_hWnd, IDC_LEVEL_CBO, CB_SETCURSEL, m_nLevelIndex, 0);
SendDlgItemMessage(m_hWnd, IDC_SIDE_CBO, CB_SETCURSEL, m_nSideIndex, 0);
// Disable the side text & combo box
EnableWindow(GetDlgItem(m_hWnd, IDC_SIDE_TEXT), false);
EnableWindow(GetDlgItem(m_hWnd, IDC_SIDE_CBO), false);
// Set button icons
m_hRecord = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_RECORD), IMAGE_ICON, 128, 128, LR_DEFAULTCOLOR);
m_hStop = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_STOP), IMAGE_ICON, 128, 128, LR_DEFAULTCOLOR);
m_hShot = LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_SHOT), IMAGE_ICON, 27, 18, LR_DEFAULTCOLOR);
SendDlgItemMessage(m_hWnd, IDC_BUTTON_RECORD, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)m_hRecord);
SendDlgItemMessage(m_hWnd, IDC_BUTTON_SHOT, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)m_hShot);
// Set sfolder
StringCchPrintf(m_cModelFolder, _countof(m_cModelFolder), L"2D");
StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"2D//wi_tr_1");
}
/// <summary>
/// Process the UI inputs
/// </summary>
/// <param name="wParam">message data</param>
/// <param name="lParam">additional message data</param>
void CKinectV2Recorder::ProcessUI(WPARAM wParam, LPARAM)
{
const wchar_t *Model2D[] = { L"Wing", L"Duck", L"City", L"Beach", L"Firework", L"Maple" };
const wchar_t *Model3D[] = { L"Soda", L"Chest", L"Ironman", L"House", L"Bike", L"Jet" };
// Select 2D Model
if (IDC_2D == LOWORD(wParam) && BN_CLICKED == HIWORD(wParam))
{
m_bSelect2D = true;
SendDlgItemMessage(m_hWnd, IDC_MODEL_CBO, CB_RESETCONTENT, 0, 0);
for (int i = 0; i < 6; i++)
{
SendDlgItemMessage(m_hWnd, IDC_MODEL_CBO, CB_ADDSTRING, 0, (LPARAM)Model2D[i]);
}
// Setup combo boxes
SendDlgItemMessage(m_hWnd, IDC_MODEL_CBO, CB_SETCURSEL, m_nModel2DIndex, 0);
// Disable the side text & combo box
EnableWindow(GetDlgItem(m_hWnd, IDC_SIDE_TEXT), false);
EnableWindow(GetDlgItem(m_hWnd, IDC_SIDE_CBO), false);
}
// Select 3D Model
if (IDC_3D == LOWORD(wParam) && BN_CLICKED == HIWORD(wParam))
{
m_bSelect2D = false;
SendDlgItemMessage(m_hWnd, IDC_MODEL_CBO, CB_RESETCONTENT, 0, 0);
for (int i = 0; i < 6; i++)
{
SendDlgItemMessage(m_hWnd, IDC_MODEL_CBO, CB_ADDSTRING, 0, (LPARAM)Model3D[i]);
}
// Setup combo boxes
SendDlgItemMessage(m_hWnd, IDC_MODEL_CBO, CB_SETCURSEL, m_nModel3DIndex, 0);
// Enable the side text & combo box
EnableWindow(GetDlgItem(m_hWnd, IDC_SIDE_TEXT), true);
EnableWindow(GetDlgItem(m_hWnd, IDC_SIDE_CBO), true);
}
// Model selection
if (IDC_MODEL_CBO == LOWORD(wParam))
{
UINT index = (UINT)SendDlgItemMessage(m_hWnd, IDC_MODEL_CBO, CB_GETCURSEL, 0, 0);
if (m_bSelect2D) m_nModel2DIndex = index;
else m_nModel3DIndex = index;
}
// Motion type selection
if (IDC_TYPE_CBO == LOWORD(wParam))
{
m_nTypeIndex = (UINT)SendDlgItemMessage(m_hWnd, IDC_TYPE_CBO, CB_GETCURSEL, 0, 0);
if (m_nTypeIndex > 3)
{
EnableWindow(GetDlgItem(m_hWnd, IDC_LEVEL_TEXT), false);
EnableWindow(GetDlgItem(m_hWnd, IDC_LEVEL_CBO), false);
}
else
{
EnableWindow(GetDlgItem(m_hWnd, IDC_LEVEL_TEXT), true);
EnableWindow(GetDlgItem(m_hWnd, IDC_LEVEL_CBO), true);
}
}
// Motion digradation level selection
if (IDC_LEVEL_CBO == LOWORD(wParam))
{
m_nLevelIndex = (UINT)SendDlgItemMessage(m_hWnd, IDC_LEVEL_CBO, CB_GETCURSEL, 0, 0);
}
// Model side selection
if (IDC_SIDE_CBO == LOWORD(wParam))
{
m_nSideIndex = (UINT)SendDlgItemMessage(m_hWnd, IDC_SIDE_CBO, CB_GETCURSEL, 0, 0);
}
// Set save folder
if (m_bSelect2D)
{
StringCchPrintf(m_cModelFolder, _countof(m_cModelFolder), L"2D");
switch (m_nModel2DIndex)
{
case 0: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"2D\\wi"); break;
case 1: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"2D\\du"); break;
case 2: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"2D\\ci"); break;
case 3: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"2D\\be"); break;
case 4: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"2D\\fi"); break;
case 5: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"2D\\ma"); break;
}
}
else{
StringCchPrintf(m_cModelFolder, _countof(m_cModelFolder), L"3D");
switch (m_nModel3DIndex)
{
case 0: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"3D\\so"); break;
case 1: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"3D\\ch"); break;
case 2: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"3D\\ir"); break;
case 3: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"3D\\ho"); break;
case 4: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"3D\\bi"); break;
case 5: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"3D\\je"); break;
}
}
switch (m_nTypeIndex)
{
case 0: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_tr", m_cSaveFolder); break;
case 1: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_zo", m_cSaveFolder); break;
case 2: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_ir", m_cSaveFolder); break;
case 3: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_or", m_cSaveFolder); break;
case 4: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_fl", m_cSaveFolder); break;
case 5: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_ml", m_cSaveFolder); break;
case 6: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_fm", m_cSaveFolder); break;
}
if (m_nTypeIndex < 4)
{
switch (m_nLevelIndex)
{
case 0: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_1", m_cSaveFolder); break;
case 1: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_2", m_cSaveFolder); break;
case 2: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_3", m_cSaveFolder); break;
case 3: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_4", m_cSaveFolder); break;
case 4: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_5", m_cSaveFolder); break;
}
}
if (!m_bSelect2D)
{
switch (m_nSideIndex)
{
case 0: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_f", m_cSaveFolder); break;
case 1: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_l", m_cSaveFolder); break;
case 2: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_b", m_cSaveFolder); break;
case 3: StringCchPrintf(m_cSaveFolder, _countof(m_cSaveFolder), L"%s_r", m_cSaveFolder); break;
}
}
WCHAR szStatusMessage[128];
StringCchPrintf(szStatusMessage, _countof(szStatusMessage), L" Save Folder: %s FPS(Infrared, Depth, Color) = (%0.2f, %0.2f, %0.2f)", m_cSaveFolder, m_fInfraredFPS, m_fDepthFPS, m_fColorFPS);
SetStatusMessage(szStatusMessage, 500, true);
// If it is a record control and a button clicked event, save the video sequences
if (IDC_BUTTON_RECORD == LOWORD(wParam) && BN_CLICKED == HIWORD(wParam))
{
if (m_bRecord)
{
#ifdef VERBOSE
CheckImages();
#endif
ResetRecordParameters();
}
else
{
m_bRecord = true;
SendDlgItemMessage(m_hWnd, IDC_BUTTON_RECORD, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)m_hStop);
}
}
// If it is a shot control and a button clicked event, save the camera images
if (IDC_BUTTON_SHOT == LOWORD(wParam) && BN_CLICKED == HIWORD(wParam))
{
m_bShot = true;
}
}
/// <summary>
/// Handles window messages, passes most to the class instance to handle
/// </summary>
/// <param name="hWnd">window message is for</param>
/// <param name="uMsg">message</param>
/// <param name="wParam">message data</param>
/// <param name="lParam">additional message data</param>
/// <returns>result of message processing</returns>
LRESULT CALLBACK CKinectV2Recorder::MessageRouter(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CKinectV2Recorder* pThis = NULL;
if (WM_INITDIALOG == uMsg)
{
pThis = reinterpret_cast<CKinectV2Recorder*>(lParam);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pThis));
}
else
{
pThis = reinterpret_cast<CKinectV2Recorder*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
}
if (pThis)
{
return pThis->DlgProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
/// <summary>
/// Handle windows messages for the class instance
/// </summary>
/// <param name="hWnd">window message is for</param>
/// <param name="uMsg">message</param>
/// <param name="wParam">message data</param>
/// <param name="lParam">additional message data</param>
/// <returns>result of message processing</returns>
LRESULT CALLBACK CKinectV2Recorder::DlgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
// Bind application window handle
m_hWnd = hWnd;
InitializeUIControls();
// Init Direct2D
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &m_pD2DFactory);
// Create and initialize a new Direct2D image renderer (take a look at ImageRenderer.h)
// We'll use this to draw the data we receive from the Kinect to the screen
m_pDrawInfrared = new ImageRenderer();
HRESULT hr = m_pDrawInfrared->Initialize(GetDlgItem(m_hWnd, IDC_INFRAREDVIEW), m_pD2DFactory, cInfraredWidth, cInfraredHeight, cInfraredWidth * sizeof(RGBQUAD));
if (FAILED(hr))
{
SetStatusMessage(L"Failed to initialize the Direct2D draw device.", 10000, true);
}
m_pDrawDepth = new ImageRenderer();
hr = m_pDrawDepth->Initialize(GetDlgItem(m_hWnd, IDC_DEPTHVIEW), m_pD2DFactory, cDepthWidth, cDepthHeight, cDepthWidth * sizeof(RGBQUAD));
if (FAILED(hr))
{
SetStatusMessage(L"Failed to initialize the Direct2D draw device.", 10000, true);
}
m_pDrawColor = new ImageRenderer();
hr = m_pDrawColor->Initialize(GetDlgItem(m_hWnd, IDC_COLORVIEW), m_pD2DFactory, cColorWidth, cColorHeight, cColorWidth * sizeof(RGBQUAD));
if (FAILED(hr))
{
SetStatusMessage(L"Failed to initialize the Direct2D draw device.", 10000, true);
}
// Get and initialize the default Kinect sensor
InitializeDefaultSensor();
StartMultithreading();
}
break;
// If the titlebar X is clicked, destroy app
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
// Quit the main message pump
PostQuitMessage(0);
break;
// Handle button press
case WM_COMMAND:
ProcessUI(wParam, lParam);
break;
}
return FALSE;
}
/// <summary>
/// Initializes the default Kinect sensor
/// </summary>
/// <returns>indicates success or failure</returns>
HRESULT CKinectV2Recorder::InitializeDefaultSensor()
{
HRESULT hr;
hr = GetDefaultKinectSensor(&m_pKinectSensor);
if (FAILED(hr))
{
return hr;
}
if (m_pKinectSensor)
{
// Initialize the Kinect and get the readers
IInfraredFrameSource* pInfraredFrameSource = NULL;
IDepthFrameSource* pDepthFrameSource = NULL;
IColorFrameSource* pColorFrameSource = NULL;
hr = m_pKinectSensor->Open();
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_InfraredFrameSource(&pInfraredFrameSource);
}
if (SUCCEEDED(hr))
{
hr = pInfraredFrameSource->OpenReader(&m_pInfraredFrameReader);
}
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_DepthFrameSource(&pDepthFrameSource);
}
if (SUCCEEDED(hr))
{
hr = pDepthFrameSource->OpenReader(&m_pDepthFrameReader);
}
if (SUCCEEDED(hr))
{
hr = m_pKinectSensor->get_ColorFrameSource(&pColorFrameSource);
}
if (SUCCEEDED(hr))
{
hr = pColorFrameSource->OpenReader(&m_pColorFrameReader);
}
SafeRelease(pInfraredFrameSource);
SafeRelease(pDepthFrameSource);
SafeRelease(pColorFrameSource);
}
if (!m_pKinectSensor || FAILED(hr))
{
SetStatusMessage(L"No ready Kinect found!", 10000, true);
return E_FAIL;
}
return hr;
}
/// <summary>
/// Handle new infrared data
/// <param name="nTime">timestamp of frame</param>
/// <param name="pBuffer">pointer to frame data</param>
/// <param name="nWidth">width (in pixels) of input image data</param>
/// <param name="nHeight">height (in pixels) of input image data</param>
/// </summary>
void CKinectV2Recorder::ProcessInfrared(INT64 nTime, const UINT16* pBuffer, int nWidth, int nHeight)
{
if (m_hWnd)
{
double fps = 0.0;
LARGE_INTEGER qpcNow = { 0 };
if (m_fFreq)
{
if (QueryPerformanceCounter(&qpcNow))
{
if (m_nInfraredLastCounter)
{
m_nInfraredFramesSinceUpdate++;
fps = m_fFreq * m_nInfraredFramesSinceUpdate / double(qpcNow.QuadPart - m_nInfraredLastCounter);
}
}
}
WCHAR szStatusMessage[128];
StringCchPrintf(szStatusMessage, _countof(szStatusMessage), L" Save Folder: %s FPS(Infrared, Depth, Color) = (%0.2f, %0.2f, %0.2f)", m_cSaveFolder, m_fInfraredFPS, m_fDepthFPS, m_fColorFPS);
if (SetStatusMessage(szStatusMessage, 1000, false))
{
m_nInfraredLastCounter = qpcNow.QuadPart;
m_nInfraredFramesSinceUpdate = 0;
m_fInfraredFPS = fps;
#ifdef VERBOSE
if (m_bRecord && m_fInfraredFPS < 29.5)
{
ResetRecordParameters();
MessageBox(NULL,
L"Infrared frame dropping occurred...\n",
L"No Good",
MB_OK | MB_ICONERROR
);
return;
}
#endif
}
}
if (m_pInfraredRGBX && pBuffer && (nWidth == cInfraredWidth) && (nHeight == cInfraredHeight))
{
INT64 index = m_nInfraredIndex % BufferSize;
RGBQUAD* pRGBX = m_pInfraredRGBX;
UINT16* pUINT16 = m_pInfraredUINT16[index];
pBuffer += cInfraredWidth - 1;
for (int i = 0; i < cInfraredHeight; ++i)
{
for (int j = 0; j < cInfraredWidth; ++j)
{
// normalize the incoming infrared data (ushort) to a float ranging from
// [InfraredOutputValueMinimum, InfraredOutputValueMaximum] by
// 1. dividing the incoming value by the source maximum value
float intensityRatio = static_cast<float>(*pBuffer) / InfraredSourceValueMaximum;
// 2. dividing by the (average scene value * standard deviations)
intensityRatio /= InfraredSceneValueAverage * InfraredSceneStandardDeviations;
// 3. limiting the value to InfraredOutputValueMaximum
intensityRatio = min(InfraredOutputValueMaximum, intensityRatio);
// 4. limiting the lower value InfraredOutputValueMinimym
intensityRatio = max(InfraredOutputValueMinimum, intensityRatio);
// 5. converting the normalized value to a byte and using the result
// as the RGB components required by the image
byte intensity = static_cast<byte>(intensityRatio * 255.0f);
pRGBX->rgbRed = intensity;
pRGBX->rgbGreen = intensity;
pRGBX->rgbBlue = intensity;
// convert UINT16 to Big-Endian format
(*pUINT16) = ((*pBuffer) >> 8) | ((*pBuffer) << 8);
--pBuffer;
++pRGBX;
++pUINT16;
}
pBuffer += (cInfraredWidth << 1);
}
// Draw the data with Direct2D
m_pDrawInfrared->Draw(reinterpret_cast<BYTE*>(m_pInfraredRGBX), cInfraredWidth * cInfraredHeight * sizeof(RGBQUAD));
if (m_bRecord)
{
if (!m_nStartTime)
{
if (IsDirectoryExists(m_cSaveFolder))
{
MessageBox(NULL,
L"The related folder is not emtpy!\n",
L"Frames already existed",
MB_OK | MB_ICONERROR
);
m_bRecord = false;
SendDlgItemMessage(m_hWnd, IDC_BUTTON_RECORD, BM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)m_hRecord);
return;
}
m_nStartTime = nTime;
}
// Write out the bitmap to disk (enqeue)
m_qInfraredTimeQueue.push(nTime - m_nStartTime);
m_qInfraredFrameQueue.push(m_pInfraredUINT16[index]);
++m_nInfraredIndex;
}
if (m_bShot)
{
m_nInfraredShotTime = nTime;
m_bShotReady = true;
}
}
}
/// <summary>
/// Handle new depth data
/// <param name="nTime">timestamp of frame</param>
/// <param name="pBuffer">pointer to frame data</param>
/// <param name="nWidth">width (in pixels) of input image data</param>
/// <param name="nHeight">height (in pixels) of input image data</param>
/// <param name="nMinDepth">minimum reliable depth</param>
/// <param name="nMaxDepth">maximum reliable depth</param>
/// </summary>
void CKinectV2Recorder::ProcessDepth(INT64 nTime, const UINT16* pBuffer, int nWidth, int nHeight, USHORT nMinDepth, USHORT nMaxDepth)
{
if (m_hWnd)
{
double fps = 0.0;
LARGE_INTEGER qpcNow = { 0 };
if (m_fFreq)
{
if (QueryPerformanceCounter(&qpcNow))
{
if (m_nDepthLastCounter)
{
m_nDepthFramesSinceUpdate++;
fps = m_fFreq * m_nDepthFramesSinceUpdate / double(qpcNow.QuadPart - m_nDepthLastCounter);
}
}
}
WCHAR szStatusMessage[128];
StringCchPrintf(szStatusMessage, _countof(szStatusMessage), L" Save Folder: %s FPS(Infrared, Depth, Color) = (%0.2f, %0.2f, %0.2f)", m_cSaveFolder, m_fInfraredFPS, m_fDepthFPS, m_fColorFPS);
if (m_nDepthFramesSinceUpdate % 30 == 0)