-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdisplay_color_xyt.m
178 lines (157 loc) · 5.89 KB
/
display_color_xyt.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
function display_color_xyt(y_xyt, cb_xyt,cr_xyt, varargin)
% DISPLAY_COLOR_XYT
% Display a sequence of color images from memory to the screen. Image
% coordinates are x (horizontal), y (vertical), and t (time). Options
% exist for interlaced playback, slow motion, and looping.
% SYNTAX
% display_color_xyt(y,cb,cr)
% display_color_xyt(...,'Flag', ...)
% DEFINITION
% display_color_xyt(y,cb,cr) displays sequence of color images, 'y' is the
% luminance planes, 'cb' the Cb planes, and 'cr' the Cr planes. Each must
% have identical coordinates (row,col,time).
% Optional flags are:
% 'interlace_lower_field_first' indicates that y interlaced images have the
% lower field displayed earlier in time (e.g., D1 NTSC). The time
% history of fields will be displayed.
% 'interlace_upper_field_first' indicates that y interlaced images have the
% upper field displayed earlier in time (e.g., D1 Pal). The time
% history of fields will be displayed.
% 'progressive' requests a progressive playback. This is the default.
% 'slowmo' requests slow motion playback, pausing 1 second after each
% image display and beeping.
% 'subplot' don't open a new figure. Assume a figure is already open, and
% display the image or video with no axes.
% REMARKS
% Routine tested.
% Cb and Cr must contain values centered around zero (i.e., -128 to 127)
is_lower_first = 1;
is_interlace = 0;
is_slowmo = 0;
is_repeat = 1;
is_subplot = 0;
if nargin > 3
for cnt = 4:nargin
if strcmp(varargin{cnt-3},'interlace_upper_field_first') == 1
is_lower_first = 0;
is_interlace = 1;
elseif strcmp(varargin{cnt-3},'interlace_lower_field_first') == 1
is_lower_first = 1;
is_interlace = 1;
elseif strcmp(varargin{cnt-3},'progressive') == 1
is_interlace = 0;
elseif strcmp(varargin{cnt-3},'slowmo') == 1
is_slowmo = 1;
elseif strcmp(varargin{cnt-3},'subplot') == 1
is_subplot = 1;
else
error('display_color_xyt Flag not recognized');
end
end
end
% reshape video sequence into fields
if is_interlace
% find size of image
[num_rows, num_cols,num_frames] = size(y_xyt);
% reshape into fields
y_temp = reshape( y_xyt, 2, num_rows/2, num_cols, num_frames );
y_xyt = zeros(2, num_rows/2, num_cols, 2*num_frames);
cb_temp = reshape( cb_xyt, 2, num_rows/2, num_cols, num_frames );
cb_xyt = zeros(2, num_rows/2, num_cols, 2*num_frames);
cr_temp = reshape( cr_xyt, 2, num_rows/2, num_cols, num_frames );
cr_xyt = zeros(2, num_rows/2, num_cols, 2*num_frames);
if is_lower_first
early = 2;
late = 1;
else
early = 1;
late = 2;
end
% form a progressive frame from each field
y_xyt(1, :, :, 1:2:2*num_frames) = y_temp(early, :, :, :);
y_xyt(2, :, :, 1:2:2*num_frames) = y_temp(early, :, :, :);
y_xyt(1, :, :, 2:2:2*num_frames) = y_temp(late, :, :, :);
y_xyt(2, :, :, 2:2:2*num_frames) = y_temp(late, :, :, :);
cb_xyt(1, :, :, 1:2:2*num_frames) = cb_temp(early, :, :, :);
cb_xyt(2, :, :, 1:2:2*num_frames) = cb_temp(early, :, :, :);
cb_xyt(1, :, :, 2:2:2*num_frames) = cb_temp(late, :, :, :);
cb_xyt(2, :, :, 2:2:2*num_frames) = cb_temp(late, :, :, :);
cr_xyt(1, :, :, 1:2:2*num_frames) = cr_temp(early, :, :, :);
cr_xyt(2, :, :, 1:2:2*num_frames) = cr_temp(early, :, :, :);
cr_xyt(1, :, :, 2:2:2*num_frames) = cr_temp(late, :, :, :);
cr_xyt(2, :, :, 2:2:2*num_frames) = cr_temp(late, :, :, :);
% reshape back
y_xyt = reshape( y_xyt, num_rows, num_cols, 2*num_frames);
cb_xyt = reshape( cb_xyt, num_rows, num_cols, 2*num_frames);
cr_xyt = reshape( cr_xyt, num_rows, num_cols, 2*num_frames);
clear y_temp cb_temp cr_temp;
end
% find size of image
[num_rows, num_cols,num_frames] = size(y_xyt);
if ~is_subplot
figure('Units', 'pixels', 'Position', [100 100 num_cols num_rows],'Name','Display Color XYT');
set(gca, 'Position', [0 0 1 1]);
end
colormap(gray(256));
%
for loop = 1:is_repeat
% loop through and display color images
for cnt = 1:num_frames
% convert to RGB computer, same algorithm as ycbcr2rgb_double
y = (y_xyt(:,:,cnt)-16)*1.164384;
cb = (cb_xyt(:,:,cnt))*2.017233;
cr = (cr_xyt(:,:,cnt))*1.596027;
red = cr+y;
green = y-0.194208*cb-0.509370*cr;
blue = cb+y;
if cnt ~= 1
% display previous frame. Do this here instead of when it is
% produced, so that frames come out at a more even rate.
set(h, 'CData', wrap_rgb)
drawnow
% pause & beep if requested.
if is_slowmo
beep;
pause(1.0);
end
end
% clip pixel values to be integers within the valid range.
red = max(0, min(red,255)) / 255;
green = max(0, min(green,255)) / 255;
blue = max(0, min(blue,255)) / 255;
if cnt == 1
% display first frame
wrap_rgb = cat(3,red,green,blue);
old_red = red;
old_green = green;
old_blue = blue;
if loop == 1
h = image(wrap_rgb);
else
set(h, 'CData', wrap_rgb)
drawnow
end
if is_subplot
axis 'off'
end
% pause & beep if requested.
if is_slowmo
beep;
pause(1.0);
end
else
% compute RGB for this frame (but wait to display it, so that
% frames will be displayed more evenly in time)
wrap_rgb = cat(3,red,green,blue);
pause(0.02);
end
end
% display final frame.
set(h, 'CData', wrap_rgb)
drawnow
% pause & beep if requested.
if is_slowmo
beep;
pause(1.0);
end
end