-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwavework.m
105 lines (96 loc) · 3.55 KB
/
wavework.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
function [varargout] = wavework(opcode, type, c, s, n, x)
%WAVEWORK is used to edit wavelet decomposition structures.
% [VARARGOUT] = WAVEWORK(OPCODE, TYPE, C, S, N, X) gets the
% coefficients specified by the TYPE and N for access or modification
% based on OPCODE.
%
% INPUTS:
% OPCODE Operation to perform
% -----------------------------------------------------------------
% 'copy' [varargout] = Y = Requested (via TYPE and N)
% coefficient matrix
% 'cut' [varargout] = [NC, Y] = New decomposition vector
% (with requested coefficient matrix zeroed) AND
% requested coefficient matrix
% 'paste' [varargout] = [NC] = New decomposition vector with
% coefficient matrix replaced by X
%
% TYPE Coefficient category
% -----------------------------------------------------------------
% 'a' Approximation coefficients
% 'h' Horizontal details
% 'v' Vertical details
% 'd' Diagonal details
%
% [C, S] is a wavelet toolbox decomposition structure.
% N is a decomposition level (ignored if TYPE = 'a').
% X is a two-dimensional coefficient matrix for pasting.
%
% See also WAVECUT, WAVECOPY, AND WAVEPASTE.
%
% Taken from: Rafael C. Gonzalez, Richard E. Woods. and Steven L. Eddins,
% Digital Image Processing Using MATLAB®, Prentice-Hall Inc, 2002.
narginchk(4, 6);
if (~ismatrix(c)) || (size(c, 1) ~= 1)
error('C must be a row vector.');
end
if (~ismatrix(s)) || ~isreal(s) || ~isnumeric(s) || (size(s, 2) ~= 2)
error('S must be a real, numeric two-column array.');
end
elements = prod(s, 2); % Coefficient matrix elements.
if (length(c) < elements(end)) || ...
~(elements(1) + 3 * sum(elements(2:end - 1)) >= elements(end))
error(['[C, S] must form a standard wavelet decomposition '...
'structure.']);
end
if strcmpi(opcode(1:3), 'pas') && nargin < 6
error('Not enough input arguments');
end
if nargin < 5
n = 1; % Default level is 1.
end
nmax = size(s, 1) - 2; % Maximum levels in [C, S].
aflag = (lower(type(1)) == 'a');
if ~aflag && (n > nmax)
error('N exeeds the decompositions in [C, S].');
end
switch lower(type(1)) % Make pointers into C.
case 'a'
nindex = 1;
start = 1; stop = elements(1); ntst = nmax;
case {'h', 'v', 'd'}
switch type
case 'h', offset = 0; % Offset to details.
case 'v', offset = 1;
case 'd', offset = 2;
end
nindex = size(s, 1) - n; % Index to detail info.
start = elements(1) + 3 * sum(elements(2:nmax - n + 1)) + ...
offset * elements(nindex) + 1;
stop = start + elements(nindex) - 1;
ntst = n;
otherwise
error('TYPE must begin with "a", "h", "v", or "d". ');
end
switch lower(opcode) % Do requested action.
case {'copy', 'cut'}
y = zeros(s(nindex, :));
y(:) = c(start:stop);
nc = c;
if strcmpi(opcode(1:3), 'cut')
nc(start:stop) = 0;
varargout = {nc, y};
else
varargout = {y};
end
case 'paste'
if numel(x) ~= elements(end - ntst)
error('X is not sized for the requested paste.');
else
nc = c;
nc(start:stop) = x(:);
varargout = {nc};
end
otherwise
error('Unrecognized OPCODE.');
end