File: VignettingCorrectionDemo.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 (131 lines) | stat: -rw-r--r-- 4,472 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
124
125
126
127
128
129
130
131
function VignettingCorrectionDemo(docolor, precision)
% Demonstrate how to do display devignetting aka per-pixel gain correction.
%
% Usage: VignettingCorrectionDemo([docolor=0][, precision=max]);
%
% 'docolor' if set to non-zero will compute and apply per-color channel
% gains instead of the default luminance gain.
%
% 'precision' selects the precision vs. speed tradeoff: 2 = max precision,
% min speed, 1 = balanced, 0 = max speed and minimum precision.
%
% Because gain correction is memory and compute intense, per-color
% correction is slower than global luminance only correction. Higher
% precisions for the definition of the gainmatrix are slower, and higher
% display resolutions are slower.
%
% The demo runs until a key is hit twice.
%
% On modern graphics cards, Psychtoolbox allows to automatically apply a
% luminance- or color gainfield to all presented stimuli. Each pixel
% location can get individually gain corrected. This is useful to correct
% for spatial luminance or color inhomogenities of display devices, e.g.,
% video projectors (due to lens vignetting effects), or flat panels (with
% their contrast and color view dependency).
%
% You need recent graphics hardware for this to work and to work at a
% decent speed.
%
% See "help PsychImaging" under subsection 'ColorCorrection' and "help
% PsychColorCorrection" under subsection 'GainMatrix' for explanation of
% the math, the parameters and other notable things of interest about gain
% correction.
%

% History:
% 6.3.2010  mk   Written.
%

% Assign parameters or defaults:
if nargin < 1 || isempty(docolor)
    docolor = 0;
end

if nargin < 2
    precision = [];
end

% Check for properly installed Psychtoolbox:
AssertOpenGL;

% Output to max screen:
screenid = max(Screen('Screens'));

% Use imaging pipeline for visual processing and display:
PsychImaging('PrepareConfiguration');

% Request per-pixel 2D gain correction for display:
PsychImaging('AddTask', 'FinalFormatting', 'DisplayColorCorrection', 'GainMatrix');

% Open window with gray background (128 aka 50% gray), return handle 'win':
win = PsychImaging('OpenWindow', screenid, 128);

try
    Screen('TextSize', win, 48);

    % Build a simple luminance gain matrix, the size of the window, with a
    % simple linear gradient of increasing gain values from left to right, here
    % in the gain range from 0.0 to 1.5:
    [width, height] = Screen('WindowSize', win);

    % Test support for display gain correction:
    
    % Try to read from default configuration file:
    filename = [PsychtoolboxConfigDir('ShadingCalibration') 'VignetCalibration' sprintf('_%i_%i_%i', screenid, width, height) '.mat'];
    if exist(filename, 'file')
        load(filename, 'gainMatrix');
    else
        % gainmatrix is a linear "gain gradient" from 0.0 at the left border to
        % 1.5 at the right border of the display:
        gainMatrix = (meshgrid(1:width, 1:height) / width) * 1.5;
    end
    
    % If per-colorchannel correction is requested...
    if docolor
        % Replicate linear gains into all three color gain channels...
        gainMatrix = repmat(gainMatrix, [1 1 3]);
        % ... and invert the gradient in the green channel to make it
        % visually more interesting:
        gainMatrix(:,:,2) = (1.5 - 1.5 * meshgrid(1:width, 1:height) / width);
    end
    
    % Apply gain correction matrix to display: This will take effect at the
    % next Screen('Flip'). We pass in a 'precision' flag as well, to choose
    % the tradeoff between accuracy and speed:
    PsychColorCorrection('SetGainMatrix', win, gainMatrix, [], precision);

    KbReleaseWait;
    fc = 0;
    tstart = GetSecs;
    
    % Repeat redraw loop until keypress.
    while ~KbCheck
        fc = fc + 1;
        cl = mod(fc, 170);

        % Draw a yellow oval to the screen which changes brightness periodically:
        Screen('FillOval', win, [cl cl 0]);

        % Text drawing is time consuming, btw...
        DrawFormattedText(win, 'Hit any key twice to finish!', 'center', 'center', [0 0 128]);

        % Show updated image at next refresh cycle:
        Screen('Flip', win);
    end

    % Some stats...
    fprintf('\n\nAvg redraw rate was %f Hz.\n\n', fc / (GetSecs - tstart));
    
    % Wait for keyboard stroke before closing down...
    KbStrokeWait;
        
    % Done. Close everything down and terminate:
    sca;
    
catch
    % Usual error handling...
    sca;
    psychrethrow(psychlasterror);
end

return;