source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/LearningCode/Features/OldBatchVersion/gen_feature_filt1D_sepH2H4_fixMem.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

File size: 20.0 KB
Line 
1% *  This code was used in the following articles:
2% *  [1] Learning 3-D Scene Structure from a Single Still Image,
3% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
4% *      In ICCV workshop on 3D Representation for Recognition (3dRR-07), 2007.
5% *      (best paper)
6% *  [2] 3-D Reconstruction from Sparse Views using Monocular Vision,
7% *      Ashutosh Saxena, Min Sun, Andrew Y. Ng,
8% *      In ICCV workshop on Virtual Representations and Modeling
9% *      of Large-scale environments (VRML), 2007.
10% *  [3] 3-D Depth Reconstruction from a Single Still Image,
11% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
12% *      International Journal of Computer Vision (IJCV), Aug 2007.
13% *  [6] Learning Depth from Single Monocular Images,
14% *      Ashutosh Saxena, Sung H. Chung, Andrew Y. Ng.
15% *      In Neural Information Processing Systems (NIPS) 18, 2005.
16% *
17% *  These articles are available at:
18% *  http://make3d.stanford.edu/publications
19% *
20% *  We request that you cite the papers [1], [3] and [6] in any of
21% *  your reports that uses this code.
22% *  Further, if you use the code in image3dstiching/ (multiple image version),
23% *  then please cite [2].
24% * 
25% *  If you use the code in third_party/, then PLEASE CITE and follow the
26% *  LICENSE OF THE CORRESPONDING THIRD PARTY CODE.
27% *
28% *  Finally, this code is for non-commercial use only.  For further
29% *  information and to obtain a copy of the license, see
30% *
31% *  http://make3d.stanford.edu/publications/code
32% *
33% *  Also, the software distributed under the License is distributed on an
34% * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
35% *  express or implied.   See the License for the specific language governing
36% *  permissions and limitations under the License.
37% *
38% */
39function []=gen_feature_filt1D_sepH2H4_fixMem(batchNumber,HistFeaType,Absolute)
40
41% This function calculate the feature of each subsuperpixel using texture
42% infomation
43
44% decide the Hist
45if strcmp(HistFeaType,'Whole')
46   Hist = 1;
47   HistFeaType
48else
49   Hist = 0;
50end
51
52if Hist ~= 1 && Absolute ~=1
53        return
54end
55
56if nargin < 1
57        batchNumber = 1;
58elseif nargin < 2
59        Hist = 1;
60        Absolute =1;
61elseif nargin < 3
62        Absolute =1;
63end
64
65global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory...
66    ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default...
67    Horizon_default filename batchSize NuRow_default SegVertYSize SegHoriXSize WeiBatchSize PopUpVertY PopUpHoriX taskName...
68    TrainVerYSize TrainHoriXSize MempryFactor;
69
70
71% load estimated sky
72load([ScratchDataFolder '/data/MaskGSky.mat']); % maskg is the estimated ground maskSky is the estimated sky
73
74% load([ScratchDataFolder '/data/filename.mat']);% load the filename
75% load([ScratchDataFolder '/data/PlaneParameterTure.mat']); % planeParameter
76load([ScratchDataFolder '/data/LowResImgIndexSuperpixelSep.mat']); % superpixel_index
77%load([ScratchDataFolder '/data/MediResImgIndexSuperpixelSep.mat']); % MediResImgIndexSuperpixelSep
78load([ScratchDataFolder '/data/DiffLowResImgIndexSuperpixelSep.mat']); % DiffLowResImgIndexSuperpixelSep
79DiffLowResImgIndexSuperpixelSep = DiffLowResImgIndexSuperpixelSep(:,1);% need only the middle scale segmentation
80
81%load([ScratchDataFolder '/data/FeatureSuperpixel.mat']); %load feature of superpixel
82% load FeaMax to do normalizeing
83load([GeneralDataFolder '/FeaMax.mat']);
84FeaMax = 10.^floor(log10(FeaMax));
85
86% prepare data step
87nu_pics = size(filename,2); % number of pictures
88
89
90% ====================== change able parameter =====================
91%batchSize = 10;% decide the batch size as 10 image pre batch
92% ==================================================================
93
94% for batchImg = 1:batchSize:nu_pics
95batchImg = 1:batchSize:nu_pics;
96
97f = []; % total feature: feature of superpixel followed by texture feature of patch
98    PicsNu = 1
99    for i = batchImg(batchNumber):min(batchImg(batchNumber)+batchSize-1, nu_pics)
100       i
101       % load MediResImgIndexSuperpixelSep
102       load([ScratchDataFolder '/data/MedSeg/MediResImgIndexSuperpixelSep' num2str(i) '.mat']);       
103 
104       % calculate all the features, ray, plane parameter, and row column value
105       img = imread([GeneralDataFolder '/' ImgFolder '/' filename{i} '.jpg']);% read in the hi resolution image of the ith file
106       size(img)
107       % change the resolution in to exactly [TrainVerYSize TrainHoriXSize]
108       
109       if ~all(size(img) == [TrainVerYSize TrainHoriXSize 3])
110           disp('resize to 2272 1704')
111           img = imresize(img,[TrainVerYSize TrainHoriXSize],'bilinear');
112       end
113       % the images resolusion must be bigger then a certain size to have reasonable predicted depth
114%       if prod(size(img))<SegVertYSize*SegHoriXSize*3
115%           img = imresize(img,[SegVertYSize SegHoriXSize],'bilinear');
116       % the images resolusion must be smaller then a certain size to avoid out of memory
117%       elseif prod(size(img)) > TrainVerYSize*TrainHoriXSize*3*(MempryFactor);
118%           disp('origin size')
119%           size(img)
120%           img = imresize(img,[TrainVerYSize TrainHoriXSize],'bilinear');
121%           disp('image too big')
122%           size(img)
123%       end   
124       [vertical_size_hi_res horizontal_size_hi_res t] = size(img); clear t;
125           % get the horizontal(vertical_size_hi_res) and vertical(horizontal_size_hi_res) size of hi resolution image
126       sup_hi_res = imresize(MediResImgIndexSuperpixelSep, ...
127       [vertical_size_hi_res horizontal_size_hi_res],'nearest');% enlarge the low res superpixel into hi res superpixel
128       clear MediResImgIndexSuperpixelSep; 
129 
130       % generate the superpixel in the depth_grid size
131       sup = imresize(LowResImgIndexSuperpixelSep{i,1},[VertYNuDepth HoriXNuDepth],'nearest');       
132       
133       % load picsinfo just for the horizontal value
134        PicsinfoName = strrep(filename{i},'img','picsinfo');
135        temp = dir([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']);
136        if size(temp,1) == 0
137            a = a_default;
138            b = b_default;
139            Ox = Ox_default;
140            Oy = Oy_default;
141            Horizon = Horizon_default;
142        else   
143            load([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']);
144        end
145       
146       % calculate the ray
147       RayCenter = GenerateRay(HoriXNuDepth,VertYNuDepth,'center',a,b,Ox,Oy); %[ horiXSizeLowREs VertYSizeLowREs 3]
148       
149       % calculate how many mask we need
150       NuMask = ceil([HoriXNuDepth/HoriXNuPatch; VertYNuDepth/VertYNuPatch])
151
152       % Grid Info
153       gridinfo = [horizontal_size_hi_res HoriXNuDepth HoriXNuPatch;    vertical_size_hi_res VertYNuDepth VertYNuPatch];
154       ratio(1:2) = gridinfo(:,1)./gridinfo(:,end);
155       ratio(3:4) = gridinfo(:,1)./gridinfo(:,2);
156
157       % calcuate the position of the mask
158       hight(1) = round((floor(ratio(2))-1)/2);
159       hight(2) = floor(ratio(2)) - 1 - hight(1);
160       width(1) = round((floor(ratio(1))-1)/2);
161       width(2) = floor(ratio(1)) - 1 - width(1);
162
163       %big_sup_depthmap_res = imresize(LowResImgIndexSuperpixelSep{i,2}, ...
164        %               [VertYNuDepth HoriXNuDepth],'nearest');% enlarge the low res superpixel into hi res superpixel
165       f_pics = [];
166       tic
167       [H2] = calculateFilterBanks_old(img); % (hard work 1min) use Ashutaosh's code
168       H2 = H2.^2;
169%        NuFeaH2H4 = 2*size(H2,3);
170       hcol = ones(floor(ratio(2)),1);
171       hrow = ones(1,floor(ratio(1)));
172       hrect = ones(floor(ratio(2)),floor(ratio(1)));
173size(hcol)
174size(hrow)
175size(hrect)
176
177if Absolute == 1
178       % highest resolution
179       row_start = 1;
180%        MinTest = zeros(55*305,1);
181       for j = 1:NuMask(1)
182           for k = 1:NuMask(2);
183               f_pics_mask = [];
184               % first generate the mask respect to the dominate
185               % subsuperpixel
186               [mask,SupIndex,PixelMask,PatchMask] = makeSubSupMask(gridinfo,sup_hi_res,sup,[j; k],width,hight);
187               % calculaing the normalize value
188                  NormalizeValue = conv2(hcol,hrow,mask,'same');
189
190               % 1) zeroth feature the superpixel index to keep a record
191               f_pics_mask = [f_pics_mask SupIndex];
192               clear SupIndex;
193
194               % 2) generate the 1:34 features for H2  for 1 center and 4 neighbor (left right top bottom)
195               fInd=2;
196               for m = 1:17
197                   %temp1 = conv2(hcol,hrow,H2(:,:,m).*(mask),'same');
198                   temp = conv2(H2(:,:,m).*(mask),hrect,'same');
199                   size(temp)
200                   f_pics_mask = [f_pics_mask temp(PixelMask)./NormalizeValue(PixelMask)...
201                                  ./FeaMax(1,fInd)];
202                   fInd = fInd+1;
203               end   
204
205               f_pics(PatchMask, row_start:row_start+size(f_pics_mask,2)-1) = f_pics_mask;
206               
207           end
208       end
209
210       H2 = H2.^2; % This H2 is H4
211       row_start = row_start+size(f_pics_mask,2);
212
213        % RAJIV MIN -- ERROR ------- this part which sums up the filter response in a superpixel, should
214        % be done using integral images, prefably in C++ code.
215
216       for j = 1:NuMask(1)
217           for k = 1:NuMask(2);
218               f_pics_mask = [];
219               % first generate the mask respect to the dominate
220               % subsuperpixel
221               [mask,SupIndex,PixelMask,PatchMask] = makeSubSupMask(gridinfo,sup_hi_res,sup,[j; k],width,hight);
222               % calculaing the normalize value
223                  NormalizeValue = conv2(hcol,hrow,mask,'same');
224
225               % 1) zeroth feature the superpixel index to keep a record
226%               f_pics_mask = [f_pics_mask SupIndex];
227               clear SupIndex;
228
229               % 2) generate the 1:34 features for H4 for 1 center and 4 neighbor (left right top bottom)
230               fInd=19;
231               for m = 1:17
232                   %temp = conv2(hcol,hrow,H2(:,:,m).*(mask),'same');
233                   temp = conv2(H2(:,:,m).*(mask),hrect,'same');
234                   f_pics_mask = [f_pics_mask temp(PixelMask)./NormalizeValue(PixelMask)...
235                                 ./FeaMax(1,fInd)];
236                   fInd = fInd+1;
237               end
238
239               f_pics(PatchMask, row_start:row_start+size(f_pics_mask,2)-1) = f_pics_mask;
240               
241           end
242       end
243end
244
245if Hist == 1
246% RAJIV ---- ERROR -- should be disabled.
247H2 = H2.^(0.5);
248% =============calculate the histagram of the features for relative depth estimation================
249        disp('cal_relative')
250        [relativeFeatureVector] = makeRelativeFeatureVector(H2,1);
251% ==================================================================================================
252end
253       clear H2;
254
255       % 1/3 resolution
256       feaScale1 = [];
257       MedResY = round(gridinfo(2,1)/3);     
258       MedResX = round(gridinfo(1,1)/3);
259       DepthGridSizeY = MedResY/VertYNuDepth;     
260       DepthGridSizeX= MedResX/HoriXNuDepth;     
261       imgMedRes = imresize(img,[MedResY MedResX],'nearest');
262       clear img;
263       [H2] = calculateFilterBanks_old(imgMedRes); % (hard work 1min) use Ashutaosh's code
264       H2 = H2.^2;
265       H4 = H2.^2;
266if Absolute == 1
267       row_start = size(f_pics,2)+1;
268       % calculating number of mask
269       NormalizeValue = conv2(hcol,hrow,ones(MedResY,MedResX),'same');
270       NuMask = ceil([HoriXNuDepth/HoriXNuPatch*3; VertYNuDepth/VertYNuPatch*3]);
271             
272       % 1) generating the PixelMask
273       PixelMask = logical(zeros(MedResY,MedResX));
274       [X Y] = meshgrid(ceil((1/2)*DepthGridSizeX:DepthGridSizeX:MedResX),...
275                        ceil((1/2)*DepthGridSizeY:DepthGridSizeY:MedResY));
276       PixelMask = sub2ind(size(PixelMask),Y(:),X(:));
277       for m = 1:17
278           temp = conv2(hcol,hrow,H2(:,:,m),'same');% 102.533776 seconds
279           feaScale1(:,m) = temp(PixelMask)./NormalizeValue(PixelMask)...
280               ./FeaMax(1,fInd);
281           fInd = fInd +1;
282       end   
283               
284       for m = 1:17
285           temp = conv2(hcol,hrow,H4(:,:,m),'same');% 102.533776 seconds
286           feaScale1(:,m+17) = temp(PixelMask)./NormalizeValue(PixelMask)...
287               ./FeaMax(1,fInd);
288           fInd = fInd +1;
289       end
290
291%       shift = [0 0; -1 0; 1 0; 0 -1; 0 1].*repmat(NuMask',[5 1]);
292%       for l = 1:5
293%           [Ix Iy] = meshgrid(max(min(2+shift(l,1):HoriXNuDepth+2-1+shift(l,1),HoriXNuDepth+2),1),...
294%                     max(min(2+shift(l,2):VertYNuDepth+2-1+shift(l,2),VertYNuDepth+2),1));
295%           maskNeibor = sub2ind([VertYNuDepth+2, HoriXNuDepth+2], Iy(:), Ix(:));
296%           f_pics = [f_pics  feaScale1(maskNeibor,:)];
297%       end
298       
299       size(f_pics)
300       size(feaScale1)   
301       f_pics = [f_pics  feaScale1];
302end
303clear H4;
304
305if Hist == 1
306% =============calculate the histagram of the features for relative depth estimation================
307        [relativeFeatureVector] = cat(3,relativeFeatureVector,makeRelativeFeatureVector(H2,2));
308% ==================================================================================================
309end
310
311       clear H2;
312
313       % 1/9 resolution
314       feaScale1 = [];
315       LowResY = round(gridinfo(2,1)/9);     
316       LowResX = round(gridinfo(1,1)/9);     
317       DepthGridSizeY = LowResY/VertYNuDepth;     
318       DepthGridSizeX= LowResX/HoriXNuDepth;     
319       imgLowRes = imresize(imgMedRes,[LowResY LowResX],'nearest');
320       clear imgMedRes;
321       [H2] = calculateFilterBanks_old(imgLowRes); % (hard work 1min) use Ashutaosh's code
322       H2 = H2.^2;
323       H4 = H2.^2;
324
325if Absolute == 1
326       row_start = size(f_pics,2)+1;
327       % calculating number of mask
328       NormalizeValue = conv2(hcol,hrow,ones(LowResY,LowResX),'same');
329       NuMask = ceil([HoriXNuDepth/HoriXNuPatch*9; VertYNuDepth/VertYNuPatch*9]);
330               
331       % 1) generating the PixelMask
332       PixelMask = logical(zeros(LowResY,LowResX));
333       [X Y] = meshgrid(ceil((1/2)*DepthGridSizeX:DepthGridSizeX:LowResX),...
334                        ceil((1/2)*DepthGridSizeY:DepthGridSizeY:LowResY));
335       PixelMask = sub2ind(size(PixelMask),Y(:),X(:));
336%       size(maskInd)
337%       PixelMask(maskInd) = true;
338       %PixelMask = logical(zeros(LowResY,LowResX));
339       %PixelMask(ceil((1/2)*DepthGridSizeY:DepthGridSizeY:LowResY),...
340       %          ceil((1/2)*DepthGridSizeX:DepthGridSizeX:LowResX)) = true;
341%       valuemask = sum(PixelMask,1)>0;
342%       if all(PixelMask(1,valuemask) == true)
343%          PixelMask(2,valuemask) = true;
344%       else
345%          PixelMask(1,valuemask) = true;
346%       end   
347%       if all(PixelMask(end,valuemask) == true)
348%          PixelMask(end-1,valuemask) = true;
349%       else
350%          PixelMask(end,valuemask) = true;
351%       end   
352%          valuemask = sum(PixelMask,2)>0;
353%       if all(PixelMask(valuemask,1) == true)
354%          PixelMask(valuemask,2) = true;
355%       else
356%          PixelMask(valuemask,1) = true;
357%       end   
358%       if all(PixelMask(valuemask,end) == true);
359%          PixelMask(valuemask,end-1) = true;
360%       else
361%          PixelMask(valuemask,end) = true;
362%       end               
363               
364       % 2) generate the 1:34 features for H2 and H4 for 1 center and 4 neighbor (left right top bottom)
365       for m = 1:17
366           temp = conv2(hcol,hrow,H2(:,:,m),'same');% 102.533776 seconds
367           feaScale1(:,m) = temp(PixelMask)./NormalizeValue(PixelMask)...
368                   ./FeaMax(1,fInd);
369           fInd = fInd +1;
370       end   
371               
372       for m = 1:17
373           temp = conv2(hcol,hrow,H4(:,:,m),'same');% 102.533776 seconds
374           feaScale1(:,m+17) = temp(PixelMask)./NormalizeValue(PixelMask)...
375                   ./FeaMax(1,fInd);
376           fInd = fInd +1;
377       end
378
379%       shift = [0 0; -1 0; 1 0; 0 -1; 0 1].*repmat(NuMask',[5 1]);
380%       for l = 1:5
381%           [Ix Iy] = meshgrid(max(min(2+shift(l,1):HoriXNuDepth+2-1+shift(l,1),HoriXNuDepth+2),1),...
382%%                     max(min(2+shift(l,2):VertYNuDepth+2-1+shift(l,2),VertYNuDepth+2),1));
383%           maskNeibor = sub2ind([VertYNuDepth+2, HoriXNuDepth+2], Iy(:), Ix(:));
384%           f_pics = [f_pics  feaScale1(maskNeibor,:)];
385%       end
386       f_pics = [f_pics  feaScale1];
387end         
388clear H4;
389
390if Hist ==1
391% =============calculate the histagram of the features for relative depth estimation================
392        [relativeFeatureVector] = cat(3,relativeFeatureVector,makeRelativeFeatureVector(H2,5));
393        RFVector{PicsNu} = relativeFeatureVector;
394        clear relativeFeatureVector;
395        DateStamp = date;
396        save([ScratchDataFolder '/data/feature_Hist_Whole' num2str(batchNumber) '_' DateStamp  '.mat'],'RFVector');
397% ==================================================================================================
398end
399
400       clear H2;
401 
402if Absolute == 1
403       % superpixel features
404       %fsup = FeatureSuperpixel{i};
405       %f_pics = [f_pics fsup(:,f_pics(:,1))'];
406       % other features without relation with neiborfeatures
407       % 1) the closest ground position to the (i, j) patch
408         % How can we remove DiffLowResImgIndexSuperpixelSep since generate another superpixel takes time
409         big_sup = imresize(DiffLowResImgIndexSuperpixelSep{i,1},[VertYNuDepth HoriXNuDepth],'nearest');
410         GroundSupIndex = unique(big_sup(maskg{i}));
411
412%         NonGround = [];
413%       for j = 1:HoriXNuDepth
414%           Ground(j) = analysesupinpatch(big_sup(round(gridinfo(2,2)*(1-Horizon)):end,j));
415%            NonGround(j) = analysesupinpatch(big_sup(round(1:(gridinfo(2,2)*(1-Horizon)-1)),j));
416%           NonGround = [NonGround (unique(big_sup(round(1:(gridinfo(2,2)*(1-Horizon)-1)),j)))'];
417%       end
418%       Ground = setdiff(Ground,NonGround);
419       for j = 1:HoriXNuDepth
420           Gmask = logical(zeros(size( big_sup(:,j))));
421           for k=GroundSupIndex'
422               Gmask(big_sup(:,j) == k) = true;
423           end
424           [rowSub colSub ] = find(Gmask);
425           minRow = min(rowSub);
426%           if size(minRow,1) == 0
427%               Gmask = logical(zeros(size( big_sup(:,j))));
428%               MaybeGround = unique(big_sup(round(gridinfo(2,2)*(1-Horizon)):end,j));
429%               MaybeGround = setdiff(MaybeGround,NonGround);
430%               for k=MaybeGround
431%                   Gmask(big_sup(:,j) == k) = true;
432%               end
433%               [rowSub colSub ] = find(Gmask);
434%               minRow = min(rowSub);
435%               if size(minRow,1) == 0
436%                  lastguessGround = analysesupinpatch(big_sup(round(gridinfo(2,2)*(1-Horizon)):end,j));
437%                  [rowSub colSub ] = find(big_sup(round(gridinfo(2,2)*(1-Horizon)):end,j)==lastguessGround);
438%                  minRow = min(rowSub);
439%               end
440%           end   
441           %minRow = max(minRow,round(gridinfo(2,2)*(1-Horizon)));
442
443           GroundVertEdge(:,j) = (1-(VertYNuDepth-1)/VertYNuDepth)./RayCenter(:,j,3);
444           if size(minRow,1) ~= 0
445                GroundVertEdge((minRow+1):VertYNuDepth,j) = (1-((minRow+1):VertYNuDepth)'/VertYNuDepth)./RayCenter((minRow+1):VertYNuDepth,j,3);
446                GroundVertEdge(1:(minRow),j) = (1- minRow/VertYNuDepth)./RayCenter(1:(minRow),j,3);
447           end
448       end
449       GroundVertEdgePics = GroundVertEdge;
450
451
452       %f_pics(:,end+1) = max([GroundVertEdgePics(:) repmat((1:VertYNuDepth)',[HoriXNuDepth 1])],[],2);
453       %f_pics(:,end+1) = Gr undVertEdgePics(:);%./reshape(RayCenter(:,:,3),[],1);
454       f_pics(:,end+1) = min([GroundVertEdgePics(:)...
455                repmat((VertYNuDepth:-1:1)'/VertYNuDepth,[HoriXNuDepth 1])./reshape(RayCenter(:,:,3),[],1)],[],2);   
456       
457       disp(['Image Number ' num2str(i)]);
458       f{PicsNu} = f_pics;
459       % Absolute Features are index by Type BatchNu Data
460       DateStamp = date;
461       save([ScratchDataFolder '/data/feature_Abs_Whole' num2str(batchNumber) '_' DateStamp  '.mat'],'f');
462
463toc;       
464%return;
465end
466       PicsNu = PicsNu + 1
467       batchNumber
468    end
469    % Absolute Features are index by Type BatchNu Data
470    DateStamp = date;
471    if Absolute == 1
472    save([ScratchDataFolder '/data/feature_Abs_Whole' num2str(batchNumber) '_' DateStamp  '.mat'],'f');
473    end
474    % Hist Features are index by Type BatchNu Data
475    if Hist == 1
476       save([ScratchDataFolder '/data/feature_Hist_Whole' num2str(batchNumber) '_' DateStamp  '.mat'],'RFVector');
477    end
478return;
Note: See TracBrowser for help on using the repository browser.