source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/LearningCode/Inference/OldVersion/ReportPlaneParaMRF.m @ 37

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

Added original make3d

File size: 27.6 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  ReportPlaneParaMRF(step, DepthFolder,...
40          Sup,MedSup,depthMap,VarMap,RayOri, Ray,MedRay,maskSky,maskG,Algo,k, CornerList, OccluList,...
41          MultiScaleSupTable, StraightLineTable, HBrokeBook, VBrokeBook,previoslyStored,...
42          baseline);
43% This function runs the RMF over the plane parameter of each superpixels
44
45global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory...
46    ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default...
47    Horizon_default filename batchSize NuRow_default SegVertYSize SegHoriXSize WeiBatchSize PopUpVertY PopUpHoriX taskName;
48
49if nargin <20
50   baseline = 0;
51end
52
53% initialize parameters
54displayFlag = false;
55RenderVrmlFlag = true;
56StickHori = 5;%0.1; % sticking power in horizontal direction
57StickVert = 10;     % sticking power in vertical direction
58Center = 2; % Co-Planar weight at the Center of each superpixel
59HoriConf = 1; % set the confidant of the learned depth at the middle in Horizontal direction of the image
60VertConf = 0.01; % set the confidant of the learned depth at the top of the image
61BandWith = 1; % Nov29 1 Nov30 0.1 check result change to 0.1 12/1 0.1 lost detail
62mapVert = linspace(VertConf,1,VertYNuDepth); % modeling the gravity prior
63mapHori = [linspace(HoriConf,1,round(HoriXNuDepth/2)) fliplr(linspace(HoriConf,1,HoriXNuDepth-round(HoriXNuDepth/2)))]; % modeling the gravity prior
64% ========set the range of depth that our model in
65ClosestDist = 1;
66% set the FarestDist to very 5 times to median depth
67FarestDist = 1.5*median(depthMap(:)); % tried on university % nogood effect but keep it since usually it the rangelike this   % change to 1.5 for church
68% ================================================
69ceiling = 0*VertYNuDepth; % set the position of the ceiling, related to No plane coming back constrain % changed for newchurch
70Name{1} = 'FraWOPri';
71Name{2} = 'FraCoP';
72if isempty(MultiScaleSupTable)
73   Name{3} = 'Var_FraStickCoP';
74else
75   Name{3} = 'Var_FraStickCoPSTasCoP';
76end
77if ~isempty(MultiScaleSupTable)
78   MultiScaleFlag = true;
79   WeiV = 2*ones(1,size(MultiScaleSupTable,2)-1);
80else
81   MultiScaleFlag = false;
82   WeiV = 1;
83end
84WeiV(1,1:2:end) = 6; % emphasize the middle scale three times smaller than large scale
85WeiV =WeiV./sum(WeiV);% normalize if pair of superpixels have same index in all the scale, their weight will be 10
86ShiftStick = -.1;  % between -1 and 0, more means more smoothing.
87ShiftCoP = -.5;  % between -1 and 0, more means more smoothing.
88gravity =true; % if true, apply the HoriConf and VertConf linear scale weight
89CoPST = true; % if true, apply the Straight line prior as the Co-Planar constrain
90ConerImprove = false;
91FractionalDepthError = true;
92% get rid of the error point and sky point in the depthMap
93% set every depth bigger than FarestDistmeter to FarestDistmeters
94%CleanedDepthMap = (depthMapif ~previoslyStored >80).*medfilt2(depthMap,[4 4])+(depthMap<=80).*depthMap;
95CleanedDepthMap = depthMap;
96%CleanedDepthMap(depthMap>FarestDist) = FarestDist; % don't clean the point >80 sometimes it occlusion
97disp('Nu of depthMap>FarestDist');
98%sum(sum(depthMap>FarestDist))
99CleanedDepthMap(depthMap>FarestDist) = NaN; % don't clean the point >80 sometimes it occlusion
100%size(CleanedDepthMap)
101%size(depthMap)
102Posi3D = im_cr2w_cr(CleanedDepthMap,permute(Ray,[2 3 1]));
103if ~previoslyStored
104
105NewMap = [rand(max(Sup(:)),3); [0 0 0]];
106% Clean the Sup near sky
107%maskSky = imdilate(maskSky, strel('disk', 3) );
108%[Sup,MedSup]=CleanedSup(Sup,MedSup,maskSky);
109maskSky = Sup == 0;
110maskSkyEroded = imerode(maskSky, strel('disk', 4) );
111SupEpand = ExpandSup2Sky(Sup,maskSkyEroded);
112NuPatch = HoriXNuDepth*VertYNuDepth-sum(maskSky(:));
113
114NuSup = setdiff(unique(Sup)',0);
115NuSup = sort(NuSup);
116NuSupSize = size(NuSup,2);
117
118% Sup index and planeParameter index inverse map
119Sup2Para = sparse(1,max(Sup(:)));
120Sup2Para(NuSup) = 1:NuSupSize;
121
122% constructinf the Straight line prior matrix Will be add in the CoPlane matrix
123NuLine = size(StraightLineTable,2);
124CoPSTList = [];
125for i = 1:NuLine
126    L = StraightLineTable{i};
127    X = L(1:(end-1))';
128    Y = L(2:end)';
129    if isempty(X)
130       continue;
131    end
132    for j = 1:size(X,1)
133        if X(j)~=Y(j)
134           CoPSTList = [CoPSTList; X(j) Y(j)];
135        end
136    end
137end
138end
139
140% Generate the Matrix for MRF
141tic
142 % ===========================================================================================================================================
143  groundThreshold = cos([ zeros(1, VertYNuDepth - ceil(VertYNuDepth/2)+10) linspace(0,15,ceil(VertYNuDepth/2)-10)]*pi/180); 
144  %  v1 15 v2 20 too big v3 20 to ensure non misclassified as ground.
145%  verticalThreshold = cos(linspace(5,55,VertYNuDepth)*pi/180); % give a vector of size 55 in top to down :
146  verticalThreshold = cos([ 5*ones(1,VertYNuDepth - ceil(VertYNuDepth/2)) linspace(5,55,ceil(VertYNuDepth/2))]*pi/180); % give a vector of size 55 in top to down :
147  % 50 means suface norm away from y axis more than 50 degree
148 % ===========================================================================================================================================
149PosiM = sparse(0,0);
150VarM = sparse(0,0);
151RayMd = sparse(0,0);
152RayAllOriM = sparse(0,0);
153RayAllM = sparse(0,0);
154RayMtilt = sparse(0,0);
155RayMCent = sparse(0,0);
156DepthInverseMCent = [];
157DepthInverseM = [];
158YPointer = [];
159YPosition = [];
160beta = [];
161EmptyIndex = [];
162for i = NuSup
163%    mask = Sup ==i;
164    mask = SupEpand ==i; % include the Ray that will be use to expand the NonSky
165    RayAllOriM = blkdiag( RayAllOriM, RayOri(:,mask)');
166    RayAllM = blkdiag( RayAllM, Ray(:,mask)');
167    mask = Sup ==i; % Not include the Ray that will be use to expand the NonSky   
168    [yt x] = find(mask);
169    CenterX = round(median(x));
170    CenterY = round(median(yt));
171    YPointer = [YPointer; CenterY >= ceiling];
172    YPosition = [YPosition; CenterY];
173    mask(isnan(CleanedDepthMap)) = false;
174    SupNuPatch(i) = sum(mask(:));
175%    if sum(mask(:)) < 5
176%       EmptyIndex = [EmptyIndex; i];
177%       mask(:) = false;
178%    end
179    % find center point
180    [yt x] = find(mask);
181    CenterX = round(median(x));
182    CenterY = round(median(yt));
183 
184  if ~all(mask(:)==0)
185    if gravity
186      Pa2CenterRatio = median(CleanedDepthMap(mask))./CleanedDepthMap(mask);
187      if sum(mask(:)) > 0
188         RayMtilt = blkdiag(RayMtilt, ...
189             ( Pa2CenterRatio(:,[1 1 1]).*repmat(RayOri(:,CenterY,CenterX)',[ SupNuPatch(i) 1])- RayOri(:,mask)'));
190      else
191         RayMtilt = blkdiag(RayMtilt, RayOri(:,mask)');
192      end
193      RayMCent = blkdiag(RayMCent, RayOri(:,CenterY,CenterX)'*SupNuPatch(i)*mapVert(CenterY)*mapHori(CenterX));
194      PosiM = blkdiag(PosiM,Posi3D(:,mask)');%.*repmat( mapVert(yt)',[1 3]).*repmat( mapHori(x)',[1 3]));
195      VarM = [VarM; VarMap(mask).*(mapVert(yt)').*( mapHori(x)')];
196      RayMd = blkdiag(RayMd,RayOri(:,mask)'.*repmat( mapVert(yt)',[1 3]).*repmat( mapHori(x)',[1 3]));
197      DepthInverseMCent = [DepthInverseMCent; 1./median(CleanedDepthMap(mask)).*SupNuPatch(i)* mapVert(CenterY)'*mapHori(CenterX)];
198      DepthInverseM = [DepthInverseM; 1./CleanedDepthMap(mask).* mapVert(yt)'.*mapHori(x)'];
199    else
200      Pa2CenterRatio = CleanedDepthMap(CenterY,CenterX)./CleanedDepthMap(mask);
201      if sum(mask(:)) > 0
202         RayMtilt = blkdiag(RayMtilt, ...
203             ( Pa2CenterRatio(:,[1 1 1]).*repmat(RayOri(:,CenterY,CenterX)',[ SupNuPatch(i) 1])- RayOri(:,mask)'));
204      else
205         RayMtilt = blkdiag(RayMtilt, RayOri(:,mask)');
206      end
207      RayMCent = blkdiag(RayMCent, RayOri(:,CenterY,CenterX)'*SupNuPatch(i));
208      PosiM = blkdiag(PosiM,Posi3D(:,mask)');
209      VarM = [VarM; VarMap(mask)];
210      RayMd = blkdiag(RayMd,RayOri(:,mask)');
211      DepthInverseMCent = [DepthInverseMCent; 1./median(CleanedDepthMap(mask)).*SupNuPatch(i)];
212      DepthInverseM = [DepthInverseM; 1./CleanedDepthMap(mask)];
213    end
214  else
215     RayMtilt = blkdiag(RayMtilt, RayOri(:,mask)');
216     RayMCent = blkdiag(RayMCent, RayOri(:,mask)');
217     PosiM = blkdiag(PosiM, Posi3D(:,mask)');
218     VarM = [VarM; VarMap(mask)];
219     RayMd = blkdiag(RayMd, RayOri(:,mask)');
220  end
221end
222YPointer(YPointer==0) = -1;
223
224% buliding CoPlane Matrix=========================================================================
225BounaryPHori = conv2(Sup,[1 -1],'same') ~=0;
226BounaryPHori(:,end) = 0;
227BounaryPVert = conv2(Sup,[1; -1],'same') ~=0;
228BounaryPVert(end,:) = 0;
229ClosestNList = [ Sup(find(BounaryPHori==1)) Sup(find(BounaryPHori==1)+VertYNuDepth);...
230                 Sup(find(BounaryPVert==1)) Sup(find(BounaryPVert==1)+1)];
231ClosestNList = sort(ClosestNList,2);
232ClosestNList = unique(ClosestNList,'rows');
233ClosestNList(ClosestNList(:,1) == 0,:) = [];
234BoundaryAll = BounaryPHori + BounaryPHori(:,[1 1:(end-1)])...
235             +BounaryPVert + BounaryPVert([1 1:(end-1)],:);
236BoundaryAll([1 end],:) = 1;
237BoundaryAll(:,[1 end]) = 1;
238NuSTList = 0;
239if CoPST
240   ClosestNList = [ClosestNList; CoPSTList];
241   NuSTList = size(CoPSTList,1);
242end
243NuNei = size(ClosestNList,1);
244CoPM1 = sparse(0,3*NuSupSize);
245CoPM2 = sparse(0,3*NuSupSize);
246CoPEstDepth = sparse(0,0);
247CoPNorM = [];
248WeiCoP = [];
249for i = 1: NuNei
250%  if ~CornerList(i)
251    mask = Sup == ClosestNList(i,1);
252    SizeMaskAll = sum(mask(:));
253    [y x] = find(mask);
254    CenterX = round(median(x));
255    CenterY = round(median(y));
256    y = find(mask(:,CenterX));
257    if ~isempty(y)
258       CenterY = round(median(y));
259    end
260%    CenterX = round(median(x));
261%    CenterY = round(median(yt));
262   
263    temp1 = sparse(1, 3*NuSupSize);
264    temp2 = sparse(1, 3*NuSupSize);
265    temp1(:,(Sup2Para(ClosestNList(i,1))*3-2): Sup2Para(ClosestNList(i,1))*3) = Ray(:,CenterY,CenterX)';
266    temp2(:,(Sup2Para(ClosestNList(i,2))*3-2): Sup2Para(ClosestNList(i,2))*3) = Ray(:,CenterY,CenterX)';
267    %NuNei-NuSTList
268%    i
269    if i < NuNei-NuSTList % only immediate connecting superpixels are weighted using MultiScaleSup
270%       wei = WeiV*10;%*(MultiScaleSupTable(Sup2Para(ClosestNList(i,1)),2:end) == MultiScaleSupTable(Sup2Para(ClosestNList(i,2)),2:end))'; 
271       if MultiScaleFlag
272          vector = (MultiScaleSupTable(Sup2Para(ClosestNList(i,1)),2:end) == MultiScaleSupTable(Sup2Para(ClosestNList(i,2)),2:end));
273          expV = exp(-10*(WeiV*vector' + ShiftCoP) );
274          wei = 1/(1+expV);
275       else
276          wei = 1;
277       end
278    else
279       wei = 1;
280    end
281    CoPM1 = [CoPM1; temp1*wei];
282    CoPM2 = [CoPM2; temp2*wei];
283    tempWeiCoP = [SizeMaskAll];
284    CoPEstDepth = [CoPEstDepth; max(median(CleanedDepthMap(mask)),ClosestDist)];
285   
286    mask = Sup == ClosestNList(i,2);
287    SizeMaskAll = sum(mask(:));
288    [y x] = find(mask);
289    CenterX = round(median(x));
290    CenterY = round(median(y));
291    y = find(mask(:,CenterX));
292    if ~isempty(y)
293       CenterY = round(median(y));
294    end
295%    [yt x] = find(mask);
296%    CenterX = round(median(x));
297%    CenterY = round(median(yt));
298
299    temp1 = sparse(1, 3*NuSupSize);
300    temp2 = sparse(1, 3*NuSupSize);
301    temp1(:,(Sup2Para(ClosestNList(i,1))*3-2): Sup2Para(ClosestNList(i,1))*3) = Ray(:,CenterY,CenterX)';
302    temp2(:,(Sup2Para(ClosestNList(i,2))*3-2): Sup2Para(ClosestNList(i,2))*3) = Ray(:,CenterY,CenterX)';
303    CoPM1 = [CoPM1; temp1*wei];
304    CoPM2 = [CoPM2; temp2*wei];
305    tempWeiCoP = [tempWeiCoP; SizeMaskAll];
306    WeiCoP = [WeiCoP; tempWeiCoP];
307    CoPEstDepth = [CoPEstDepth; max(median(CleanedDepthMap(mask)),ClosestDist)];
308%  end
309end%=========================================================================================================
310
311% find the boundary point that might need to be stick ot each other==========================================
312HoriStickM_i = sparse(0,3*NuSupSize);
313HoriStickM_j = sparse(0,3*NuSupSize);
314HoriStickPointInd = [];
315EstDepHoriStick = [];
316for i = find(BounaryPHori==1)'
317    j = i+VertYNuDepth;
318    if Sup(i) == 0 || Sup(j) == 0
319       continue;
320    end
321%  if ~OccluList(sum( ClosestNList == repmat(sort([Sup(i) Sup(j)]), [NuNei  1]),2) == 2)
322%  size(OccluList)
323%  if ~any(sum( OccluList == repmat(sort([Sup(i) Sup(j)]), [size(OccluList,1)  1]),2) == 2)
324    Target(1) = Sup2Para(Sup(i));
325    Target(2) = Sup2Para(Sup(j));
326    rayBoundary(:,1) =  Ray(:,i);
327    rayBoundary(:,2) =  Ray(:,i);
328%    betaTemp = StickHori;%*(DistStickLengthNormWei.^2)*beta(Target(I));
329%    betaTemp = StickHori*WeiV;%*(MultiScaleSupTable(Sup2Para(Sup(i)),2:end) == MultiScaleSupTable(Sup2Para(Sup(j)),2:end))';%*(DistStickLengthNormWei.^2)*beta(Target(I));
330    if MultiScaleFlag
331          vector = (MultiScaleSupTable(Sup2Para(Sup(i)),2:end) == MultiScaleSupTable(Sup2Para(Sup(j)),2:end));
332          expV = exp(-10*(WeiV*vector' + ShiftStick) );
333       betaTemp = StickHori*(0.5+1/(1+expV)); %*(DistStickLengthNormWei.^2)*beta(Target(I));
334       % therr should always be sticking (know don't care about occlusion)
335    else
336       betaTemp = StickHori;
337    end
338    temp = sparse(3,NuSupSize);
339    temp(:,Target(1)) = rayBoundary(:,1);
340    HoriStickM_i = [HoriStickM_i; betaTemp*temp(:)'];
341    temp = sparse(3,NuSupSize);
342    temp(:,Target(2)) = rayBoundary(:,2);
343    HoriStickM_j = [HoriStickM_j; betaTemp*temp(:)'];
344    EstDepHoriStick = [EstDepHoriStick; sqrt(max(CleanedDepthMap(i),ClosestDist)*max(CleanedDepthMap(j),ClosestDist))];
345    HoriStickPointInd = [HoriStickPointInd i ];
346%  else
347%    disp('Occlu');
348%  end
349end
350VertStickM_i = sparse(0,3*NuSupSize);
351VertStickM_j = sparse(0,3*NuSupSize);
352VertStickPointInd = [];
353EstDepVertStick = [];
354for i = find(BounaryPVert==1)'
355    j = i+1;
356    if Sup(i) == 0 || Sup(j) == 0
357       continue;
358    end
359%  if ~OccluList(sum( ClosestNList == repmat(sort([Sup(i) Sup(j)]), [NuNei  1]),2) == 2)
360%  if ~any(sum( OccluList == repmat(sort([Sup(i) Sup(j)]), [size(OccluList,1)  1]),2) == 2)
361    Target(1) = Sup2Para(Sup(i));
362    Target(2) = Sup2Para(Sup(j));
363    rayBoundary(:,1) =  Ray(:,i);
364    rayBoundary(:,2) =  Ray(:,i);
365%    betaTemp = StickVert;%*(DistStickLengthNormWei.^2)*beta(Target(I));
366    if MultiScaleFlag
367       vector = (MultiScaleSupTable(Sup2Para(Sup(i)),2:end) == MultiScaleSupTable(Sup2Para(Sup(j)),2:end));
368       expV = exp(-10*(WeiV*vector' + ShiftStick) );
369       betaTemp = StickVert*(0.5+1/(1+expV));
370       % therr should always be sticking (know don't care about occlusion)
371    else
372       betaTemp = StickVert;
373    end
374    temp = sparse(3,NuSupSize);
375    temp(:,Target(1)) = rayBoundary(:,1);
376    VertStickM_i = [VertStickM_i; betaTemp*temp(:)'];
377    temp = sparse(3,NuSupSize);
378    temp(:,Target(2)) = rayBoundary(:,2);
379    VertStickM_j = [VertStickM_j; betaTemp*temp(:)'];
380    EstDepVertStick = [EstDepVertStick; sqrt(max(CleanedDepthMap(i),ClosestDist)*max(CleanedDepthMap(j),ClosestDist))];
381    VertStickPointInd = [VertStickPointInd i ];
382%  else
383%    disp('Occlu');
384%  end
385end
386% ======================================Finish building up matrix=====================hard work======================
387% ================================================================================================================
388depthfile = strrep(filename{k},'img','depth_learned'); %
389
390for i = step
391    opt = sdpsettings('solver','sedumi');
392    ParaPPCP = sdpvar(3*NuSupSize,1);
393    F = set(ParaPPCP(3*(1:NuSupSize)-1).*YPointer<=0) ...
394        +set(RayAllM*ParaPPCP >=1/FarestDist)...
395        +set(RayAllM*ParaPPCP <=1/ClosestDist)...
396        +set(RayAllOriM*ParaPPCP >=1/FarestDist)...
397        +set(RayAllOriM*ParaPPCP <=1/ClosestDist);
398% First fit the plane to find the estimated plane parameters
399% If PosiM contain NaN data the corresponding Plane Parameter will also be NaN
400    if i == 1 % W/O  priors
401%    solvesdp(F,norm( (PosiM*ParaPPCP-ones(size(PosiM,1),1))./(abs(VarM)),1)...
402    solvesdp(F,norm( (PosiM*ParaPPCP-ones(size(PosiM,1),1))./exp(abs(VarM)/BandWith),1)...
403             , opt);
404    elseif i ==2 % Coner
405    solvesdp(F,norm( (PosiM*ParaPPCP-ones(size(PosiM,1),1))./exp(abs(VarM)/BandWith),1)...
406             +Center*norm(((CoPM1 - CoPM2)*ParaPPCP).*CoPEstDepth, 1)...
407             , opt);
408    else % all the priors
409%    sum(sum(isnan(PosiM)))
410    disp('Whole MRF')
411    solvesdp(F,norm( (PosiM*ParaPPCP-ones(size(PosiM,1),1))./exp(abs(VarM)/BandWith),1)...
412             +Center*norm(((CoPM1 - CoPM2)*ParaPPCP).*CoPEstDepth, 1)...
413             +norm(((HoriStickM_i-HoriStickM_j)*ParaPPCP).*EstDepHoriStick,1)...
414             +norm(((VertStickM_i-VertStickM_j)*ParaPPCP).*EstDepVertStick,1)...
415             , opt);
416    end
417
418    %pause
419    ParaPPCP = double(ParaPPCP);
420    sum(isnan(ParaPPCP))
421    %pause
422    SepPointMeasureHori = 1./(HoriStickM_i*ParaPPCP)-1./(HoriStickM_j*ParaPPCP);
423    SepPointMeasureVert = 1./(VertStickM_i*ParaPPCP)-1./(VertStickM_j*ParaPPCP);
424    ParaPPCP = reshape(ParaPPCP,3,[]);
425    %any(any(isnan(ParaPPCP)))
426    % porject the ray on planes to generate the ProjDepth
427    FitDepthPPCP = FarestDist*ones(1,VertYNuDepth*HoriXNuDepth);
428    FitDepthPPCP(~maskSkyEroded) = (1./sum(ParaPPCP(:,Sup2Para(SupEpand(~maskSkyEroded))).*Ray(:,~maskSkyEroded),1))';
429    FitDepthPPCP = reshape(FitDepthPPCP,VertYNuDepth,[]);
430    % storage for comparison purpose ======================
431    depthMap = FarestDist*ones(1,VertYNuDepth*HoriXNuDepth);
432    depthMap(~maskSkyEroded) = (1./sum(ParaPPCP(:,Sup2Para(SupEpand(~maskSkyEroded))).*RayOri(:,~maskSkyEroded),1))';
433    depthMap = reshape(depthMap,VertYNuDepth,[]);
434%    if baseline == 0
435%       system(['mkdir ' ScratchDataFolder '/' Name{i} '_' DepthFolder '/']);
436%       save([ScratchDataFolder '/' Name{i} '_' DepthFolder '/' depthfile '.mat'],'depthMap');
437%    elseif baseline == 1
438%       system(['mkdir ' ScratchDataFolder '/' Name{i} '_' DepthFolder '_baseline/']);
439%       save([ScratchDataFolder '/' Name{i} '_' DepthFolder '_baseline/' depthfile '.mat'],'depthMap');
440%    else
441%       system(['mkdir ' ScratchDataFolder '/' Name{i} '_' DepthFolder '_baseline2/']);
442%       save([ScratchDataFolder '/' Name{i} '_' DepthFolder '_baseline2/' depthfile '.mat'],'depthMap');
443%    end
444 
445    % =====================================================
446    [Position3DFitedPPCP] = im_cr2w_cr(FitDepthPPCP,permute(Ray,[2 3 1]));
447    if false %RenderVrmlFlag && i==3
448       [VrmlName] = vrml_test_faceset_goodSkyBoundary(  filename{k}, Position3DFitedPPCP, FitDepthPPCP, permute(Ray,[2 3 1]), [Name{i} DepthFolder 'ExpVar'], ...
449                    [], [], 0, maskSkyEroded, maskG, 1, 0, a_default, b_default, Ox_default, Oy_default);
450       system(['gzip -9 -c ' ScratchDataFolder '/vrml/' VrmlName ' > ' ScratchDataFolder '/vrml/' VrmlName '.gz']);
451       %delete([ScratchDataFolder '/vrml/' VrmlName]);
452    end
453%    save([ScratchDataFolder '/data/PreDepth/FitDepthPPCP' num2str(k) '.mat'],'FitDepthPPCP','SepPointMeasureHori',...
454%      'SepPointMeasureVert','VertStickPointInd','HoriStickPointInd');
455%save([ScratchDataFolder '/data/All.mat']);
456end
457
458%return;
459%==================Finished for one step MRF==========================================================================================================
460
461
462% ====================================following are 2nd and 3rd step MRF to give more visually pleasing result=======================================
463if ConerImprove
464[CoPM1 CoPM2 WeiCoP Sup Sup2ParaNew FixPara NuSup VertStickPointInd HoriStickPointInd] ...
465            = FixParaFreeCorner( Sup, Ray, HoriStickPointInd, VertStickPointInd,...
466              SepPointMeasureHori, SepPointMeasureVert, OccluList, WeiV);
467
468RayAllM = sparse(0,0);
469YPointer = [];
470for i = NuSup
471    mask = Sup ==i;
472    RayAllM = blkdiag( RayAllM, Ray(:,mask)');
473    [yt x] = find(mask);
474    CenterX = round(median(x));
475    CenterY = round(median(yt));
476    YPointer = [YPointer; CenterY >= ceiling];
477end
478
479NuSupSize = size(NuSup,2);
480opt = sdpsettings('solver','sedumi');
481ParaCorner = sdpvar(3*NuSupSize,1);
482F = set(ParaCorner(3*(1:NuSupSize)-1).*YPointer<=0) + set(RayAllM*ParaCorner >=1/FarestDist)...
483    +set(RayAllM*ParaCorner <=1/ClosestDist)...
484    +set(ParaCorner([(Sup2ParaNew(FixPara)*3-2); (Sup2ParaNew(FixPara)*3-1); (Sup2ParaNew(FixPara)*3)]) == ...
485     ParaPPCP([(Sup2Para(FixPara)*3-2); (Sup2Para(FixPara)*3-1); (Sup2Para(FixPara)*3)]));
486solvesdp(F,Center*norm((CoPM1 - CoPM2)*ParaCorner, 1)...
487          , opt);
488ParaCorner = double(ParaCorner);
489ParaCorner = reshape(ParaCorner,3,[]);
490%FitDepthCorner = 80*ones(1,VertYNuDepth*HoriXNuDepth);
491FitDepthCorner = FarestDist*ones(1,VertYNuDepth*HoriXNuDepth);
492FitDepthCorner(Sup~=0) = (1./sum(ParaCorner(:,Sup2ParaNew(Sup(Sup~=0))).*Ray(:,Sup~=0),1))';
493FitDepthCorner = reshape(FitDepthCorner,VertYNuDepth,[]);
494[Position3DFitedCorner] = im_cr2w_cr(FitDepthCorner,permute(Ray,[2 3 1]));
495[VrmlName] = vrml_test_faceset_goodSkyBoundary( filename{k}, Position3DFitedCorner, FitDepthCorner, permute(Ray,[2 3 1]), [Name 'Corner'], ...
496[], [], 0, maskSkyEroded, maskG, 1, 0, a_default, b_default, Ox_default, Oy_default);
497system(['gzip -9 -c ' ScratchDataFolder '/vrml/' VrmlName ' > ' ScratchDataFolder '/vrml/' VrmlName '.gz']);
498%delete([ScratchDataFolder '/vrml/' VrmlName]);
499end
500%return;
501% ============================================================
502;% generating new PosiMPPCP using the new position
503
504save([ScratchDataFolder '/data/VertShiftVrml.mat' ] );
505%  groundThreshold = cos(5*pi/180);  % make small range
506%  verticalThreshold = cos(50*pi/180);
507  normPara = norms(ParaPPCP);
508  normalizedPara = ParaPPCP ./ repmat( normPara, [3 1]);
509  groundPara = abs(normalizedPara(2,:)) >= groundThreshold(YPosition);
510  groundParaInd = find(groundPara);
511  verticalPara = abs(normalizedPara(2,:)) <= verticalThreshold(YPosition); % change to have different range of vertical thre ================
512  verticalParaInd = find(verticalPara);
513
514 if false
515    [CoPM1 CoPM2 HoriStickM_i HoriStickM_j VertStickM_i VertStickM_j WeiCoP NewSup NewNuSup NewNuSupSize NewSup2Para FixPara VertStickPointInd HoriStickPointInd] ...
516            = FreeSupSharpCorner( Sup, Ray, OccluList, WeiV, ShiftStick, StickHori, StickVert,...
517              MultiScaleSupTable, verticalParaInd, graoundParaInd, MultiScaleFlag);
518
519 end
520
521  indexVertical = find( verticalPara)*3-1;
522  indexGroundX = find( groundPara)*3-2;
523  indexGroundZ = find( groundPara)*3;
524
525  PosiMPPCP = sparse(0,0);
526  VarM2 = sparse(0,0);
527%  VertVar = 10^(-2);
528  for i = NuSup
529      mask = Sup == i;
530      if any(verticalParaInd == Sup2Para(i))
531         mask = logical(zeros(size(Sup)));
532      end
533%         PosiMPPCP = blkdiag(PosiMPPCP, Posi3D(:,mask)');
534%         VarM2 = [VarM2; VertVar*ones(sum(mask(:)), 1)];         
535  %       mask = logical(zeros(size(Sup)));
536%      elseif any(groundParaInd == Sup2Para(i))
537%         PosiMPPCP = blkdiag(PosiMPPCP, Position3DFitedPPCP(:,mask)');
538%         VarM2 = [VarM2; VarMap(mask)];         
539%      else
540         PosiMPPCP = blkdiag(PosiMPPCP, Posi3D(:,mask)');
541         VarM2 = [VarM2; VarMap(mask)];         
542%      end
543  end
544
545  opt = sdpsettings('solver','sedumi');
546  Para = sdpvar(3*NuSupSize,1);
547  F = set(Para(3*(1:NuSupSize)-1).*YPointer<=0) + set(RayAllM*Para >=1/FarestDist)...
548      +set(RayAllM*Para <=1/ClosestDist)...
549      +set(Para(indexVertical) == 0);
550%      +set(Para(indexGroundX) == 0)...
551%      +set(Para(indexGroundZ) == 0);
552if FractionalDepthError
553   size(PosiMPPCP)
554   solvesdp(F,norm( ( PosiMPPCP*Para-ones(size(PosiMPPCP,1),1))./exp(abs(VarM2)/BandWith),1)...
555             +Center*norm(((CoPM1 - CoPM2)*Para).*CoPEstDepth, 1)...
556             +norm(((HoriStickM_i-HoriStickM_j)*Para).*EstDepHoriStick,1)...
557             +norm(((VertStickM_i-VertStickM_j)*Para).*EstDepVertStick,1)...
558             +10*norm( Para(indexGroundX)./ normPara( find(groundPara) )', 1)  ...
559             +10*norm( Para(indexGroundZ)./ normPara( find(groundPara) )', 1) ...
560             , opt);
561%             sqrt( max(CoPM1*ParaPPCP(:), 1/FarestDist).*max(CoPM2*ParaPPCP(:), 1/FarestDist)), 1)...
562%             +norm(((VertStickM_i-VertStickM_j)*Para)./...
563%             sqrt( max(VertStickM_i*ParaPPCP(:), 1/FarestDist).*max(VertStickM_j*ParaPPCP(:), 1/FarestDist)),1)...
564%   pause;
565    %         +1000*norm( Para(indexVertical)./ normPara( find(verticalPara) )',1) ...
566%            +norm(((HoriStickM_i-HoriStickM_j)*Para)./sqrt((VertStickM_i*ParaPPCP(:)).*(VertStickM_j*ParaPPCP(:))),1)...
567else
568   solvesdp(F,norm( RayMd*Para - DepthInverseM,1)...
569            +Center*norm((CoPM1 - CoPM2)*Para,1)...
570            +norm((VertStickM_i-VertStickM_j)*Para,1)...
571            +norm((HoriStickM_i-HoriStickM_j)*Para,1)...
572            +1000*norm( Para(indexVertical)./ normPara( find(verticalPara) )',1) ...
573            +1000*norm( Para(indexGroundX)./ normPara( find(groundPara) )', 1)  ...
574            +1000*norm( Para(indexGroundZ)./ normPara( find(groundPara) )', 1) ...
575            , opt);
576%         +0.01*norm( RayMtilt*ParaPPCP,1)...       
577%         +norm(spdiags(WeiCoP,0,size(CoPM1,1),size(CoPM1,1))*(CoPM1 - CoPM2)*ParaPPCP,1)...
578%         +norm( (CoPM1 - CoPM2)*ParaPPCP,1 )...
579end
580  Para = reshape(Para,3,[]);
581  Para = double(Para);
582  sum(sum(isnan(Para)))
583  %pause
584%  FitDepth = 80*ones(1,VertYNuDepth*HoriXNuDepth);
585  FitDepth = FarestDist*ones(1,VertYNuDepth*HoriXNuDepth);
586  FitDepth(~maskSkyEroded) = (1./sum(Para(:,Sup2Para(SupEpand(~maskSkyEroded))).*Ray(:,~maskSkyEroded),1))';
587  FitDepth = reshape(FitDepth,VertYNuDepth,[]);
588  %sum(isnan(FitDepth(:)))
589  [Position3DFited] = im_cr2w_cr(FitDepth,permute(Ray,[2 3 1]));
590  Position3DFited(3,:) = -Position3DFited(3,:);
591  Position3DFited = permute(Position3DFited,[2 3 1]);
592  temp = ray(:,:,1:2)./repmat(ray(:,:,3),[1 1 2]);
593  PositionTex = permute(temp./repmat(cat(3,a,b),[VertYNuDepth HoriXNuDepth 1])+repmat(cat(3,Ox,Oy),[VertYNuDepth HoriXNuDepth 1]),[3 1 2]);
594  PositionTex = permute(PositionTex,[2 3 1]);
595  WrlFacest(Position3DFited,ones(3,3,2),Sup,filename{1},'./')
596%   [VrmlName] = vrml_test_faceset_goodSkyBoundary(     filename{k}, Position3DFited, FitDepth, permute(Ray,[2 3 1]), 'VNonSupport_ExpVar_NewVert5_50', ...
597%                                       [], [], 0, maskSkyEroded, maskG, 1, 0, a_default, b_default, Ox_default, Oy_default);
598%   system(['gzip -9 -c ' ScratchDataFolder '/vrml/' VrmlName ' > ' ScratchDataFolder '/vrml/' VrmlName '.gz']);
599%   system(['mv ' ScratchDataFolder '/vrml/' VrmlName '.gz ' ScratchDataFolder '/vrml/' VrmlName]);
600  %delete([ScratchDataFolder '/vrml/' VrmlName]);
601
602% save depth Map ++++++++++++++++
603%       depthMap = FitDepth;
604%       system(['mkdir ' ScratchDataFolder '/VNonSupport_' DepthFolder '/']);
605%       save([ScratchDataFolder '/VNonSupport_' DepthFolder '/' depthfile '.mat'],'depthMap');
606% =============================
607%end
608toc
609return;
Note: See TracBrowser for help on using the repository browser.