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

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

Added original make3d

File size: 14.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 []=OcclusionMRF(k);
40
41% not working perfectly. More search needed.
42
43global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory...
44    ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default...
45    Horizon_default filename batchSize NuRow_default SegVertYSize SegHoriXSize WeiBatchSize PopUpVertY PopUpHoriX taskName;
46
47% This function run a boundary MRF using the laser data to estimate the occlusion boundary
48tic
49% set Parameters
50WSLInitialWei = 0.5;
51WCornerInitialWei = 0.3;
52SLExcludeHoriWei = 5; % Hori have a bigger resolusion so have a bigger Exlude region
53SLExcludeVertWei = 10;
54BpWidthV = ceil(0.01*VertYNuDepth); % assume uniform distibution of the shift of estimated Bp with Width = BpWidthV (Precentage in Vert diection)
55BpWidthH = ceil(0.1*HoriXNuDepth); % BpWidthH (Precentage in Vert diection)
56
57% prepare the data
58%1) depth ready
59depthfile = strrep(filename{k},'img','depth_sph_corr'); % the depth filename
60load([ScratchDataFolder '/Gridlaserdata/' depthfile '.mat']);
61LaserDepth = Position3DGrid(:,:,4);
62% 2) ProjXY ready
63PicsinfoName = strrep(filename{k},'img','picsinfo');
64temp = dir([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']);
65if size(temp,1) == 0
66   a = a_default;
67   b = b_default;
68   Ox = Ox_default;
69   Oy = Oy_default;
70   Horizon = Horizon_default;
71else
72   load([GeneralDataFolder '/PicsInfo/' PicsinfoName '.mat']);
73end
74RayProjY = repmat((1:VertYNuDepth)',[1 HoriXNuDepth]);
75RayProjX = repmat((1:HoriXNuDepth),[VertYNuDepth 1]);
76RayPorjectImgMapYImCo = ((VertYNuDepth+1-RayProjY)-0.5)/VertYNuDepth - Oy;
77RayPorjectImgMapXImCo = (RayProjX-0.5)/HoriXNuDepth - Ox;
78% 3) Ray ready
79Ray = RayImPosition(RayPorjectImgMapYImCo,RayPorjectImgMapXImCo,a,b,Ox,Oy); %[ horiXSizeLowREs VertYSizeLowREs 3]
80Ray = permute(Ray,[3 1 2]);
81
82% Vertex X Y position
83VertexY = repmat((1:(VertYNuDepth+1))',[1 HoriXNuDepth+1]);
84VertexX = repmat((1:(HoriXNuDepth+1)),[VertYNuDepth+1 1]);
85 % change them into image coordinate
86Vertex = Matrix2ImgCo(HoriXNuDepth+1, VertYNuDepth+1, [VertexX(:) VertexY(:)]);
87VertexY = reshape(Vertex(:,2), VertYNuDepth+1, []);
88VertexX = reshape(Vertex(:,1), VertYNuDepth+1, []);
89
90% 4) Position 3D ready
91%Posi3D = im_cr2w_cr(LaserDepth,permute(Ray,[2 3 1]));
92
93% 5) Sup Ready
94load([ScratchDataFolder '/data/CleanSup/CleanSup' num2str(k) '.mat']);
95load([ScratchDataFolder '/Gridlaserdata/' depthfile '.mat']);
96LaserDepth = Position3DGrid(:,:,4);
97[SegVertYSize, SegHoriXSize] = size(MedSup);
98MedSup = double(MedSup);
99Sup = double(Sup);
100MaxSup = max( Sup(:));
101MaskSky = Sup ==0;
102
103% 6) Img Ready
104Img = imresize(imread([GeneralDataFolder '/' ImgFolder '/' filename{k} ],'jpg'), [SegVertYSize SegHoriXSize]);
105
106% 7) SupBounday with HashIndex Ready
107BoundaryPVert = conv2(Sup,[MaxSup 1],'valid').*( conv2(Sup,[1 -1],'valid') >0)...
108                .* ~MaskSky(:,1:(end-1)) .* ~MaskSky(:,2:end); % two step build up hash index 1) Left > Righ index
109BoundaryPVert = BoundaryPVert + conv2(Sup,[1 MaxSup],'valid').* (conv2(Sup,[-1 1],'valid') >0)...
110                .* ~MaskSky(:,1:(end-1)) .* ~MaskSky(:,2:end); % 2) Left < Righ index
111BoundaryPHori = conv2(Sup,[MaxSup; 1],'valid').* (conv2(Sup,[1; -1],'valid') >0)...
112                .* ~MaskSky(1:(end-1),:) .* ~MaskSky(2:end,:); % two step build up hash index 1) Top > bottom index
113BoundaryPHori = BoundaryPHori + conv2(Sup,[1; MaxSup],'valid').*( conv2(Sup,[-1; 1],'valid') >0)...
114                .* ~MaskSky(1:(end-1),:) .* ~MaskSky(2:end,:); % 2) Top < Bottom index
115ClosestNHashList = setdiff(unique([BoundaryPHori(:); BoundaryPVert(:)]),0);
116NuNei = size(ClosestNHashList,1);
117MaxHash = max(ClosestNHashList(:));
118Hash2Ind = sparse(1,MaxHash);
119Hash2Ind(ClosestNHashList) = 1:NuNei;
120
121% 8) Straight Line detection in Multi Scale
122[seglist]=edgeSegDetection(Img,k,0);
123
124figure(100); subplot(2,2,1);
125ImgMapSup = Img;
126ImgMapSup(:,:,1) = 255/max(Sup(:))*imresize(Sup, [ SegVertYSize SegHoriXSize]);
127image(ImgMapSup);
128drawseg(seglist,100);
129PlotGridBoundary( (BoundaryPHori ~=0), (BoundaryPVert ~=0), VertexX, VertexY, [ SegVertYSize SegHoriXSize], 100, 'y');
130% maping Straight line to BoundaryLineCross  matrix
131[BoundaryLineCrossHori BoundaryLineCrossVert] = ...
132    ST2BoundaryLineCross(seglist, BoundaryPHori, BoundaryPVert); % now seglist change to size of VertYNuDepth HoriXNuDepth
133    % same size of BoundaryPHori with
134
135% Show result of all possible Bunday and the Straighe line detected
136figure(100); subplot(2,2,1);
137%PlotGridBoundary( ones(size( BoundaryLineCrossHori)),ones(size(
138%BoundaryLineCrossVert)), VertexX, VertexY, [ SegVertYSize SegHoriXSize],
139%100,'y'); % plot all the boundary
140PlotGridBoundary( BoundaryLineCrossHori, BoundaryLineCrossVert, VertexX, VertexY, [ SegVertYSize SegHoriXSize], 100, 'g');
141
142% Constructing W matrix
143WL = sparse(NuNei,NuNei);
144WI = sparse(NuNei,NuNei);
145WIL = sparse(NuNei,NuNei);
146% 1) inital preference
147ii = 1:(size(BoundaryPVert,1)-1);
148jj = 1:(size(BoundaryPVert,2));
149mask = zeros(size(BoundaryPVert));
150mask(ii,jj) = 1;
151IndVert = find(mask);
152mask = zeros(size(BoundaryPHori));
153mask(ii,jj) = 1;
154IndHori = find(mask);
155ProximitySLHast = sort([[BoundaryPVert(IndVert) BoundaryPVert(IndVert+1)]; [BoundaryPHori(IndHori) BoundaryPHori(IndHori+size(BoundaryPHori,1))]],2);
156ProximitySLHast( (ProximitySLHast(:,1) == 0 | ProximitySLHast(:,2) == 0),:) = [];
157ProximityCornerHast = sort([[BoundaryPVert(IndVert) BoundaryPHori(IndHori)];...
158                            [BoundaryPVert(IndVert) BoundaryPHori(IndHori+size(BoundaryPHori,1))];...
159                            [BoundaryPVert(IndVert+1) BoundaryPHori(IndHori)];...
160                            [BoundaryPVert(IndVert+1) BoundaryPHori(IndHori+size(BoundaryPHori,1))]],2);
161ProximityCornerHast( (ProximityCornerHast(:,1) == 0 | ProximityCornerHast(:,2) == 0),:) = [];
162IndProximitySLHast = sub2ind([NuNei NuNei],Hash2Ind(ProximitySLHast(:,1)),Hash2Ind(ProximitySLHast(:,2)));
163IndProximityCornerHast = sub2ind([NuNei NuNei], Hash2Ind(ProximityCornerHast(:,1)), Hash2Ind(ProximityCornerHast(:,2)));
164WI( IndProximitySLHast) = -WSLInitialWei;
165WI( IndProximityCornerHast) = -WCornerInitialWei;
166WIL( IndProximitySLHast) = -WSLInitialWei;
167WIL( IndProximityCornerHast) = -WCornerInitialWei;
168
169% 2) Straight Line link preference
170NuSL = size(seglist,1);
171for l = 1:NuSL
172    IndLineCrossHori = find(BoundaryLineCrossHori == l);
173    IndLineCrossVert = find(BoundaryLineCrossVert == l);
174    % decide to shift hori or vert
175    if any(IndLineCrossVert == IndLineCrossVert+size(BoundaryLineCrossVert,1))
176       shiftHori = 1;
177       shiftVert = 1;
178    else
179       shiftHori = size(BoundaryPHori,1);
180       shiftVert = size(BoundaryPVert,1);
181    end
182    SLHashVert = BoundaryPVert(IndLineCrossVert);
183    SLHashVert( SLHashVert == 0) = [];
184    SLHashHori = BoundaryPHori(IndLineCrossHori);
185    SLHashHori( SLHashHori == 0) = [];
186%     too strong preference for constructing straight line
187%     across long dist
188%     [y x] = meshgrid([SLHashVert; SLHashHori],[SLHashVert;SLHashHori]);
189%     check = y == x;
190%     y( check) =[];
191%     x( check) =[];
192%     PairHash = unique(sort([y(:) x(:)],2),'rows');
193    SLHash = [SLHashVert; SLHashHori];
194    PairHash = unique(sort( [SLHash(1:(end-1)) SLHash(2:(end)) ], 2),'rows');
195    if isempty(PairHash)
196       continue;
197    end
198    PairHash( PairHash(:,1) == PairHash(:,2),:) = [];
199    if isempty(PairHash)
200       continue;
201    end
202    SLInd = sub2ind([NuNei NuNei], Hash2Ind(PairHash(:,1)), Hash2Ind(PairHash(:,2)));
203    WL( SLInd) = -1; % -1 if prefer same label
204    WIL( SLInd) = -1; % -1 if prefer same label
205%    ExludePairHash1 = [ [reshape(SLHashVert(:,ones(1,SLExcludeVertWei)),[],1); reshape(SLHashHori(:,ones(1,SLExcludeHoriWei)),[],1)] ...
206%                      [BoundaryPVert( reshape( max( min( repmat(IndLineCrossVert,[1 SLExcludeVertWei]) + ...
207%                       repmat(shiftVert*(1:SLExcludeVertWei),[size(IndLineCrossVert,1) 1]), prod(size(BoundaryPVert))), 1), [],1) ) ;...
208%                      BoundaryPHori( reshape( max( min( repmat(IndLineCrossHori,[1 SLExcludeHoriWei]) + ...
209%                       repmat(shiftHori*(1:SLExcludeHoriWei),[size(IndLineCrossHori,1) 1]), prod(size(BoundaryPHori))), 1), [],1) ) ]...
210%                      ];
211%    ExludePairHash1( (ExludePairHash1(:,1) == 0 | ExludePairHash1(:,2) == 0),:) = [];                     
212%    ExludePairHash2 = [[reshape(SLHashVert(:,ones(1,SLExcludeVertWei)),[],1); reshape(SLHashHori(:,ones(1,SLExcludeHoriWei)),[],1)]...
213%                      [BoundaryPVert( reshape( max( min( repmat(IndLineCrossVert,[1 SLExcludeVertWei]) + ...
214%                       repmat(-shiftVert*(1:SLExcludeVertWei),[size(IndLineCrossVert,1) 1]), prod(size(BoundaryPVert))), 1), [],1) ) ;...
215%                      BoundaryPHori( reshape( max( min( repmat(IndLineCrossHori,[1 SLExcludeHoriWei]) + ...
216%                       repmat(-shiftHori*(1:SLExcludeHoriWei),[size(IndLineCrossHori,1) 1]), prod(size(BoundaryPHori))), 1), [],1) ) ]...
217%                      ];
218%    ExludePairHash2( (ExludePairHash2(:,1) == 0 | ExludePairHash2(:,2) == 0),:) = [];
219%    ExludePairHash = unique(sort([ExludePairHash1; ExludePairHash2],2), 'rows');
220%    SLExludeInd = sub2ind([NuNei NuNei], Hash2Ind(ExludePairHash(:,1)), Hash2Ind(ExludePairHash(:,2)));
221%    DonNotExcludeInd = W(SLExludeInd) == 1;
222%    W( SLExludeInd(~DonNotExcludeInd)) = 1;
223end
224
225% 3) line complete and corner complete preference
226% still don't know
227
228% 4) parallel exlude
229NewHash2Ind =Hash2Ind;
230NewHash2Ind(end+1) = NuNei+1;
231NewBoundaryPVert = BoundaryPVert;
232NewBoundaryPVert( NewBoundaryPVert==0) = size(NewHash2Ind,2);
233SV = spalloc((size(NewBoundaryPVert,2)-(SLExcludeVertWei-1))*size(NewBoundaryPVert,1),NuNei+1,...
234            (size(NewBoundaryPVert,2)-(SLExcludeVertWei-1))*size(NewBoundaryPVert,1)*SLExcludeVertWei);
235InitialRow = 1;
236for i = 1:SLExcludeVertWei
237    ResV = rem(size(BoundaryPVert,2)-i+1,SLExcludeVertWei);
238    XI = NewHash2Ind(reshape( NewBoundaryPVert(:,i:(end-ResV) )', SLExcludeVertWei, [] ));
239    NuRow = size(XI,2);
240    YI = repmat(InitialRow:(InitialRow+NuRow-1), [SLExcludeVertWei 1]);
241    InitialRow = InitialRow+NuRow;
242    SV(sub2ind(size(SV),YI(:),XI(:))) = 1;
243end   
244SV(:,end) = [];
245mask = sum(SV,2) ==0;
246SV(mask,:) = [];
247
248NewBoundaryPHori = BoundaryPHori;
249NewBoundaryPHori( NewBoundaryPHori==0) = size(NewHash2Ind,2);
250SH = spalloc((size(NewBoundaryPHori,1)-(SLExcludeHoriWei-1))*size(NewBoundaryPHori,2),NuNei+1,...
251            (size(NewBoundaryPHori,1)-(SLExcludeHoriWei-1))*size(NewBoundaryPHori,2)*SLExcludeHoriWei);
252InitialRow = 1;
253for i = 1:SLExcludeHoriWei
254    ResH = rem(size(BoundaryPHori,1)-i+1,SLExcludeHoriWei);
255    XI = NewHash2Ind(reshape( NewBoundaryPHori(i:(end-ResH),: ), SLExcludeHoriWei, [] ));
256    NuRow = size(XI,2);
257    YI = repmat(InitialRow:(InitialRow+NuRow-1), [SLExcludeHoriWei 1]);
258    InitialRow = InitialRow+NuRow;
259    SH(sub2ind(size(SH),YI(:),XI(:))) = 1;
260end
261SH(:,end) = [];
262mask = sum(SH,2) ==0;
263SH(mask,:) = [];
264
265% make sure diagnal terms are 1
266DiInd = sub2ind([NuNei NuNei],(1:NuNei)',(1:NuNei)');
267WI = WI+WI'; % make symmetric
268WL = WL+WL'; % make symmetric
269WIL = WIL+WIL'; % make symmetric
270WI(DiInd) = 1;
271WL(DiInd) = 1;
272WIL(DiInd) = 1;
273
274% maping Laser occlusion estimation to Bp matrix
275[Bp, OccluMap, BoundaryLaserOccluHori, BoundaryLaserOccluVert] = LaserDetectOcc2Bp( LaserDepth, BpWidthV, BpWidthH, BoundaryPVert, BoundaryPHori, Hash2Ind);
276%[BoundaryLaserOccluHori, BoundaryLaserOccluVert ] = BHash2BMap(Bp, BoundaryPHori, BoundaryPVert, ClosestNHashList); % output two binary mask
277
278% Show Laser occlusion detection labeled Boundary
279figure(100); subplot(2,2,2);
280ImgMapOcclu = Img;
281ImgMapOcclu(:,:,1) = 255*imresize(OccluMap, [ SegVertYSize SegHoriXSize]);
282image(ImgMapOcclu);
283PlotGridBoundary( BoundaryLaserOccluHori, BoundaryLaserOccluVert, VertexX, VertexY, [ SegVertYSize SegHoriXSize], 100,'g');
284save([ScratchDataFolder '/Temp/OccluMRF' num2str(k) '.mat']);
285
286% Run MRF
287[U S V] = svds(WI);
288Wsqrt = (S.^(0.5))*V';
289opt = sdpsettings('solver','sedumi');
290B = sdpvar(NuNei,1);
291t = sdpvar(1,1);
292%F = set( -1 < B < 1) + set(cone(Wsqrt*B,t));
293F = set( -1 < B < 1) + set(cone(Wsqrt*B,t))+set(SV*B <= (-sum(SV,2)+2) )+set(SH*B <= (-sum(SH,2)+2) );
294beta = 1; % trade off weight
295sol = solvesdp(F,t*t - beta*Bp'*B ,opt);
296B = double(B);
297toc
298% beta = 1;
299% cvx_begin
300%     variable B(NuNei);
301%     minimize(B'*WI*B - beta*Bp'*B)
302%     abs(B) <= 1;
303%     SV*B <= (-sum(SV,2)+2);
304%     SH*B <= (-sum(SH,2)+2);
305% cvx_end
306[BoundaryMRFEstHori, BoundaryMRFEstVert ] = BHash2BMap(B, BoundaryPHori, BoundaryPVert, ClosestNHashList); % output two binary mask
307
308% Show Laser occlusion detection labeled Boundary
309figure(100); subplot(2,2,3);
310image(ImgMapOcclu);
311PlotGridBoundary( BoundaryMRFEstHori, BoundaryMRFEstVert, VertexX, VertexY,[ SegVertYSize SegHoriXSize], 100,'y');
312saveas(100,[ScratchDataFolder '/data/occlu/MRFoccl' num2str(k) '.jpg']);
313close all;
Note: See TracBrowser for help on using the repository browser.