source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/LearningCode/Inference/OldVersion/PredictDepthMRF.m

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

Added original make3d

File size: 27.7 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 []=PredictDepthMRF(DepthDirectory,logScale,SkyExclude,Lazer,BatchNu,step)
40%this function generate the predicted plane
41
42
43DepthDirectory
44%return;
45% define global variable
46global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory...
47    ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default...
48    Horizon_default filename batchSize NuRow_default SegVertYSize SegHoriXSize WeiBatchSize;
49
50% initialize parameter
51NuPatch = VertYNuDepth*HoriXNuDepth;
52
53% load data
54load([ScratchDataFolder '/data/LowResImgIndexSuperpixelSep.mat']); % load LowResImgIndexSuperpixelSep
55load([ScratchDataFolder '/data/DiffLowResImgIndexSuperpixelSep.mat']); % load DiffLowResImgIndexSuperpixelSep(medi$large)
56load([ScratchDataFolder '/data/TextLowResImgIndexSuperpixelSep.mat']); % load TextLowResImgIndexSuperpixelSep using Textrea
57load([ScratchDataFolder '/data/MaskGSky.mat']); % load maskg maskSky from CMU's output
58
59% prepare to store the predictedM
60%[ScratchDataFolder '/_predicted_' DepthDirectory]
61%system(['mkdir ' ScratchDataFolder '/_predicted_' DepthDirectory]);
62% set parameter
63ZTiltFactor = 1; % both can be estimated after group fit that estimate the Norm_floor
64YTiltFactor = 1;
65
66% initial parameter
67BatchSize = 5;
68NuPics = size(filename,2);
69BatchRow = 1:BatchSize:NuPics;
70%for i = 10% : NuPics
71BatchRow(BatchNu):min(BatchRow(BatchNu)+BatchSize-1,NuPics)
72for i = BatchRow(BatchNu):min(BatchRow(BatchNu)+BatchSize-1,NuPics)
73        i
74%return;
75        % load picsinfo just for the horizontal value
76        PicsinfoName = strrep(filename{i},'img','picsinfo');
77        temp = dir([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']);
78        if size(temp,1) == 0
79            a = a_default;
80            b = b_default;
81            Ox = Ox_default;
82            Oy = Oy_default;
83            Horizon = Horizon_default;
84        else   
85            load([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']);
86        end
87 
88        % load depthMap
89        depthfile = strrep(filename{i},'img','depth_learned'); % the depth filename
90        load([ScratchDataFolder '/' DepthDirectory '/' depthfile '.mat']);
91%        if logScale == 1
92%            if SkyExclude ==1
93%                load([ScratchDataFolder '/_LearnDLogScaleNonsky_' DepthDirectory '/' depthfile '.mat']);
94%            else
95%                load([ScratchDataFolder '/_LearnDLogScale_' DepthDirectory '/' depthfile '.mat']);
96%            end
97%        else
98%            if SkyExclude ==1
99%                load([ScratchDataFolder '/_LearnD_' DepthDirectory '/' depthfile '.mat']);
100%            else
101%                load([ScratchDataFolder '/_LearnD_' DepthDirectory '/' depthfile '.mat']);
102%            end
103%        end
104%        depthMap
105%return;           
106        LearnedDepth = depthMap; clear depthMap;
107
108        % load MediResImgIndexSuperpixelSep
109        load([ScratchDataFolder '/data/MedSeg/MediResImgIndexSuperpixelSep' num2str(i) '.mat']);
110        MedSup = MediResImgIndexSuperpixelSep;
111
112        % load Lazer depthMap
113        if Lazer == 1
114           depthfile = strrep(filename{i},'img','depth_sph_corr'); % the depth filename
115           load([ScratchDataFolder '/Gridlaserdata/' depthfile '.mat']);
116           LaserDepth = Position3DGrid(:,:,4);
117           clear Position3DGrid;
118        end     
119
120if false
121        % generate straight line for straight line prior
122        Img = imread([GeneralDataFolder '/' ImgFolder '/' filename{i} '.jpg'],'jpg');
123        if  prod(size(Img))> SegVertYSize*SegHoriXSize*3
124            Img = imresize(Img,[SegVertYSize SegHoriXSize],'nearest');
125        end
126        [ImgYSize ImgXSize dummy] = size(Img);
127        [lineSegList] = edgeSegDetection(Img,i,0);
128
129% =================================================================================
130
131        NuSeg = size(lineSegList,1);
132%================================This transform might cause the offset===================================
133        % Normalize the lineSegList to the size of VertYNuDepth HoriXNuDepth (watch out for the +- 0.5 offset)
134        lineSegList = ((lineSegList+0.5)./repmat([ImgXSize ImgYSize ImgXSize ImgYSize],NuSeg,1)...
135                      .*repmat([HoriXNuDepth VertYNuDepth HoriXNuDepth VertYNuDepth],NuSeg,1))-0.5;
136%========================================================================================================
137
138        % take out the vertical edge so that we may combine it with 1stSmooth in Y direction
139        % < 0.9 like to deal with VertLine cause VerLine don't stick
140        temp = (abs(round(lineSegList(:,1))-round(lineSegList(:,3)))./abs(round(lineSegList(:,2))-round(lineSegList(:,4))));
141        VertLineSegList = lineSegList( temp < 0.9  ,:);% Verti Straight Line
142        lineSegList = lineSegList( temp >= 0.9  ,:);% NonVerti Straight Line
143       
144        % Start processing the Line
145        MovedPatchBook = []; % keep the record of Patch been moved (a patch can ouly be moved once)
146        RayPorjectImgMapY = repmat((1:VertYNuDepth)',[1 HoriXNuDepth]); % keep the new Ray in Y position
147        RayPorjectImgMapX = repmat((1:HoriXNuDepth),[VertYNuDepth 1]);
148        VBrokeBook = sparse(VertYNuDepth,HoriXNuDepth);
149        HBrokeBook = sparse(VertYNuDepth,HoriXNuDepth);
150        RayPorjectImgMapY = repmat((1:VertYNuDepth)',[1 HoriXNuDepth]); % keep the new Ray in Y position
151        RayPorjectImgMapX = repmat((1:HoriXNuDepth),[VertYNuDepth 1]);        %First process the nonVerti
152        [StraightLinePriorSelectMatrix RayPorjectImgMapY RayPorjectImgMapX MovedPatchBook LineIndex HBrokeBook VBrokeBook] = ...
153         StraightLinePrior(i,Img,lineSegList,LearnedDepth,RayPorjectImgMapY,RayPorjectImgMapX,MovedPatchBook,HBrokeBook,VBrokeBook,0,a,b,Ox,Oy);
154        %process the Verti
155        % first find out VertWall and VertiGround
156        disp('Verti')
157        [StraightLinePriorSelectMatrixVert RayPorjectImgMapY RayPorjectImgMapX MovedPatchBook LineIndexVert HBrokeBook ...
158         VBrokeBook] = StraightLinePrior(i,Img,VertLineSegList,LearnedDepth,RayPorjectImgMapY,RayPorjectImgMapX,...
159         MovedPatchBook,HBrokeBook,VBrokeBook,1,a,b,Ox,Oy);
160end
161%        VertLineSegList = round(VertLineSegList);
162%        NuVertSeg = size(VertLineSegList,1);
163%        VertLineMask = zeros(VertYNuDepth, HoriXNuDepth);
164%        for k = 1:NuVertSeg
165%            if VertLineSegList(k,2) < VertLineSegList(k,4)
166%               VertLineMask(max(VertLineSegList(k,2),1):min((VertLineSegList(k,4)-1),VertYNuDepth),...
167%                            min(max(VertLineSegList(k,1),1),HoriXNuDepth)) = 1;
168%            else
169%               VertLineMask(max(VertLineSegList(k,4),1):min((VertLineSegList(k,2)-1),VertYNuDepth),...
170%                            min(max(VertLineSegList(k,1),1),HoriXNuDepth)) = 1;
171%            end
172%        end
173         
174%         
175        % generate ray porjection position in the image plane
176        RayPorjectImgMapY = repmat((1:VertYNuDepth)',[1 HoriXNuDepth]); % keep the new Ray in Y position
177        RayPorjectImgMapX = repmat((1:HoriXNuDepth),[VertYNuDepth 1]);        %First process the nonVerti
178        RayPorjectImgMapY = ((VertYNuDepth+1-RayPorjectImgMapY)-0.5)/VertYNuDepth - Oy;
179        RayPorjectImgMapX = (RayPorjectImgMapX-0.5)/HoriXNuDepth - Ox;
180        % generate specific ray for whole pics
181        RayCenter = RayImPosition(RayPorjectImgMapY,RayPorjectImgMapX,a,b,Ox,Oy); %[ horiXSizeLowREs VertYSizeLowREs 3]
182        Rx = RayCenter(:,:,1);
183        Rx = Rx(:);
184        Ry = RayCenter(:,:,2);
185        Ry = Ry(:);
186        Rz = RayCenter(:,:,3);
187        Rz = Rz(:);
188
189%=====================================================       
190        % MRF reshape the 3d cloud
191
192%============================
193        % setting the estimated Ground Verticl Sky segmentation (generated from CMU's code)
194        maskEstGVS = 2*ones(VertYNuDepth,HoriXNuDepth);
195        maskEstGVS(maskg{i}) = 1;
196        maskEstGVS(maskSky{i}) = 3;
197        %GSize = sum(maskg{i});
198        %SkySize = sum(maskSky{i});
199%============================
200
201        NuSupType = size(LowResImgIndexSuperpixelSep,2)+size(DiffLowResImgIndexSuperpixelSep,2)...
202                   +size(TextLowResImgIndexSuperpixelSep,2)*size(TextLowResImgIndexSuperpixelSep,3);
203
204        for j = 1:NuSupType  % total 21 seg : 3 RGB 6*3= 18 texture filters
205
206            % pick the specific segmentation
207            if j==1
208                sup = LowResImgIndexSuperpixelSep{i,1};
209            elseif j<=3
210                sup = DiffLowResImgIndexSuperpixelSep{i,j-1};
211            else
212                Subm = mod((j-3),6);
213                if Subm==0
214                   Subm=6;
215                end
216                sup = TextLowResImgIndexSuperpixelSep{i,Subm,ceil((j-3)/6)};
217            end
218
219            % Exclude the sky first
220            sup(maskEstGVS == 3) = 0;
221            % extend the estimated maskGVS to the new segmentation
222            NewSupInd = setdiff((unique(sup))',0);
223            NewEstGSup = zeros(VertYNuDepth,HoriXNuDepth);
224            NewEstVSup = zeros(VertYNuDepth,HoriXNuDepth);
225            for m = NewSupInd
226                mask = sup == m;
227                if any(maskEstGVS(mask)==1)
228                    GVSInd =1;
229                else
230                    GVSInd =2;
231                end
232                %GVSInd = analysesupinpatch(maskEstGVS(mask));
233                if GVSInd == 1
234                   NewEstGSup(mask) = m;
235                   NewEstVSup(mask) = 0;
236                elseif GVSInd == 2
237                   NewEstVSup(mask) = m; 
238                   NewEstGSup(mask) = -1; 
239                end
240            end
241            %if j == 2
242            %   SpreadFactor = gen_SFactor(LearnedDepth,sup,Rz);
243            %end
244            %clear LowResImgIndexSuperpixelSep;
245           
246            % 2nd order smooth
247            [SecXG(j,:) SecYG(j,:)]= gen_2ndSmooth(NewEstGSup);
248            [SecXV(j,:) SecYV(j,:)]= gen_2ndSmooth(NewEstVSup);
249
250            % 1st order smooth
251            [FirstYG(j,:) FirstXG(j,:)] = gen_1stSmooth(NewEstGSup);
252            [FirstYV(j,:) FirstXV(j,:)] = gen_1stSmooth(NewEstVSup);
253                %[GPy{j} ] = gen_GravityP_vertical(maskV);
254            %[PlanePriorX PlanePriorY]= gen_PlanePrior(LowResImgIndexSuperpixelSep{i,1});
255        end
256 
257        % set weight for different segmentation
258        small=10; med=5; large=1;
259%        small=5; med=2.5; large=0.5;
260        temp =[small; med; large; small*ones(6,1); med*ones(6,1) ;large*ones(6,1)];
261        Wei2ndSmoothGX = temp;
262        Wei2ndSmoothGY = temp;
263        Wei2ndSmoothVX = temp;
264        Wei2ndSmoothVY = temp;
265%        Wei1stSmoothGX = temp*10;
266%        Wei1stSmoothGY = temp*10; % group can have slope
267        Wei1stSmoothVX = temp; % vertical wall can have orientation
268        Wei1stSmoothGX = temp;
269        Wei1stSmoothGY = temp; % group can have slope
270        Wei1stSmoothVY = temp;
271       
272        % set weight for the VertLineSeg Ground and VertialWalls
273%        WeiLinSegVert = 11;
274
275        % set weight for the straight line prior
276%        WeiStraightLine = 1120;
277
278        % generate the smooth matrix
279        M2ndSmoothY = spdiags([ones(NuPatch,1) -2*ones(NuPatch,1) ones(NuPatch,1)],[-1 0 1],NuPatch,NuPatch);
280        M2ndSmoothX = spdiags([ones(NuPatch,1) -2*ones(NuPatch,1) ones(NuPatch,1)],...
281                              [-VertYNuDepth 0 VertYNuDepth],NuPatch,NuPatch);
282        M1stSmoothY = spdiags([ones(NuPatch,1) -ones(NuPatch,1)],[0 1],NuPatch,NuPatch);
283        M1stSmoothX = spdiags([ones(NuPatch,1) -ones(NuPatch,1)],[0 VertYNuDepth],NuPatch,NuPatch);
284
285        % generate beta
286%        beta2ndSmoothGX = Wei2ndSmoothGX'*ones(21,1) * logisticResponse(Wei2ndSmoothGX'*SecXG / (Wei2ndSmoothGX'*ones(21,1)) );
287        beta2ndSmoothGX = Wei2ndSmoothGX'*SecXG;
288%        beta2ndSmoothGX([cell2mat(LineIndex(:)') cell2mat(LineIndexVert(:)')])=0;
289        beta2ndSmoothGY = Wei2ndSmoothGY'*SecYG;
290%        beta2ndSmoothGY([cell2mat(LineIndex(:)') cell2mat(LineIndexVert(:)')])=0;
291%        beta2ndSmoothGY = Wei2ndSmoothGY'*ones(21,1) * logisticResponse(Wei2ndSmoothGY'*SecYG / (Wei2ndSmoothGY'*ones(21,1)) );
292%        beta2ndSmoothVX = Wei2ndSmoothVX'*ones(21,1) * logisticResponse(Wei2ndSmoothVX'*SecXV / (Wei2ndSmoothVX'*ones(21,1)) );
293        beta2ndSmoothVX = Wei2ndSmoothVX'*SecXV;
294%        beta2ndSmoothVX([cell2mat(LineIndex(:)') cell2mat(LineIndexVert(:)')])=0;
295%        beta2ndSmoothVY = Wei2ndSmoothVY'*ones(21,1) * logisticResponse(Wei2ndSmoothVY'*SecYV / (Wei2ndSmoothVY'*ones(21,1)) );
296        beta2ndSmoothVY = Wei2ndSmoothVY'*SecYV;
297%        beta2ndSmoothVY([cell2mat(LineIndex(:)') cell2mat(LineIndexVert(:)')])=0;
298%        beta1stSmoothGY = Wei1stSmoothGY'*ones(21,1)*logisticResponse(Wei1stSmoothGY'*FirstYG/(Wei1stSmoothGY'*ones(21,1)) );
299        beta1stSmoothGY = Wei1stSmoothGY'*FirstYG;
300%        beta1stSmoothGY([cell2mat(LineIndex(:,1)') cell2mat(LineIndexVert(:,1)')])=0;
301%        beta1stSmoothVY = Wei1stSmoothVY'*ones(21,1)*logisticResponse(Wei1stSmoothVY'*FirstYV/(Wei1stSmoothVY'*ones(21,1)) );
302        beta1stSmoothVY = Wei1stSmoothVY'*FirstYV;
303%        beta1stSmoothVY([cell2mat(LineIndex(:,1)') cell2mat(LineIndexVert(:,1)')])=0;
304%        beta1stSmoothSY = Wei1stSmoothSY'*ones(21,1)*logisticResponse(Wei1stSmoothSY'*FirstYS/(Wei1stSmoothSY'*ones(21,1)) );
305%        beta1stSmoothGX = Wei1stSmoothGX'*ones(21,1)*logisticResponse(Wei1stSmoothGX'*FirstXG/(Wei1stSmoothGX'*ones(21,1)) );
306        beta1stSmoothGX = Wei1stSmoothGX'*FirstXG;
307%        beta1stSmoothGX([cell2mat(LineIndex(:,3)') cell2mat(LineIndexVert(:,3)')])=0;
308%        beta1stSmoothVX = Wei1stSmoothVX'*ones(21,1)*logisticResponse(Wei1stSmoothVX'*FirstXV/(Wei1stSmoothVX'*ones(21,1)) );
309        beta1stSmoothVX = Wei1stSmoothVX'*FirstXV;
310%        beta1stSmoothVX([cell2mat(LineIndex(:,3)') cell2mat(LineIndexVert(:,3)')])=0;
311%        beta1stSmoothSX = Wei1stSmoothSX'*ones(21,1)*logisticResponse(Wei1stSmoothSX'*FirstXS/(Wei1stSmoothSX'*ones(21,1)) );
312       
313        % generate Q
314        tempMask = beta2ndSmoothGX == 0;
315        NuElement = sum(~tempMask);
316        Q2ndXy = spdiags((beta2ndSmoothGX(~tempMask))'...
317                 ,0,NuElement,NuElement)*M2ndSmoothX(~tempMask,:)*spdiags(Ry,0,NuPatch,NuPatch);
318
319        tempMask = beta2ndSmoothVX == 0;
320        NuElement = sum(~tempMask);
321        Q2ndXz = spdiags((beta2ndSmoothVX(~tempMask))'...
322                 ,0,NuElement,NuElement)*M2ndSmoothX(~tempMask,:)*spdiags(Rz,0,NuPatch,NuPatch); 
323
324        tempMask = beta2ndSmoothGY == 0;
325        NuElement = sum(~tempMask);
326        Q2ndYy = spdiags((beta2ndSmoothGY(~tempMask))'...
327                 ,0,NuElement,NuElement)*M2ndSmoothY(~tempMask,:)*spdiags(Ry,0,NuPatch,NuPatch);
328
329        tempMask = beta2ndSmoothVY == 0;
330        NuElement = sum(~tempMask);
331        Q2ndYz = spdiags((beta2ndSmoothVY(~tempMask))'...
332                 ,0,NuElement,NuElement)*M2ndSmoothY(~tempMask,:)*spdiags(Rz,0,NuPatch,NuPatch);
333
334%        Q2ndLine = WeiStraightLine*StraightLinePriorSelectMatrix;
335
336%        Q2ndVertLine = WeiLinSegVert*StraightLinePriorSelectMatrixVert;
337         
338        tempMask = beta1stSmoothVY == 0;
339        NuElement = sum(~tempMask);
340        Q1stYz = spdiags((beta1stSmoothVY(~tempMask))'...
341                 ,0,NuElement,NuElement)*M1stSmoothY(~tempMask,:)*spdiags(Rz*ZTiltFactor,0,NuPatch,NuPatch);
342
343        tempMask = beta1stSmoothVX == 0;
344        NuElement = sum(~tempMask);
345        Q1stXz = spdiags((beta1stSmoothVX(~tempMask))'...
346                 ,0,NuElement,NuElement)*M1stSmoothX(~tempMask,:)*spdiags(Rz*ZTiltFactor,0,NuPatch,NuPatch);
347
348        tempMask = beta1stSmoothGY == 0;
349        NuElement = sum(~tempMask);
350        Q1stYy = spdiags((beta1stSmoothGY(~tempMask))'...
351                 ,0,NuElement,NuElement)*M1stSmoothY(~tempMask,:)*spdiags(Ry*YTiltFactor,0,NuPatch,NuPatch);
352
353        tempMask = beta1stSmoothGX == 0;
354        NuElement = sum(~tempMask);
355        Q1stXy = spdiags((beta1stSmoothGX(~tempMask))'...
356                 ,0,NuElement,NuElement)*M1stSmoothX(~tempMask,:)*spdiags(Ry*YTiltFactor,0,NuPatch,NuPatch);
357% ============================START MRF OPTMIZATIOM=========================================================
358         %=================
359% lineprog ===========================================
360%         B = [DMatrixY;spdiags(maskO(:),[0],NuPatch,NuPatch)];
361%         Q = [Q2ndXz;Q2ndYz];
362%         A = [B -speye(size(B,1)) sparse(size(B,1),size(Q,1));...
363%             -B -speye(size(B,1)) sparse(size(B,1),size(Q,1));...
364%              Q sparse(size(Q,1),size(B,1)) -speye(size(Q,1));...
365%             -Q sparse(size(Q,1),size(B,1)) -speye(size(Q,1));...
366%             -speye(NuPatch) sparse(NuPatch,size(B,1)+size(Q,1))];
367%         bb = [B*LearnedDepth(:); - B*LearnedDepth(:);...
368%               sparse(size(Q,1)*2,1); -5*ones(NuPatch,1)];
369%         f = [sparse(NuPatch,1); ones(size(B,1)+size(Q,1),1)];
370%         x = linprog(f,A,bb);
371%=====================================================
372
373% quadprog ===========================================
374% only 2nd order smooth use L1 norm
375%        Q1size = size([Q1stXy;Q1stYy],1)% Q1stYz;Q1stXz],1)
376        Q1size = size([Q1stXy;Q1stYy; Q1stYz;Q1stXz],1)
377%        Q1size = size([Q1stXy;Q1stYy; Q1stYz;Q1stXz],1)
378%        SLsize = size([Q2ndLine;Q2ndVertLine],1)
379%        Q2size = size([Q2ndXz;Q2ndYz],1)
380        Q2size = size([Q2ndXz;Q2ndYy;Q2ndYz;Q2ndXy],1)
381        size([Q1stXy;Q1stYy;Q1stYz;Q1stXz])% Q1stYz;Q1stXz],1)
382%        size([Q2ndLine])
383        size([Q2ndXz;Q2ndYy;Q2ndYz;Q2ndXy])
384%       E = [Q1stYy ;Q1stYz; Q1stXz;...
385%              spdiags(ones(NuPatch,1),[0],NuPatch,NuPatch);...
386%              Q2ndLinez];
387%        e = [sparse(Q1size,1); -LearnedDepth(:); sparse(SLsize,1)];
388%        B = [Q2ndXz ; Q2ndYz];
389%       H = 2*[[Q1stYy;Q1stYz; Q1stXz;...
390%              spdiags(ones(NuPatch,1),[0],NuPatch,NuPatch);Q2ndLinez]'*...
391%             [Q1stYy;Q1stYz; Q1stXz;...
392%              spdiags(ones(NuPatch,1),[0],NuPatch,NuPatch);Q2ndLinez] sparse(NuPatch,Q2size*3);...
393%              sparse(Q2size*3,NuPatch+Q2size*3)];
394%        f =[2*[sparse(Q1size,1); -LearnedDepth(:); sparse(SLsize,1)]'*...
395%            [Q1stYy;Q1stYz; Q1stXz...
396%             spdiags(ones(NuPatch,1),[0],NuPatch,NuPatch); Q2ndLinez] ones(1,Q2size) sparse(1,Q2size*2)]';
397%        Aeq = [[Q2ndXz ; Q2ndYz] -spdiags(ones(Q2size,1),[0],Q2size,Q2size)...
398%               spdiags(ones(Q2size,1),[0],Q2size,Q2size) sparse(Q2size,Q2size);...
399%              [Q2ndXz ; Q2ndYz] spdiags(ones(Q2size,1),[0],Q2size,Q2size)...
400%               sparse(Q2size,Q2size) spdiags(ones(Q2size,1),[0],Q2size,Q2size)];
401%        beq = sparse(Q2size*2,1);
402%        lb = [5*ones(NuPatch,1);sparse(Q2size*2,1);...
403%              -2*max(LearnedDepth(:))*ones(Q2size,1)];
404%        ub = [max(LearnedDepth(:))*ones(NuPatch,1);...
405%              2*max(LearnedDepth(:))*ones(Q2size*2,1); sparse(Q2size,1)];
406%        options = optimset('LargeScale', 'on', 'Display', 'off');
407%        x = quadprog(H,f,[],[],Aeq,beq,lb,ub,[],options);
408%        predictedM = [spdiags(ones(NuPatch,1),[0],NuPatch,NuPatch)...
409%                      sparse(NuPatch,Q2size*3)]*x;
410%        A = [sparse(Q2size*2,NuPatch+Q2size)...
411%             spdiags([-ones(Q2size,1);ones(Q2size,1)],[0],Q2size*2,Q2size*2)];
412%        b =sparse(Q2size*2,1);
413%=====================================================
414%         predictedM = spdiags([ones(1,NuPatch) sparse(1,size(B,1)+size(Q,1))]',0,NuPatch...
415%                             ,NuPatch+size(B,1)+size(Q,1))*x;
416%        predictedM = (LearnedDepth(:));
417
418
419             %[[Q2ndYz] -spdiags(ones(Q2size,1),[0],Q2size,Q2size)...
420             %   spdiags(ones(Q2size,1),[0],Q2size,Q2size) sparse(Q2size,Q2size);...
421             %  [Q2ndYz] spdiags(ones(Q2size,1),[0],Q2size,Q2size)...
422             %   sparse(Q2size,Q2size) spdiags(ones(Q2size,1),[0],Q2size,Q2size)]*[predictedM; kt; ga; al] == 0;
423             
424%             [Q2ndXz; Q2ndYz]*predictedM <=kt;
425%             [Q2ndXz; Q2ndYz]*predictedM >= -kt;
426%             predictedM>=5;
427              %ga>=0;
428              %al<=0;
429%         cvx_end   
430%         toc;
431
432% post prosessing on straight line
433% second optimization
434% predictedM = [Q2ndLinez;spdiags(ones(NuPatch,1),[0],NuPatch,NuPatch)]\[sparse(SLsize,1); -LearnedDepth(:)];
435% predictedM = full(predictedM);
436        Date =date;
437%        predictedM =reshape(predictedM,VertYNuDepth,[]);
438         % quadprog Start
439  % Yalmip Start ===============================
440  mask = LearnedDepth(:)<80;
441  AssociateM = zeros(NuPatch,1);
442  AssociateM(mask) = 1;
443  %ClearLearnedDepth = LearnedDepth(:);
444  %ClearLearnedDepth(~mask) = 0;
445  Name{1} = '1stOSmooth';
446  Name{2} = '2ndOSmooth';
447  for m =step
448%  for m =1
449if true
450    m
451    opt = sdpsettings('solver','sedumi');
452    predictedM = sdpvar(NuPatch,1);
453    F = set(predictedM>=0)+set(predictedM<=80);
454    if m == 1
455       solvesdp(F,norm( [Q1stXy;Q1stYy; Q1stYz;Q1stXz;...
456                    spdiags(AssociateM,[0],NuPatch,NuPatch)]...
457                    *predictedM+[sparse(Q1size,1);-LearnedDepth(:)],1)...
458               , opt);
459%                    spdiags(ones(NuPatch,1),[0],NuPatch,NuPatch)]...
460    else
461  if false
462       solvesdp(F,norm( [Q2ndXz;Q2ndYy;Q2ndYz;Q2ndXy;...
463                    spdiags(AssociateM,[0],NuPatch,NuPatch)]...
464                    *predictedM+[sparse(Q2size,1);-LearnedDepth(:)],1)...
465               , opt);
466  end
467       solvesdp(F,norm( [Q1stXy; Q1stYy; Q1stYz; Q1stXz ;Q2ndXz;Q2ndYy;Q2ndYz;Q2ndXy;...
468                    spdiags(AssociateM,[0],NuPatch,NuPatch)]...
469                    *predictedM+[sparse(Q1size+Q2size,1);-LearnedDepth(:)],1)...
470               , opt);
471%                    spdiags(ones(NuPatch,1),[0],NuPatch,NuPatch)]...
472    end
473    predictedM = double(predictedM);
474end
475
476  % end       
477
478if false
479         cvx_begin
480             cvx_quiet(false);
481             variable predictedM(NuPatch,1);
482%             variable kt(Q2size,1);
483%             variable al(Q2size,1);
484%             variable ga(Q2size,1);
485             variable st(1,1);
486%             minimize(st+norm(predictedM(cell2mat(LineIndex(:,1)'))-predictedM(cell2mat(LineIndex(:,2)')),1)+...
487%                      norm(predictedM(cell2mat(LineIndexVert(:,3)'))-predictedM(cell2mat(LineIndexVert(:,4)')),1));
488%              norm([Q2ndLine;Q2ndVertLine;...
489             minimize(st);
490              norm([Q1stXy;Q1stYy;Q2ndYz;Q2ndXz;...
491                    spdiags(ones(NuPatch,1),[0],NuPatch,NuPatch)]...
492                    *predictedM+[sparse(Q1size+Q2size,1);-LearnedDepth(:)])<=st;
493%                    *predictedM+[sparse(SLsize,1);-LearnedDepth(:)])<=st;
494%            [[Q2ndXz] -spdiags(ones(Q2size,1),[0],Q2size,Q2size)...
495%                spdiags(ones(Q2size,1),[0],Q2size,Q2size) sparse(Q2size,Q2size);...
496%               [Q2ndXz] spdiags(ones(Q2size,1),[0],Q2size,Q2size)...
497%                sparse(Q2size,Q2size) -spdiags(ones(Q2size,1),[0],Q2size,Q2size)]*[predictedM; kt; ga; al] == 0;
498             
499%             for q = 0:min(1,size(LineIndex,1)-1)
500%                    predictedM(LineIndex{end-q,1})-predictedM(LineIndex{end-q,2})==0;
501%             end
502             predictedM>=0;
503%             ga>=0;
504%             al>=0;
505         cvx_end   
506%         mean(Q2ndLine*predictedM)
507%         mean(Q2ndVertLine*predictedM)
508end
509%         figure(1); plot(predictedM-LearnedDepth(:));
510%         figure(2); plot(Q2ndLine*predictedM);
511%         figure(3); plot(Q2ndLine*predictedM);
512%             for q = 0:min(1,(size(LineIndex,1)-1))
513%                    predictedM(LineIndex{end-q,1})-predictedM(LineIndex{end-q,2})
514%             end
515%         toc;
516%predictedM =LearnedDepth(:);
517% post prosessing on straight line
518
519% second optimization
520       
521        Date =date;
522        predictedM =reshape(predictedM,VertYNuDepth,[]);
523        depthMap = predictedM;
524        depthfile = strrep(filename{i},'img','depth_learned'); % the depth filename
525       system(['mkdir ' ScratchDataFolder '/DepthMRF_' Name{m} '_' DepthDirectory 'Clean2L1/']);
526       save([ScratchDataFolder '/DepthMRF_' Name{m} '_' DepthDirectory 'Clean2L1/' depthfile '.mat'],'depthMap');
527i%        save([ScratchDataFolder '/_predicted_' DepthDirectory '/' depthfile '.mat'],'depthMap');
528        clear depthMap;
529       
530%=====================================================
531        % 2d to would 3d
532        [Position3DPredicted] = im_cr2w_cr(predictedM,RayCenter);
533   
534        % generate new LowResImgIndexSuperpixelSep_deoffset
535        %LowResImgIndexSuperpixelSep_deoffset = LowResImgIndexSuperpixelSep{i};
536   
537        % add on image feature
538        %global Imf;
539        %Imf= cat(1,Position3DPredicted,permute(ones(VertY,1)*[1:HoriX],[3 1 2]), permute([1:VertY]'*ones(1,HoriX),[3 1 2]),permute(double(zeros(VertY,HoriX,3)),[3 1 2]),permute(double(LowResImgIndexSuperpixelSep_deoffset),[3 1 2]),permute(double(LowResImgIndexSuperpixelSep{i}),[3 1 2]) );
540   
541        % calculate each plane parameter for each superpixel
542        %[PlaneParameterPredicted] = fit_all_planes(RayLoResCorner); % hard work around 2min
543   
544        % generate VRML
545        Date = date;
546%        [VrmlName] = vrml_test_faceset_triangle_nosky_v3(filename{i},Position3DPredicted,predictedM,RayCenter,['PredictM_' DepthDirectory '_' num2str(logScale) '_' num2str(SkyExclude) '_' Date '_LearnedLogScaleNonsky_PredictNonsky_L21st_L12ndSmooth_StraightLineDoblueStitchOrientEst_gravity'],a,b,Ox,Oy,maskSky{i},1,10);
547%        size(VBrokeBook)
548%        [VrmlName] = vrml_test_faceset_grid_nojump(filename{i},Position3DPredicted,predictedM,RayCenter,['PredictM_' DepthDirectory '_' num2str(logScale) '_' num2str(SkyExclude) '_' Date '_LearnedLogScaleNonsky_PredictNonsky_L21st_L12ndSmooth_StraightLineDoblueStitchOrientEst_gravity_nonSep'],HBrokeBook,VBrokeBook,0,maskSky{i},1,10,a,b,Ox,Oy,100);
549      if m == 2
550        [VrmlName] = vrml_test_faceset_goodSkyBoundary(  filename{i}, Position3DPredicted,predictedM,RayCenter, [Name{m} DepthDirectory], ...
551                    [], [], 0, maskSky{i}, maskg{i}, 1, 0, a_default, b_default, Ox_default, Oy_default);
552%        [VrmlName] = vrml_test_faceset_goodSkyBoundary(fiLENAME{i},Position3DPREDicted,predictedM,RayCenter,[Name{i} DepthDirectory],[],[],0,maskSky{i},1,0,a,b,Ox,Oy);
553        system(['gzip -9 -c ' ScratchDataFolder '/vrml/' VrmlName ' > ' ScratchDataFolder '/vrml/' VrmlName '.gz']);
554        %delete([ScratchDataFolder '/vrml/' VrmlName]);
555      end
556            %vrml_test_faceset_triangle(filename{i},PlaneParameterPredicted,LowResImgIndexSuperpixelSep{i},LowResImgIndexSuperpixelSep_deoffset,[DepthDirectory '_' Date]);
557
558  end  % end of step
559end   
Note: See TracBrowser for help on using the repository browser.