File: ShrinkMatrix.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 (47 lines) | stat: -rw-r--r-- 1,220 bytes parent folder | download | duplicates (6)
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
function out = ShrinkMatrix(in,fac)
% out = ShrinkMatrix(IN, FAC)
% Shrinks a 2-D or 3-D matrix IN (an image) by a factor FAC.
% matrix will be truncated horizontally and vertically so that the
% resultant shrunk matrix would have integer sizes in the x an y dimension.
% size of 3rd dimension will not be scaled.
% shrinking is performed by mean computation.

% 05/09/08 DN  Wrote it.
% 13/06/12 DN  Urgh, this only went over the diagonal of each NxN submatrix
%              (where N is the scaling factor)


% input checking
if ndims(in)>3
    error('input is not an image');
end

ys = size(in,1);
xs = size(in,2);

if fac ~= round(fac)
    error('scaling factor must be an integer');
end

hcut = mod(xs,fac);
vcut = mod(ys,fac);
if hcut~=0
    disp(sprintf('Warning: right edge of input will be truncated by %d pixels',hcut));
end
if vcut~=0
    disp(sprintf('Warning: lower edge of input will be truncated by %d pixels',vcut));
end

if hcut~=0 || vcut~=0
    in = in(1:end-vcut,1:end-hcut,:);
    xs = xs-hcut;
    ys = ys-vcut;
end

out = zeros(ys/fac,xs/fac,size(in,3));
for p=1:fac     % image rows
    for q=1:fac     % image columns
        out = out + in(p:fac:ys,q:fac:xs,:);
    end
end
out = out./(fac^2);