1 | % Read all images and extract point coordinates. |
---|
2 | % |
---|
3 | % All information needed are stored and retrieved |
---|
4 | % from the function CONFIGDATA |
---|
5 | |
---|
6 | % $Author: svoboda $ |
---|
7 | % $Revision: 2.6 $ |
---|
8 | % $Id: im2points.m,v 2.6 2005/05/23 16:26:03 svoboda Exp $ |
---|
9 | % $State: Exp $ |
---|
10 | |
---|
11 | clear all; |
---|
12 | |
---|
13 | % add path to config data |
---|
14 | addpath ../../CommonCfgAndIO |
---|
15 | % add path for graphical output if needed |
---|
16 | addpath ../OutputFunctions |
---|
17 | |
---|
18 | SHOWFIG = 0; % show images during point extraction |
---|
19 | STEP4STAT = 1; % step for computing average and std images, if 1 then all images taken |
---|
20 | |
---|
21 | config = configdata(expname); |
---|
22 | |
---|
23 | im.dir = config.paths.img; |
---|
24 | im.ext = config.files.imgext; |
---|
25 | |
---|
26 | NoCams = size(config.files.idxcams,2); % number of cameras |
---|
27 | |
---|
28 | % load image names |
---|
29 | for i=1:NoCams, |
---|
30 | seq(i).camId = config.files.idxcams(i); |
---|
31 | if seq(i).camId > -1 |
---|
32 | if findstr(expname,'oscar') |
---|
33 | seq(i).data = dir([sprintf(im.dir,seq(i).camId),config.files.imnames,'*.',im.ext]); |
---|
34 | else |
---|
35 | seq(i).data = dir([sprintf(im.dir,seq(i).camId),sprintf(config.files.imnames,seq(i).camId),im.ext]); |
---|
36 | end |
---|
37 | else |
---|
38 | seq(i).data = dir([im.dir,sprintf(config.files.imnames),im.ext]); |
---|
39 | end |
---|
40 | seq(i).size = size(seq(i).data,1); |
---|
41 | if seq(i).size<4 |
---|
42 | error('Not enough images found. Wrong image path or name pattern?'); |
---|
43 | end |
---|
44 | end |
---|
45 | |
---|
46 | |
---|
47 | % create an occupancy matrix for image frames |
---|
48 | occmat=1; try config.files.maxid; catch occmat=0; end |
---|
49 | if occmat, |
---|
50 | NoPoints = config.files.maxid; |
---|
51 | FrameMat = zeros(config.files.maxid,NoCams); |
---|
52 | for i=1:NoCams, |
---|
53 | seq(i).imgidx = zeros(size(1:NoPoints)); |
---|
54 | for j=1:size(seq(i).data,1) |
---|
55 | FrameMat(str2num(seq(i).data(j).name(config.files.posid)),i)=j; |
---|
56 | end |
---|
57 | end |
---|
58 | else |
---|
59 | NoPoints = min([seq.size]); |
---|
60 | FrameMat = zeros(NoPoints,NoCams); |
---|
61 | for i=1:NoCams, |
---|
62 | FrameMat(:,i) = [1:NoPoints]'; |
---|
63 | end |
---|
64 | end |
---|
65 | |
---|
66 | % In fact, some frames might be without any calibration point |
---|
67 | |
---|
68 | % Becouse of non-consistent stopping of capturing, the sequences might |
---|
69 | % have different number of images, select the minimal value as the right one |
---|
70 | |
---|
71 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
72 | %%% beginning of the findings |
---|
73 | |
---|
74 | t = cputime; |
---|
75 | for i=1:NoCams, |
---|
76 | if ~exist(sprintf(config.files.avIM,seq(i).camId)), |
---|
77 | disp(sprintf('The average image of the camera %d is being computed',seq(i).camId)); |
---|
78 | avIM = zeros(size(imread([sprintf(im.dir,seq(i).camId),seq(i).data(1).name]))); |
---|
79 | pointIdx = 1:STEP4STAT:seq(i).size; |
---|
80 | for j=pointIdx, |
---|
81 | IM = imread([sprintf(im.dir,seq(i).camId),seq(i).data(j).name]); |
---|
82 | avIM = avIM + double(IM); |
---|
83 | end |
---|
84 | avIM = uint8(round(avIM./size(pointIdx,2))); |
---|
85 | imwrite(avIM,sprintf(config.files.avIM,seq(i).camId)); |
---|
86 | else disp('Average file already exists'); |
---|
87 | end |
---|
88 | end |
---|
89 | disp(sprintf('Elapsed time for computation of average images: %4.2f [sec]',cputime-t)) |
---|
90 | % compute the standard deviations images that will be used for finding LEDs |
---|
91 | % if not already computed |
---|
92 | t = cputime; |
---|
93 | for i=1:NoCams, |
---|
94 | if ~exist(sprintf(config.files.stdIM,seq(i).camId)), |
---|
95 | avIM = double(imread(sprintf(config.files.avIM,seq(i).camId))); |
---|
96 | disp(sprintf('The image of standard deviations of the camera %d is being computed',seq(i).camId)); |
---|
97 | stdIM = zeros(size(imread([sprintf(im.dir,seq(i).camId),seq(i).data(1).name]))); |
---|
98 | pointIdx = 1:STEP4STAT:seq(i).size; |
---|
99 | for j=pointIdx, |
---|
100 | IM = imread([sprintf(im.dir,seq(i).camId),seq(i).data(j).name]); |
---|
101 | stdIM = stdIM + (double(IM)-avIM).^2; |
---|
102 | end |
---|
103 | stdIM = uint8(round(sqrt(stdIM./(size(pointIdx,2)-1)))); |
---|
104 | imwrite(stdIM,sprintf(config.files.stdIM,seq(i).camId)); |
---|
105 | else |
---|
106 | disp('Image of standard deviations already exists') |
---|
107 | end |
---|
108 | end |
---|
109 | |
---|
110 | disp(sprintf('Elapsed time for computation of variance images: %4.2f [sec]',cputime-t)) |
---|
111 | |
---|
112 | % find points in the images |
---|
113 | Ws = []; % joint image matrix |
---|
114 | Res = []; % resolution of cameras |
---|
115 | % UsableFramesIdx = find(sum(FrameMat')>2); |
---|
116 | IdMat = ones(NoCams,NoPoints); |
---|
117 | % IdMat is very important for Martinec&Pajdla filling [ECCV2002] |
---|
118 | % it is a NoCams x NoPoints matrix, |
---|
119 | % IdMat(i,j) = 0 -> no j-th point in i-th |
---|
120 | % IdMat(i,j) = 1 -> point successfully detected |
---|
121 | |
---|
122 | |
---|
123 | disp('*********************************************') |
---|
124 | disp('Finding points (laser projections) in cameras') |
---|
125 | disp(sprintf('Totally %d cameras, %d images for each cam', NoCams, NoPoints')) |
---|
126 | disp('*********************************************') |
---|
127 | for i=1:NoCams, |
---|
128 | t1 = cputime; |
---|
129 | disp(sprintf('Finding points in camera No: %0.2d',config.files.idxcams(i))) |
---|
130 | Points = []; |
---|
131 | avIM = imread(sprintf(config.files.avIM,seq(i).camId)); |
---|
132 | stdIM = imread(sprintf(config.files.stdIM,seq(i).camId)); |
---|
133 | for j=1:NoPoints, |
---|
134 | fprintf(1,'\b\b\b\b\b\b %5d',j); |
---|
135 | idx2data = FrameMat(j,i); |
---|
136 | if idx2data |
---|
137 | [pos,err] = getpoint([sprintf(im.dir,seq(i).camId),seq(i).data(idx2data).name], SHOWFIG, config.imgs, avIM, stdIM); |
---|
138 | else |
---|
139 | err = 1; |
---|
140 | end |
---|
141 | if err |
---|
142 | IdMat(i,j) = 0; |
---|
143 | Points = [Points, [NaN; NaN; NaN]]; |
---|
144 | else |
---|
145 | Points = [Points, [pos; 1]]; |
---|
146 | end |
---|
147 | end |
---|
148 | Ws = [Ws; Points]; |
---|
149 | Res= [Res; size(avIM,2), size(avIM,1)]; |
---|
150 | t2 = cputime; |
---|
151 | disp(sprintf('\nElapsed time for finding points in one camera: %d minutes %d seconds',floor((t2-t1)/60), round(mod((t2-t1),60)))) |
---|
152 | disp(sprintf('%4d points found in camera No: %0.2d',sum(Points(3,:)>0),config.files.idxcams(i))); |
---|
153 | end |
---|
154 | |
---|
155 | %%% End of the findings |
---|
156 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
157 | |
---|
158 | if findstr(expname,'oscar') |
---|
159 | % needs special care for handling projector data |
---|
160 | ProjPoints = load(config.files.projdata,'-ASCII'); |
---|
161 | Ws = [Ws; ProjPoints(:,end-1:end)'; ones(size(ProjPoints(:,1)'))]; |
---|
162 | IdMat = [IdMat; ones(size(ProjPoints(:,1)'))]; |
---|
163 | Res = [Res; config.imgs.projres]; |
---|
164 | end |
---|
165 | |
---|
166 | save(config.files.points, 'Ws','-ASCII') |
---|
167 | save(config.files.Res, 'Res', '-ASCII') |
---|
168 | save(config.files.IdMat, 'IdMat', '-ASCII') |
---|
169 | |
---|
170 | % display the overall statistics |
---|
171 | disp('Overall statistics from im2points: ************************ ') |
---|
172 | disp(sprintf('Total number of frames (possible 3D points): %d',NoPoints)) |
---|
173 | disp(sprintf('Total number of cameras %d', NoCams)) |
---|
174 | disp('More important statistics: ********************************* ') |
---|
175 | disp(sprintf('Detected 3D points: %d', sum(sum(IdMat)>0))) |
---|
176 | disp(sprintf('Detected 3D points in at least 3 cams: %d', sum(sum(IdMat)>2))) |
---|
177 | disp(sprintf('Detected 3D points in ALL cameras: %d', sum(sum(IdMat)==NoCams))) |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|