-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathMainPage.xaml.cpp
More file actions
174 lines (147 loc) · 5.41 KB
/
MainPage.xaml.cpp
File metadata and controls
174 lines (147 loc) · 5.41 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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "pch.h"
#include "MainPage.xaml.h"
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
namespace ComputeOnDesktop
{
MainPage::MainPage()
{
InitializeComponent();
}
void MainPage::ConnectSocket_Click()
{
//
// By default 'HostNameForConnect' is disabled and host name validation is not required. When enabling the
// text box validating the host name is required since it was received from an untrusted source
// (user input). The host name is validated by catching ArgumentExceptions thrown by the HostName
// constructor for invalid input.
//
Windows::Networking::HostName^ hostName;
try
{
hostName = ref new Windows::Networking::HostName(
HostNameForConnect->Text);
}
catch (Platform::Exception^)
{
return;
}
Windows::Networking::Sockets::StreamSocket^ pvCameraSocket =
ref new Windows::Networking::Sockets::StreamSocket();
pvCameraSocket->Control->KeepAlive = true;
//
// Save the socket, so subsequent steps can use it.
//
concurrency::create_task(
pvCameraSocket->ConnectAsync(hostName, _pvServiceName->Text)
).then(
[this, pvCameraSocket]()
{
#if DBG_ENABLE_VERBOSE_LOGGING
dbg::trace(
L"MainPage::ConnectSocket_Click: server connection established");
#endif /* DBG_ENABLE_VERBOSE_LOGGING */
HoloLensForCV::SensorFrameReceiver^ receiver =
ref new HoloLensForCV::SensorFrameReceiver(
pvCameraSocket);
ReceiverLoop(
receiver);
});
}
void MainPage::ReceiverLoop(
HoloLensForCV::SensorFrameReceiver^ receiver)
{
concurrency::create_task(
receiver->ReceiveAsync()).then(
[this, receiver](concurrency::task<HoloLensForCV::SensorFrame^> sensorFrameTask)
{
HoloLensForCV::SensorFrame^ sensorFrame =
sensorFrameTask.get();
#if DBG_ENABLE_VERBOSE_LOGGING
dbg::trace(
L"MainPage::ReceiverLoop: receiving a %ix%i image of type %i with timestamp %llu",
sensorFrame->SoftwareBitmap->PixelWidth,
sensorFrame->SoftwareBitmap->PixelHeight,
(int32_t)sensorFrame->FrameType,
sensorFrame->Timestamp);
#endif /* DBG_ENABLE_VERBOSE_LOGGING */
OnFrameReceived(
sensorFrame);
Windows::UI::Core::CoreDispatcher^ uiThreadDispatcher =
Windows::ApplicationModel::Core::CoreApplication::MainView->CoreWindow->Dispatcher;
uiThreadDispatcher->RunAsync(
Windows::UI::Core::CoreDispatcherPriority::Normal,
ref new Windows::UI::Core::DispatchedHandler(
[this, sensorFrame]()
{
switch (sensorFrame->FrameType)
{
case HoloLensForCV::SensorType::PhotoVideo:
{
Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource^ imageSource =
ref new Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource();
concurrency::create_task(
imageSource->SetBitmapAsync(sensorFrame->SoftwareBitmap)
).then(
[this, imageSource]()
{
_pvImage->Source = imageSource;
}, concurrency::task_continuation_context::use_current());
}
break;
default:
throw new std::logic_error("invalid frame type");
}
}));
ReceiverLoop(
receiver);
});
}
void MainPage::OnFrameReceived(
HoloLensForCV::SensorFrame^ sensorFrame)
{
cv::Mat wrappedImage;
rmcv::WrapHoloLensSensorFrameWithCvMat(
sensorFrame,
wrappedImage);
cv::Mat blurredImage;
cv::medianBlur(
wrappedImage,
blurredImage,
3 /* ksize */);
cv::Mat cannyImage;
cv::Canny(
blurredImage,
cannyImage,
50.0,
200.0);
for (int32_t y = 0; y < blurredImage.rows; ++y)
{
for (int32_t x = 0; x < blurredImage.cols; ++x)
{
if (cannyImage.at<uint8_t>(y, x) > 64)
{
wrappedImage.at<uint32_t>(y, x) = 0xFF00FF00;
}
}
}
}
}