Skip to content

Commit 8910a4a

Browse files
author
nath@W740SU
committed
own dwt
1 parent 6ca553d commit 8910a4a

File tree

4 files changed

+107
-19
lines changed

4 files changed

+107
-19
lines changed

cshift.m

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function shifted = cshift(orig, shift, dir)
2+
3+
% 2D Circular Shift
4+
%
5+
% USAGE:
6+
% y = cshift2D(x, m)
7+
% INPUT:
8+
% x - M by N array
9+
% m - amount of shift
10+
% OUTPUT:
11+
% y - matrix x will be shifed by m samples down
12+
%
13+
% WAVELET SOFTWARE AT POLYTECHNIC UNIVERSITY, BROOKLYN, NY
14+
% http://taco.poly.edu/WaveletSoftware/
15+
16+
[nr, nc] = size(orig);
17+
if dir == 1
18+
n = 0:nr-1;
19+
n = mod(n-shift, nr);
20+
shifted = orig(n+1,:);
21+
elseif dir == 2
22+
n = 0:nc-1;
23+
n = mod(n-shift, nc);
24+
shifted = orig(:,n+1);
25+
end
26+
end
27+
28+

main.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
clear all; close all;
22

33
MIN_THRESH = 4;
4-
LEVEL = 5;
4+
LEVEL = 6;
55
LOGRESCALE = 0;
66
IMAGE = 'castle.png';
77
OIMAGE = 'castle';

singledec.m

+44-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,46 @@
11
function [A,H,V,D] = singledec(img,LPF,HPF)
2-
if size(LPF,1) > 1
3-
LPF = LPF';
4-
end
5-
if size(HPF,1) > 1
6-
HPF = HPF';
7-
end
8-
n = size(img,3);
9-
for i =1:n
10-
[a_, h_, v_, d_] = dwt2(img(:,:,i), LPF, HPF, 'mode','per');
11-
A(:,:,i) = a_;
12-
H(:,:,i) = h_;
13-
V(:,:,i) = v_;
14-
D(:,:,i) = d_;
15-
end
2+
if size(LPF,1) > 1
3+
LPF = LPF';
4+
end
5+
if size(HPF,1) > 1
6+
HPF = HPF';
7+
end
8+
L = length(LPF)/2;
9+
[nr, nc, nl] = size(img);
10+
for layer = 1:nl
11+
imglayer = cshift(img(:,:,layer), -L, 2);
12+
13+
convresult = conv2(imglayer, LPF);
14+
hzlo = convresult(:,1:2:end);
15+
hzlo(:, 1:L) = hzlo(:, 1:L) + hzlo(:,(1:L)+nc/2);
16+
hzlo = hzlo(:,1:nc/2);
17+
18+
convresult = conv2(imglayer, HPF);
19+
hzhi = convresult(:,1:2:end);
20+
hzhi(:, 1:L) = hzhi(:, 1:L) + hzhi(:,(1:L)+nc/2);
21+
hzhi = hzhi(:,1:nc/2);
22+
23+
hzlo = cshift(hzlo, -L, 1);
24+
hzhi = cshift(hzhi, -L, 1);
25+
26+
convresult = conv2(hzlo, LPF');
27+
alayer = convresult(1:2:end,:);
28+
alayer(1:L, :) = alayer(1:L, :) + alayer((1:L)+nr/2, :);
29+
A(:,:,layer) = alayer(1:nr/2,:);
30+
31+
convresult = conv2(hzhi, LPF');
32+
hlayer = convresult(1:2:end,:);
33+
hlayer(1:L, :) = hlayer(1:L, :) + hlayer((1:L)+nr/2, :);
34+
H(:,:,layer) = hlayer(1:nr/2,:);
35+
36+
convresult = conv2(hzlo, HPF');
37+
vlayer = convresult(1:2:end,:);
38+
vlayer(1:L, :) = vlayer(1:L, :) + vlayer((1:L)+nr/2, :);
39+
V(:,:,layer) = vlayer(1:nr/2,:);
40+
41+
convresult = conv2(hzhi, HPF');
42+
dlayer = convresult(1:2:end,:);
43+
dlayer(1:L, :) = dlayer(1:L, :) + dlayer((1:L)+nr/2, :);
44+
D(:,:,layer) = dlayer(1:nr/2,:);
45+
end
1646
end

singlerec.m

+34-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,38 @@
55
if size(HPF,1) > 1
66
HPF = HPF';
77
end
8-
o = size(A,3);
9-
for i =1:o
10-
img(:,:,i) = idwt2(A(:,:,i), H(:,:,i), V(:,:,i), D(:,:,i), LPF, HPF,'mode','per');
11-
end
8+
[nr, nc, nl] = size(A);
9+
for layer = 1:nl
10+
aup = zeros(nr, 2*nc);
11+
aup(:,1:2:end) = A(:,:,layer);
12+
hup = zeros(nr, 2*nc);
13+
hup(:,1:2:end) = H(:,:,layer);
14+
vtlo = conv2(aup,LPF )+conv2(hup,HPF);
15+
L = length(LPF);
16+
vtlo(:, 1:L-2) = vtlo(:,1:L-2) + vtlo(:, 2*nc+(1:L-2));
17+
vtlo = vtlo(:,1:2*nc);
18+
vtlo = cshift(vtlo, 1-L/2, 2);
19+
20+
vup = zeros(nr, 2*nc);
21+
vup(:,1:2:end) = V(:,:,layer);
22+
dup = zeros(nr, 2*nc);
23+
dup(:,1:2:end) = D(:,:,layer);
24+
vthi = conv2(vup,LPF)+conv2(dup,HPF);
25+
vthi(:, 1:L-2) = vthi(:,1:L-2) + vthi(:, 2*nc+(1:L-2));
26+
vthi = vthi(:,1:2*nc);
27+
vthi = cshift(vthi, 1-L/2, 2);
28+
29+
vtloup = zeros(2*nr,2*nc);
30+
vtloup(1:2:end,:) = vtlo;
31+
vthiup = zeros(2*nr,2*nc);
32+
vthiup(1:2:end,:) = vthi;
33+
34+
layerout = conv2(vtloup,LPF')+conv2(vthiup,HPF');
35+
layerout(1:L-2,:) = layerout(1:L-2,:) + layerout(2*nr+(1:L-2), :);
36+
layerout = layerout(1:2*nr, :);
37+
layerout = cshift(layerout, 1-L/2, 1);
38+
39+
img(:,:,layer) = layerout;
40+
end
41+
1242
end

0 commit comments

Comments
 (0)