File: VideoCaptureToMatlabDemo.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (123 lines) | stat: -rw-r--r-- 4,373 bytes parent folder | download | duplicates (5)
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
function VideoCaptureToMatlabDemo(deviceIndex)
% VideoCaptureToMatlabDemo([deviceIndex])
%
% Minimalistic demo on how to capture video data and return it in a
% Matlab/Octave matrix.
%
% The demo starts video capture on a video device, auto-detected or
% specified by optional 'deviceIndex'. Instead of requesting images as
% textures and displaying them in a ptb onscreen window, it requests images
% as raw data in a matrix rawImage. After converting rawImage into the
% standard Matlab/Octave image matrix format, it imshow()'s the image in a
% standard figure window.
%
% Press any key or wait for 600 seconds to end the demo.
%

AssertOpenGL;

% Default is to auto-detect video device to use:
if nargin < 1
    deviceIndex = [];
end

try
    % Open a minimalistic window (only 10 x 10 pixels). This is needed
    % because we need to provide a 'win'dowhandle to the videocapture
    % functions. We skip startup timing tests...
    skipsync = Screen('Preference','SkipSyncTests', 2);

    % Disable high precision timestamping:
    timstampm = Screen('Preference', 'VBLTimestampingMode', -1);
    
    % ... and we make the window completely invisible, as we don't need it
    % for showing anything to the user. A shielding level of -1 does this:
    winlevel = Screen('Preference','WindowShieldinglevel', -1);
    
    % Open an invisible dummy window of 10 x 10 pixels size:
    win = Screen('OpenWindow', 0, 0, [0 0 10 10]);
    
    % Open videocapture device, requesting 640 x 480 pixels resolution:
    grabber = Screen('OpenVideoCapture', win, deviceIndex, [0 0 640 480],[],[],[],[],[],[],8);
    
    % Start low-latency capture with requested 30 fps:
    Screen('StartVideoCapture', grabber, 30, 1);
    
    oldpts = 0;
    count = 0;
    t=GetSecs;
    
    % Capture for 600 seconds or until keypress:
    while (GetSecs - t) < 600
        if KbCheck
            break;
        end
        
        % Retrieve next captured image in 'rawImage'. The 'waitforImage=2'
        % flag disables texture creation, so 'tex' is actually an empty
        % handle. The 'specialMode'=2 flag requests video data as matrix:
        [tex pts nrdropped rawImage]=Screen('GetCapturedImage', win, grabber, 2, [], 2); %#ok<ASGLU>

        % Display class of returned image matrix:
        matrixClassIs = class(rawImage)
        
        % Bits of accounting and stats to the Matlab window:
        if count > 0
            % Compute delta:
            delta = (pts - oldpts) * 1000;
            fprintf('%.4f\n', delta);
        end
        oldpts = pts;
        
        % Convert rawImage matrix into a matrix suitable for display with
        % Matlabs imshow(). imshow needs a height x width x 3 colors
        % matrix, whereas rawImage is a c by width x height matrix with c=1
        % for luminance data, c=3 or 4 for RGB data, where the 4th
        % component - if present - is a useless alpha channel.
        channels = min(size(rawImage,1), 3);
        for ci=1:channels
            if channels == 1
                tci=1;
            else
                tci = 4 - ci;
            end
            matImage(1:size(rawImage,3), 1:size(rawImage,2), tci) = transpose(squeeze(rawImage(ci,:,:))); %#ok<AGROW>
        end
        
        % Show image in a figure window:
        imshow(matImage);

        % Count and print number of unique values in image - An indicator of net bitdepth:
        uniquevalues = length(unique(matImage(:)))
        
        drawnow;
        count = count + 1;
    end
    
    telapsed = GetSecs - t;
    
    % Stop capture, close engine and onscreen window:
    Screen('StopVideoCapture', grabber);
    Screen('CloseVideoCapture', grabber);
    sca;
    
    % Restore preference settings:
    Screen('Preference','SkipSyncTests', skipsync);
    Screen('Preference', 'VBLTimestampingMode', timstampm);
    Screen('Preference','WindowShieldinglevel', winlevel);
    
    avgfps = count / telapsed;
    fprintf('\n\nElapsed time %f secs, average fps %f.\n', telapsed, avgfps);
    
catch %#ok<CTCH>
    % Close windows and shutdown capture engine:
    sca;
    
    % Restore preference settings:
    Screen('Preference','SkipSyncTests', skipsync);
    Screen('Preference', 'VBLTimestampingMode', timstampm);
    Screen('Preference','WindowShieldinglevel', winlevel);
    
    % Throw error:
    psychrethrow(psychlasterror);
end