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

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

Added original make3d

File size: 9.2 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 []=FindOccluSnap(k, Ray, RayPorjectImgMapX, RayPorjectImgMapY) % for each image
40
41global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory...
42    ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default...
43    Horizon_default filename batchSize NuRow_default SegVertYSize SegHoriXSize WeiBatchSize PopUpVertY PopUpHoriX taskName;
44
45% inital parameter setting
46ThreSize = 10;
47StickHori = 0;
48StickVert = 0;
49ClosestDist = 1;
50FarestDist = 80;
51ThreUpDown = 0.5;
52ThreOcclu = 1;
53SE = strel('disk',3);
54SE5 = strel('disk',5);
55ThreVert = 0.5;
56ThreHori = 0.5;
57ThreFar = 15;
58ThrePcik = 0.5;
59ThreOut = 0.3;
60
61% get all the data we need laserDepth Sup MedSup
62depthfile = strrep(filename{k},'img','depth_sph_corr'); % the depth filename
63load([ScratchDataFolder '/Gridlaserdata/' depthfile '.mat']);
64LaserDepth = Position3DGrid(:,:,4);
65clear Position3DGrid;
66 % load the CleanedSup
67load([ScratchDataFolder '/data/CleanSup/CleanSup' num2str(k) '.mat']);         
68[SegVertYSize, SegHoriXSize] = size(MedSup);
69MedSup = double(MedSup);
70MaxMedSup = max(MedSup(:));
71Sup = double(Sup);
72 % generate the Posi3D
73Posi3D = im_cr2w_cr(LaserDepth,permute(Ray,[2 3 1]));
74 % generate the RayPorjectImgMapY and RayPorjectImgMapX to resolution as MedSup
75NewRayPorjectImgMap = Matrix2ImgCo(HoriXNuDepth, VertYNuDepth, [RayPorjectImgMapX(:) RayPorjectImgMapY(:)]);
76NewRayPorjectImgMap = ImgCo2Matrix(SegHoriXSize, SegVertYSize, NewRayPorjectImgMap);
77NewRayPorjectImgMap = reshape(NewRayPorjectImgMap',[2 VertYNuDepth HoriXNuDepth]);
78 % read in img
79Img = imresize(imread([GeneralDataFolder '/' ImgFolder '/' filename{k} ],'jpg'), [SegVertYSize SegHoriXSize]);
80
81% generate Straight lines
82[seglist]=edgeSegDetection(Img,k,0);
83Pointer = seglist(:,1) > seglist(:,3);
84temp = seglist(Pointer,1:2);
85seglist(Pointer,1:2) = seglist(Pointer,3:4);
86seglist(Pointer,3:4) = temp;
87edgeIm = zeros(SegVertYSize, SegHoriXSize);
88NuSeg =size(seglist,1);
89for l = 1:NuSeg
90    l
91    x = (floor(seglist(l,1)):ceil(seglist(l,3)))';
92    if seglist(l,1) ~= seglist(l,3)
93       y = round(LineProj(seglist(l,:), x , []));
94    else
95        if seglist(l,2) < seglist(l,4)
96           y = (floor(seglist(l,2)):ceil(seglist(l,4)))';
97        else
98           y = (floor(seglist(l,4)):ceil(seglist(l,2)))' ;
99        end
100        x = round(LineProj(seglist(l,:), [] , y));
101    end
102    index = sub2ind([SegVertYSize SegHoriXSize],y,x);
103    edgeIm(index) = l;
104   
105    if seglist(l,2) < seglist(l,4)
106           y = (floor(seglist(l,2)):ceil(seglist(l,4)))';
107    else
108           y = (floor(seglist(l,4)):ceil(seglist(l,2)))' ;
109    end
110    if seglist(l,2) ~= seglist(l,4)
111      x = round(LineProj(seglist(l,:), [] , y));
112    else
113      x = (floor(seglist(l,1)):ceil(seglist(l,3)))';
114      y = round(LineProj(seglist(l,:), x , []));
115    end
116    index = sub2ind([SegVertYSize SegHoriXSize],y,x);
117    edgeIm(index) = l;
118end
119figure(400); imagesc(edgeIm);
120
121% Find Spatial Jump Point
122DiffDepthVert = abs(conv2(LaserDepth,[1; -1],'valid'));
123DiffDepthHori = abs(conv2(LaserDepth,[1 -1],'valid'));
124FraDiffDepthVert = DiffDepthVert./ sqrt(LaserDepth(1:(end-1),:) .* LaserDepth(2:end,:) );
125%FraDiffDepthVert = DiffDepthVert./ min(LaserDepth(1:(end-1),:) , LaserDepth(2:end,:) );
126OccFraDiffDepthVert = FraDiffDepthVert > ThreVert;
127OccFraDiffDepthVert(LaserDepth(1:(end-1),:) > ThreFar & LaserDepth(2:end,:) > ThreFar) = 0;
128FraDiffDepthHori = DiffDepthHori./ sqrt( LaserDepth(:,1:(end-1)) .* LaserDepth(:,2:end) );
129%FraDiffDepthHori = DiffDepthHori./ min( LaserDepth(:,1:(end-1)) , LaserDepth(:,2:end) );
130
131OccFraDiffDepthHori = FraDiffDepthHori > ThreHori;
132OccFraDiffDepthHori(LaserDepth(:,1:(end-1)) > ThreFar & LaserDepth(:,2:end) > ThreFar) = 0;
133OccFarDiffDepthMask = zeros(VertYNuDepth, HoriXNuDepth);
134OccFarDiffDepthMask(:,1:(end-1)) = OccFraDiffDepthHori;
135OccFarDiffDepthMask(:,2:(end)) = OccFarDiffDepthMask(:,2:end) | OccFraDiffDepthHori;
136OccFarDiffDepthMask(1:(end-1),:) = OccFarDiffDepthMask(1:(end-1),:) | OccFraDiffDepthVert;
137OccFarDiffDepthMask(2:(end),:) = OccFarDiffDepthMask(2:end,:) | OccFraDiffDepthVert;
138figure(1000);
139subplot(3,2,1);
140TempDepth = LaserDepth;
141TempDepth(logical(OccFarDiffDepthMask)) =-10;
142imagesc(TempDepth);
143title('LaserDepthMap');
144figure(1000);
145subplot(3,2,2);
146ii = Img;
147ii(:,:,1) = 255*logical(edgeIm);
148image(ii);
149title('edgeDetected');
150
151% show all the possible candidate pairs
152MedBoundaryPHori = conv2(MedSup,[1 -1],'same') ~=0;
153MedBoundaryPHori(:,end) = 0;
154MedBoundaryPVert = conv2(MedSup,[1; -1],'same') ~=0;
155MedBoundaryPVert(end,:) = 0;
156MedBoundaryP = MedBoundaryPVert |MedBoundaryPHori;
157ClosestNList = [ MedSup(find(MedBoundaryPHori==1)) MedSup(find(MedBoundaryPHori==1)+SegVertYSize);...
158                 MedSup(find(MedBoundaryPVert==1)) MedSup(find(MedBoundaryPVert==1)+1)];
159ClosestNList = sort(ClosestNList,2);
160ClosestNList = unique(ClosestNList,'rows');
161ClosestNList(ClosestNList(:,1) == 0,:) = [];
162NuPair = size(ClosestNList,1)
163%generate the boundary index mask
164BoundaryIndexMask = zeros(SegVertYSize, SegHoriXSize);
165HashList = ClosestNList(:,1)*MaxMedSup+ClosestNList(:,2);
166lH = find(MedBoundaryPHori);
167j = lH + SegVertYSize;
168HashBoundaryHori = sort([MedSup(lH) MedSup(j)],2);
169HashBoundaryHori = HashBoundaryHori(:,1)*MaxMedSup + HashBoundaryHori(:,2);
170lV = find(MedBoundaryPVert);
171j = lV + 1;
172HashBoundaryVert = sort([MedSup(lV) MedSup(j)],2);
173HashBoundaryVert = HashBoundaryVert(:,1)*MaxMedSup + HashBoundaryVert(:,2);
174for m = 1:size(HashList,1)
175    Pointer = HashBoundaryHori == HashList(m);
176    BoundaryIndexMask(lH(Pointer)) = m;
177    Pointer = HashBoundaryVert == HashList(m);
178    BoundaryIndexMask(lV(Pointer)) = m;
179end
180
181figure(200); imagesc(BoundaryIndexMask);
182
183if false
184figure(300); ii = Img;
185ii(:,:,1) = 255*MedBoundaryP;
186image(ii);
187hold on;
188scatter(NewRayPorjectImgMap(1,logical(OccFarDiffDepthMask)),NewRayPorjectImgMap(2,logical(OccFarDiffDepthMask)),100,LaserDepth(logical(OccFarDiffDepthMask)));
189hold off;
190end
191
192OccFarDiffDepthMask = imresize(OccFarDiffDepthMask, size(MedBoundaryP) );
193
194Z_Mask = (MedBoundaryP .* imdilate(OccFarDiffDepthMask, SE5) ) | ( logical(edgeIm) .* imdilate(OccFarDiffDepthMask, SE5) );
195temp_mask = (MedBoundaryP .* imdilate(OccFarDiffDepthMask, SE5) );
196Mask_2nd = ( logical(edgeIm) .* imdilate(OccFarDiffDepthMask, SE5) );
197
198%comlete the boundary
199ComPMask = Z_Mask;
200for l = 1:NuPair
201    mask = BoundaryIndexMask == l;
202    AllSize = sum(mask(:));
203    AndMask = Z_Mask & mask;
204    AndSize = sum(AndMask(:));
205    PerCent = AndSize/AllSize;
206    if PerCent > ThrePcik
207       ComPMask(mask) = 1;
208    elseif PerCent < ThreOut
209       ComPMask(mask) = 0;
210    end
211end
212
213% link CompMAsk
214[seglistNew ]=edgeSegDetection_new(ComPMask,k,1, 5, 3, 0.01, 3, 20);
215
216figure(1000); ii = Img;
217subplot(3,2,3);
218ii(:,:,1) = 255*MedBoundaryP;
219ii(:,:,3) = 255*OccFarDiffDepthMask;
220image(ii);
221title('Dilate_Occlu');
222hold on;
223
224figure(700); ii = Img;
225ii(:,:,1) = 255*temp_mask;
226image(ii);
227hold on;
228figure(800); ii = Img;
229ii(:,:,1) = 255*Mask_2nd;
230image(ii);
231hold on;
232
233figure(600); ii = Img;
234ii(:,:,1) = 255*Z_Mask;
235image(ii);
236hold on;
237
238figure(1000);
239subplot(3,2,4);
240ii = Img;
241ii(:,:,1) = 255*ComPMask;
242image(ii);
243title('Complete_Occlu');
244hold on;
245
246figure(1000);
247subplot(3,2,5);
248image(Img);
249drawseg(seglistNew,1000);
250title('LinkComplete');
251
252saveas(1000,[ScratchDataFolder '/data/occlu/' filename{k} 'occlu_comp50.fig'],'fig');
253%scatter(NewRayPorjectImgMap(1,logical(OccFarDiffDepthMask)),NewRayPorjectImgMap(2,logical(OccFarDiffDepthMask)),100,LaserDepth(logical(OccFarDiffDepthMask)));
254hold off;
255
256return;
Note: See TracBrowser for help on using the repository browser.