-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.m
110 lines (90 loc) · 3.21 KB
/
decode.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
106
107
108
109
function [C,goodpixels] = decode(imageprefix,start,stop,threshold,rgb_threshold)
% function [C,goodpixels] = decode(imageprefix,start,stop,threshold)
%
%
% Input:
%
% imageprefix : a string which is the prefix common to all the images.
%
% for example, pass in the prefix '/home/fowlkes/left/left_'
% to load the image sequence '/home/fowlkes/left/left_01.jpg'
% '/home/fowlkes/left/left_02.jpg'
% etc.
%
% start : the first image # to load
% stop : the last image # to load
%
% threshold : the pixel brightness should vary more than this threshold between the positive
% and negative images. if the absolute difference doesn't exceed this value, the
% pixel is marked as undecodeable.
%
% Output:
%
% C : an array containing the decoded values (0..1023)
%
% goodpixels : a binary image in which pixels that were decodedable across all images are marked with a 1.
I = imread(sprintf('%s%2.2d.jpg',imageprefix,start));
[h,w,d] = size(I);
goodpixels = ones(h,w);
bit = 1;
% start = start+9;
for i = start:stop
I = imread(sprintf('%s%2.2d.jpg',imageprefix,i));
In = imread(sprintf('%s%2.2d_i.jpg',imageprefix,i));
I = im2double(rgb2gray(I));
In = im2double(rgb2gray(In));
Ib(:,:,bit) = I > In;
goodpixels = goodpixels.*(abs(I-In) > threshold);
if (i == stop)
%% calculate goodpixels using rgb bg
I = imread(sprintf('%srgb.jpg',imageprefix));
In = imread(sprintf('%srgb_bg.jpg',imageprefix));
I = im2double(rgb2gray(I));
In = im2double(rgb2gray(In));
goodpixels = goodpixels.*((abs(I-In)) > rgb_threshold);
figure(1);
subplot(1,2,1); imagesc(Ib(:,:,bit)); axis image; title(sprintf('bit %d',bit));
subplot(1,2,2); imagesc(goodpixels); axis image; title('goodpixels');
drawnow;
end
% figure(1);
% subplot(1,2,1); imagesc(Ib(:,:,bit)); axis image; title(sprintf('bit %d',bit));
% subplot(1,2,2); imagesc(goodpixels); axis image; title('goodpixels');
% drawnow;
bit = bit + 1;
end
% %% calculate goodpixels using rgb bg
% I = imread(sprintf('%srgb.jpg',imageprefix));
% In = imread(sprintf('%srgb_bg.jpg',imageprefix));
% I = im2double(rgb2gray(I));
% In = im2double(rgb2gray(In));
% goodpixels = goodpixels.*(abs(I-In) > 0.08);
%
% %% plot goodpixels
% figure(1);
% subplot(1,2,1); imagesc(Ib(:,:,bit)); axis image; title(sprintf('bit %d',bit));
% subplot(1,2,2); imagesc(goodpixels); axis image; title('goodpixels');
% drawnow;
%
%
%
% build up a table of gray code values
% X maps an integer to its binary gray code sequence
%
X = dec2gray(0:1023,10);
% Xdec maps an integer to its decimal gray code value
Xdec = X*(2.^(0:9)');
% Xdecinv maps a decimal gray code value back to an integer
% it is the "inverse" of Xdec
[val,Xdecinv] = sort(Xdec);
% reshape Ib from (h,w,nbits) to (nbits,h,w)
% and then collapse the last two dimensions
% to make its dimension (nbits,h*w)
Ib = shiftdim(Ib,2);
Ib = Ib(1:10,:);
% convert the binary into a decimal
Ib = Ib'*(2.^(0:9)')+1;
% now look up the results in our table Xinv
% and reshape it back into the original image
% dimensions
C = reshape( Xdecinv(Ib) ,h,w);