forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
depth_view_example.m
201 lines (162 loc) · 6.8 KB
/
depth_view_example.m
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
classdef depth_view_example < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
DepthAX matlab.ui.control.UIAxes
ColorAX matlab.ui.control.UIAxes
StartButton matlab.ui.control.Button
StopButton matlab.ui.control.Button
end
properties (Access = private) %, Hidden = true)
MeTimer % Timer object
DrawFlag % Boolean
Ctx % Realsense.context
Dev % Realsense.device
Cfg % Realsense.config
Pipe % Realsense.pipeline
Colorizer % Realsense.colorizer
Profile % Realsense.profile
Frameset % Realsense.frameset
hhDepth % Image
hhColor % Image
end
% Callbacks that handle component events
methods (Access = private)
function MeTimerFcn(app,~,~)
if ( app.DrawFlag == 1 )
% lock drawing process
app.DrawFlag = 0;
% Get frameset
app.Frameset = app.Pipe.wait_for_frames();
depth_frame = app.Frameset.get_depth_frame();
depth_color = app.Colorizer.colorize(depth_frame);
depth_data = depth_color.get_data();
depth_img = permute(reshape(depth_data',[3,depth_color.get_width(),depth_color.get_height()]),[3 2 1]);
[ki kj] = size (app.hhDepth);
if ki*kj < 1
app.hhDepth = imshow(depth_img,'Parent',app.DepthAX);
else
app.hhDepth.CData = depth_img;
end
color_frame = app.Frameset.get_color_frame();
color_data = color_frame.get_data();
color_img = permute(reshape(color_data',[3,color_frame.get_width(),color_frame.get_height()]),[3 2 1]);
[ki kj] = size (app.hhColor);
if ki*kj < 1
app.hhColor = imshow(color_img,'Parent',app.ColorAX);
else
app.hhColor.CData = color_img;
end
% unlock drawing process
app.DrawFlag = 1;
pause(0.001);
end
end
% Executes after component creation
function StartUpFunc(app)
% Create Realsense items
app.Ctx = realsense.context();
devs = app.Ctx.query_devices();
if ( numel(devs) < 1 )
error ( 'Not found RealSense device' );
end
app.Dev = devs{1};
sn = app.Dev.get_info(realsense.camera_info.serial_number);
app.Cfg = realsense.config();
app.Cfg.enable_device(sn);
app.Pipe = realsense.pipeline();
app.Colorizer = realsense.colorizer();
app.Profile = app.Pipe.start();
% Create timer object
kFramePerSecond = 30.0; % Number of frames per second
Period = double(int64(1000.0 / kFramePerSecond))/1000.0+0.001; % Frame Rate
app.MeTimer = timer(...
'ExecutionMode', 'fixedSpacing', ... % 'fixedRate', ... % Run timer repeatedly
'Period', Period, ... % Period (second)
'BusyMode', 'drop', ... %'queue',... % Queue timer callbacks when busy
'TimerFcn', @app.MeTimerFcn); % Specify callback function
app.DrawFlag = 0;
app.hhDepth = [];
app.hhColor = [];
end
% Button pushed function: start timer
function onStartButton(app, event)
% If timer is not running, start it
if strcmp(app.MeTimer.Running, 'off')
app.DrawFlag = 1;
start(app.MeTimer);
end
end
% Button pushed function: stop timer
function onStopButton(app, event)
app.DrawFlag = 0;
stop(app.MeTimer);
end
%Close request UIFigure function
function UIFigureCloseRequest(app,event)
app.DrawFlag = 0;
stop(app.MeTimer);
delete(app.MeTimer);
app.Pipe.stop();
delete(app.Profile);
delete(app.Colorizer);
delete(app.Pipe);
delete(app.Cfg);
delete(app.Dev);
delete(app.Ctx);
delete(app);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [20 20 1345 590];
app.UIFigure.Name = 'RealSenseViewStdDef Figure';
app.UIFigure.CloseRequestFcn = createCallbackFcn(app,@UIFigureCloseRequest);
setAutoResize(app,app.UIFigure,false);
% Create DepthAX
app.DepthAX = uiaxes(app.UIFigure);
app.DepthAX.Position = [25 100 640 480];
% Create ColorAX
app.ColorAX = uiaxes(app.UIFigure);
app.ColorAX.Position = [685 100 640 480];
% Create StartButton
app.StartButton = uibutton(app.UIFigure, 'push');
app.StartButton.ButtonPushedFcn = createCallbackFcn(app, @onStartButton, true);
app.StartButton.IconAlignment = 'center';
app.StartButton.Position = [100 22 100 22];
app.StartButton.Text = 'Start';
% Create StopButton
app.StopButton = uibutton(app.UIFigure, 'push');
app.StopButton.ButtonPushedFcn = createCallbackFcn(app, @onStopButton, true);
app.StopButton.IconAlignment = 'center';
app.StopButton.Position = [220 22 100 22];
app.StopButton.Text = 'Stop';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = depth_view_example
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Set Startup function - after component creation
runStartupFcn(app,@StartUpFunc);
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end