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 | % */
|
---|
39 | function [vrml_filename] = vrml_test_faceset_triangle(filename,Position3D,depth,ray,DepthDirectory,HBrokeBook,VBrokeBook,Grid,maskSky,Np,Sep,a,b,Ox,Oy,Thresh) |
---|
40 | %function [] = vrml_test_faceset_triangle(filename,PlaneParameterTure,LowResImgIndexSuperpixelSep,DepthDirectory,a,b,Ox,Oy) |
---|
41 | % this function play with the VRML |
---|
42 | % using only FaceSet |
---|
43 | % % global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory... |
---|
44 | % % ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default... |
---|
45 | % % Horizon_default batchSize NuRow_default SegVertYSize SegHoriXSize WeiBatchSize; |
---|
46 | |
---|
47 | if nargin < 12 |
---|
48 | a = 0.70783777 %0.129; % horizontal physical size of image plane normalized to focal length (in meter) |
---|
49 | b = 0.946584169%0.085; % vertical physical size of image plane normalized to focal length (in meter) |
---|
50 | Ox = -0.010727086; % camera origin offset from the image center in horizontal direction |
---|
51 | Oy = -0.0111130176; % camera origin offset from the image center in vertical direction |
---|
52 | elseif nargin < 13 |
---|
53 | b = a; |
---|
54 | Ox = -0.010727086; % camera origin offset from the image center in horizontal direction |
---|
55 | Oy = -0.0111130176; % camera origin offset from the image center in vertical direction |
---|
56 | elseif nargin < 14 |
---|
57 | Ox = -0.010727086; % camera origin offset from the image center in horizontal direction |
---|
58 | Oy = -0.0111130176; % camera origin offset from the image center in vertical direction |
---|
59 | elseif nargin < 15 |
---|
60 | Oy = Ox; |
---|
61 | end |
---|
62 | |
---|
63 | % define global variable |
---|
64 | global GeneralDataFolder ScratchDataFolder LocalFolder; |
---|
65 | |
---|
66 | vrml_filename = ['tri_' filename '_' 'flipTri4.wrl']; |
---|
67 | %LowResImgIndexSuperpixelSepTemp = LowResImgIndexSuperpixelSep; |
---|
68 | %[VertYSize HoriXSize] = size(LowResImgIndexSuperpixelSepTemp); |
---|
69 | [dum VertYSize HoriXSize] = size(Position3D); |
---|
70 | %nu_patch = VertYSize* HoriXSize; |
---|
71 | |
---|
72 | Position3DCoord = reshape(Position3D,3,VertYSize*HoriXSize); |
---|
73 | Position3DCoord(3,:) = -Position3DCoord(3,:); % important to make z direction negative |
---|
74 | |
---|
75 | table = reshape(0:(VertYSize*HoriXSize-1),VertYSize,[]); |
---|
76 | Position3DCoordOrdeerIndex = []; |
---|
77 | GridPosition3DCoordOrdeerIndex = []; |
---|
78 | |
---|
79 | countPoints=length(Position3DCoord)+1; |
---|
80 | unassignedTargets = 0; |
---|
81 | for i = 1:VertYSize-1 |
---|
82 | for j = 1:HoriXSize-1 |
---|
83 | % check which diagnoal to render |
---|
84 | target = [HBrokeBook(i,j) VBrokeBook(i,j+1) HBrokeBook(i+1,j) VBrokeBook(i,j)]; |
---|
85 | clear Index; |
---|
86 | clear GridIndex; |
---|
87 | clear indexArray; |
---|
88 | |
---|
89 | if (all(target == [0 0 0 0]) || all(target == [0 0 1 1]) || all(target == [1 1 0 0])) |
---|
90 | Index = [ table(i,j) table(i+1,j) table(i+1,j+1) table(i,j) table(i+1,j+1) table(i,j+1)]; |
---|
91 | GridIndex = [table(i,j) table(i+1,j) table(i+1,j+1) table(i,j) table(i,j) table(i+1,j+1) table(i,j+1) table(i,j)]; |
---|
92 | indexArray = [i i+1 i+1 i i+1 i; ... |
---|
93 | j j j+1 j j+1 j+1]; |
---|
94 | elseif (all(target == [1 0 0 1]) || all(target == [0 1 1 0])) |
---|
95 | Index = [ table(i,j) table(i+1,j) table(i,j+1) table(i+1,j) table(i+1,j+1) table(i,j+1)]; |
---|
96 | GridIndex = [ table(i,j) table(i+1,j) table(i,j+1) table(i,j) table(i+1,j) table(i+1,j+1) table(i,j+1) table(i+1,j)]; |
---|
97 | indexArray = [i i+1 i i+1 i+1 i; ... |
---|
98 | j j j+1 j j+1 j+1]; |
---|
99 | else |
---|
100 | unassignedTargets=unassignedTargets+1; |
---|
101 | Index = [ table(i,j) table(i+1,j) table(i+1,j+1) table(i,j) table(i+1,j+1) table(i,j+1)]; |
---|
102 | GridIndex = [table(i,j) table(i+1,j) table(i+1,j+1) table(i,j) table(i,j) table(i+1,j+1) table(i,j+1) table(i,j)]; |
---|
103 | indexArray = [i i+1 i+1 i i+1 i; ... |
---|
104 | j j j+1 j j+1 j+1]; |
---|
105 | end |
---|
106 | |
---|
107 | % Sky Exclude |
---|
108 | if maskSky(i,j)==1 |
---|
109 | %% check if the neighbors are sky as well, for Np length |
---|
110 | if (j+(Np-1))>HoriXSize |
---|
111 | jL=HoriXSize; |
---|
112 | else |
---|
113 | jL=j+(Np-1); |
---|
114 | end |
---|
115 | if (i+(Np-1))>VertYSize |
---|
116 | iL=VertYSize; |
---|
117 | else |
---|
118 | iL=i+(Np-1); |
---|
119 | end |
---|
120 | if ((sum(xor(maskSky(i,j:jL),ones(1,length(maskSky(i,j:jL)))))>0)||... |
---|
121 | sum(xor(maskSky(i:iL,j),ones(length(maskSky(i:iL,j)),1)))>0) |
---|
122 | %% also update the depth values for all the 6 points by |
---|
123 | %% changing them to a value of any non-sky point in the |
---|
124 | %% vicinity of Np down and to the right |
---|
125 | |
---|
126 | %% so first we find the point non-sky point which made us |
---|
127 | %% include this sky point |
---|
128 | iRep=i; |
---|
129 | indexVec=j:jL; |
---|
130 | nonSkyPoints=find(maskSky(i,indexVec)==0); |
---|
131 | if length(nonSkyPoints)>0 |
---|
132 | jRep=indexVec(nonSkyPoints(1)); |
---|
133 | else |
---|
134 | jRep=j; |
---|
135 | indexVec=i:iL; |
---|
136 | nonSkyPoints=find(maskSky(indexVec,j)==0); |
---|
137 | iRep=indexVec(nonSkyPoints(1)); |
---|
138 | end |
---|
139 | iChoose=iRep; |
---|
140 | jChoose=jRep; |
---|
141 | firstTriPoints=[]; |
---|
142 | num=1; |
---|
143 | [firstTriPoints,Position3DCoord, depth]=removeSkyPointsDepth(maskSky,indexArray,num,firstTriPoints,ray,depth,VertYSize,HoriXSize,Position3DCoord,iChoose,jChoose); |
---|
144 | max_or_second_max=1; |
---|
145 | [countPoints, Position3DCoord, Index, GridIndex, depth]=removeOffsetPointsReplaceDepth(firstTriPoints, Thresh, num, max_or_second_max, ray, indexArray, depth, ... |
---|
146 | VertYSize, HoriXSize, Position3DCoord, Index, GridIndex, countPoints); |
---|
147 | max_or_second_max=2; |
---|
148 | [countPoints, Position3DCoord, Index, GridIndex, depth]=removeOffsetPointsReplaceDepth(firstTriPoints, Thresh, num, max_or_second_max, ray, indexArray, depth, ... |
---|
149 | VertYSize, HoriXSize, Position3DCoord, Index, GridIndex, countPoints); |
---|
150 | % |
---|
151 | secondTriPoints=[]; |
---|
152 | num=2; |
---|
153 | [secondTriPoints,Position3DCoord, depth]=removeSkyPointsDepth(maskSky,indexArray,num,secondTriPoints,ray,depth,VertYSize,HoriXSize,Position3DCoord,iChoose,jChoose); |
---|
154 | max_or_second_max=1; |
---|
155 | [countPoints, Position3DCoord, Index, GridIndex, depth]=removeOffsetPointsReplaceDepth(secondTriPoints, Thresh, num, max_or_second_max, ray, indexArray, depth, ... |
---|
156 | VertYSize, HoriXSize, Position3DCoord, Index, GridIndex, countPoints); |
---|
157 | max_or_second_max=2; |
---|
158 | [countPoints, Position3DCoord, Index, GridIndex, depth]=removeOffsetPointsReplaceDepth(secondTriPoints, Thresh, num, max_or_second_max, ray, indexArray, depth, ... |
---|
159 | VertYSize, HoriXSize, Position3DCoord, Index, GridIndex, countPoints); |
---|
160 | % |
---|
161 | Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index]; |
---|
162 | GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex]; |
---|
163 | % if ((max(Index(1:3))<16775)&&(max(GridIndex(1:4))<16775)) |
---|
164 | % Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index(1:3)]; |
---|
165 | % GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex(1:4)]; |
---|
166 | % end |
---|
167 | % if ((max(Index(4:6))<16775)&&(max(GridIndex(5:8))<16775)) |
---|
168 | % Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index(4:6)]; |
---|
169 | % GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex(5:8)]; |
---|
170 | % end |
---|
171 | end |
---|
172 | else |
---|
173 | iChoose=i; |
---|
174 | jChoose=j; |
---|
175 | firstTriPoints=[]; |
---|
176 | num=1; |
---|
177 | [firstTriPoints,Position3DCoord, depth]=removeSkyPointsDepth(maskSky,indexArray,num,firstTriPoints,ray,depth,VertYSize,HoriXSize,Position3DCoord,iChoose,jChoose); |
---|
178 | max_or_second_max=1; |
---|
179 | [countPoints, Position3DCoord, Index, GridIndex, depth]=removeOffsetPointsReplaceDepth(firstTriPoints, Thresh, num, max_or_second_max, ray, indexArray, depth, ... |
---|
180 | VertYSize, HoriXSize, Position3DCoord, Index, GridIndex, countPoints); |
---|
181 | max_or_second_max=2; |
---|
182 | [countPoints, Position3DCoord, Index, GridIndex, depth]=removeOffsetPointsReplaceDepth(firstTriPoints, Thresh, num, max_or_second_max, ray, indexArray, depth, ... |
---|
183 | VertYSize, HoriXSize, Position3DCoord, Index, GridIndex, countPoints); |
---|
184 | % |
---|
185 | secondTriPoints=[]; |
---|
186 | num=2; |
---|
187 | [secondTriPoints,Position3DCoord, depth]=removeSkyPointsDepth(maskSky,indexArray,num,secondTriPoints,ray,depth,VertYSize,HoriXSize,Position3DCoord,iChoose,jChoose); |
---|
188 | max_or_second_max=1; |
---|
189 | [countPoints, Position3DCoord, Index, GridIndex, depth]=removeOffsetPointsReplaceDepth(secondTriPoints, Thresh, num, max_or_second_max, ray, indexArray, depth, ... |
---|
190 | VertYSize, HoriXSize, Position3DCoord, Index, GridIndex, countPoints); |
---|
191 | max_or_second_max=2; |
---|
192 | [countPoints, Position3DCoord, Index, GridIndex, depth]=removeOffsetPointsReplaceDepth(secondTriPoints, Thresh, num, max_or_second_max, ray, indexArray, depth, ... |
---|
193 | VertYSize, HoriXSize, Position3DCoord, Index, GridIndex, countPoints); |
---|
194 | % |
---|
195 | Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index]; |
---|
196 | GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex]; |
---|
197 | |
---|
198 | % if ((max(Index(1:3))<16775)&&(max(GridIndex(1:4))<16775)) |
---|
199 | % Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index(1:3)]; |
---|
200 | % GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex(1:4)]; |
---|
201 | % end |
---|
202 | % if ((max(Index(4:6))<16775)&&(max(GridIndex(5:8))<16775)) |
---|
203 | % Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index(4:6)]; |
---|
204 | % GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex(5:8)]; |
---|
205 | % end |
---|
206 | end |
---|
207 | % Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index]; |
---|
208 | % GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex]; |
---|
209 | end |
---|
210 | end |
---|
211 | |
---|
212 | % calculate PositionTexCoord |
---|
213 | PositionTex = permute(ray(:,:,1:2)./repmat(cat(3,a,b),[VertYSize HoriXSize 1])+0.5,[3 1 2]); |
---|
214 | PositionTexCoord = PositionTex; |
---|
215 | |
---|
216 | Xmin=min(Position3DCoord(1,:))-Sep; |
---|
217 | Xmax=max(Position3DCoord(1,:))+Sep; |
---|
218 | Ymin=min(Position3DCoord(2,:))-Sep; |
---|
219 | Ymax=max(Position3DCoord(2,:))+Sep; |
---|
220 | Zmin=min(Position3DCoord(3,:))-Sep; |
---|
221 | Zmax=min(Position3DCoord(3,:))+Sep; |
---|
222 | %% the eight corners of the cube would then be |
---|
223 | cc=[]; |
---|
224 | cc = [cc [Xmin Ymin Zmin]']; %% 1 |
---|
225 | cc = [cc [Xmax Ymin Zmin]']; %% 2 |
---|
226 | cc = [cc [Xmin Ymin Zmax]']; %% 3 |
---|
227 | cc = [cc [Xmax Ymin Zmax]']; %% 4 |
---|
228 | |
---|
229 | cc = [cc [Xmin Ymax Zmin]']; %% 5 |
---|
230 | cc = [cc [Xmax Ymax Zmin]']; %% 6 |
---|
231 | cc = [cc [Xmin Ymax Zmax]']; %% 7 |
---|
232 | cc = [cc [Xmax Ymax Zmax]']; %% 8 |
---|
233 | |
---|
234 | % wallIndices = [front_wall right_wall back_wall left_wall]; |
---|
235 | wallIndices = [2 4 8 6 4 8 7 3 3 7 5 1]-1; |
---|
236 | |
---|
237 | % inital header |
---|
238 | disp('writing vrml..'); |
---|
239 | fp = fopen([ScratchDataFolder '/vrml/' vrml_filename],'w'); |
---|
240 | |
---|
241 | fprintf(fp, '#VRML V2.0 utf8\n'); |
---|
242 | |
---|
243 | % add navigate_info |
---|
244 | fprintf(fp, 'NavigationInfo {\n'); |
---|
245 | fprintf(fp, ' headlight TRUE\n'); |
---|
246 | fprintf(fp, ' type ["FLY", "ANY"]}\n\n'); |
---|
247 | |
---|
248 | % add viewpoint |
---|
249 | fprintf(fp, 'Viewpoint {\n'); |
---|
250 | fprintf(fp, ' position 0 0.0 0.0\n'); |
---|
251 | fprintf(fp, ' orientation 0 0 0 0\n'); |
---|
252 | fprintf(fp, ' fieldOfView 0.7\n'); |
---|
253 | fprintf(fp, ' description "Original"}\n'); |
---|
254 | |
---|
255 | % add Shape for texture faceset |
---|
256 | fprintf(fp, 'Shape{\n'); |
---|
257 | fprintf(fp, ' appearance Appearance {\n'); |
---|
258 | fprintf(fp, [' texture ImageTexture { url "./image/' filename '.jpg' '" }\n']); |
---|
259 | fprintf(fp, ' }\n'); |
---|
260 | fprintf(fp, ' geometry IndexedFaceSet {\n'); |
---|
261 | fprintf(fp, ' coord Coordinate {\n'); |
---|
262 | |
---|
263 | % insert coordinate in 3d |
---|
264 | % ======================= |
---|
265 | fprintf(fp, ' point [ \n'); |
---|
266 | fprintf(fp, ' %.2f %.2f %.2f,\n',Position3DCoord); |
---|
267 | fprintf(fp, ' ]\n'); |
---|
268 | fprintf(fp, ' }\n'); |
---|
269 | |
---|
270 | % insert coordinate index in 3d |
---|
271 | fprintf(fp, ' coordIndex [\n'); |
---|
272 | fprintf(fp, ' %g %g %g -1,\n',Position3DCoordOrdeerIndex); |
---|
273 | fprintf(fp, ' ]\n'); |
---|
274 | |
---|
275 | % insert texture coordinate |
---|
276 | fprintf(fp, ' texCoord TextureCoordinate {\n'); |
---|
277 | fprintf(fp, ' point [\n'); |
---|
278 | fprintf(fp, ' %.4g %.4g,\n',PositionTexCoord); |
---|
279 | fprintf(fp, ' ]\n'); |
---|
280 | fprintf(fp, ' }\n'); |
---|
281 | fprintf(fp, ' }\n'); |
---|
282 | fprintf(fp, '}\n'); |
---|
283 | % %% Now we make the wall |
---|
284 | % |
---|
285 | % % add Shape for texture faceset |
---|
286 | % fprintf(fp, 'Shape{\n'); |
---|
287 | % fprintf(fp, ' appearance Appearance {\n'); |
---|
288 | % %% one can change the sky color here, at the moment its a shade of blue |
---|
289 | % %% given as [0.31 0.54 0.76] |
---|
290 | % fprintf(fp, ' material Material { emissiveColor 0.31 0.54 0.76 }\n'); |
---|
291 | % fprintf(fp, ' }\n'); |
---|
292 | % fprintf(fp, ' geometry IndexedFaceSet {\n'); |
---|
293 | % fprintf(fp, ' coord Coordinate {\n'); |
---|
294 | % |
---|
295 | % % insert coordinate in 3d |
---|
296 | % % ======================= |
---|
297 | % fprintf(fp, ' point [ \n'); |
---|
298 | % fprintf(fp, ' %.2f %.2f %.2f,\n',cc); |
---|
299 | % fprintf(fp, ' ]\n'); |
---|
300 | % fprintf(fp, ' }\n'); |
---|
301 | % |
---|
302 | % % insert coordinate index in 3d |
---|
303 | % fprintf(fp, ' coordIndex [\n'); |
---|
304 | % %Position3DCoordOrdeerIndex = 0:(size(Position3DCoord,2)-1); |
---|
305 | % fprintf(fp, ' %g %g %g %g -1,\n',wallIndices); |
---|
306 | % fprintf(fp, ' ]\n'); |
---|
307 | % |
---|
308 | % % ================================== |
---|
309 | % fprintf(fp, ' }\n'); |
---|
310 | % fprintf(fp, '}\n'); |
---|
311 | |
---|
312 | if Grid == 1 |
---|
313 | % ======================================== |
---|
314 | fprintf(fp, 'Shape{\n'); |
---|
315 | fprintf(fp, ' appearance Appearance { material Material {emissiveColor 1 0 0 }}\n'); |
---|
316 | fprintf(fp, ' geometry IndexedLineSet {\n'); |
---|
317 | fprintf(fp, ' coord Coordinate {\n'); |
---|
318 | fprintf(fp, ' point [ \n'); |
---|
319 | fprintf(fp, ' %.2f %.2f %.2f,\n',Position3DCoord); |
---|
320 | fprintf(fp, ' ]\n'); |
---|
321 | fprintf(fp, ' }\n'); |
---|
322 | fprintf(fp, ' coordIndex [\n'); |
---|
323 | fprintf(fp, ' %g %g %g %g -1,\n',GridPosition3DCoordOrdeerIndex); |
---|
324 | fprintf(fp, ' ]\n'); |
---|
325 | %fprintf(fp, ' color Color { color [1 0 0, 0 1 0, 0 0 1]}\n'); |
---|
326 | %fprintf(fp, ' colorIndex [0 0 0 0 0 ]\n'); |
---|
327 | fprintf(fp, ' colorPerVertex FALSE\n'); |
---|
328 | fprintf(fp, ' }\n'); |
---|
329 | %================================== |
---|
330 | fprintf(fp, ' }\n'); |
---|
331 | fprintf(fp, '}\n'); |
---|
332 | end |
---|
333 | % close the file |
---|
334 | fclose(fp); |
---|
335 | |
---|