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_grid(filename,Position3D,depth,ray,DepthDirectory,HBrokeBook,VBrokeBook,Grid,maskSky,maskG, Np,Sep,a,b,Ox,Oy) |
---|
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 | |
---|
44 | displayFlag = true; |
---|
45 | |
---|
46 | global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory... |
---|
47 | ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default... |
---|
48 | Horizon_default batchSize NuRow_default SegVertYSize SegHoriXSize WeiBatchSize; |
---|
49 | |
---|
50 | if nargin < 13 |
---|
51 | a = 0.70783777 %0.129; % horizontal physical size of image plane normalized to focal length (in meter) |
---|
52 | b = 0.946584169%0.085; % vertical physical size of image plane normalized to focal length (in meter) |
---|
53 | Ox = -0.010727086; % camera origin offset from the image center in horizontal direction |
---|
54 | Oy = -0.0111130176; % camera origin offset from the image center in vertical direction |
---|
55 | elseif nargin < 14 |
---|
56 | b = a; |
---|
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 | Ox = -0.010727086; % camera origin offset from the image center in horizontal direction |
---|
61 | Oy = -0.0111130176; % camera origin offset from the image center in vertical direction |
---|
62 | elseif nargin < 16 |
---|
63 | Oy = Ox; |
---|
64 | end |
---|
65 | |
---|
66 | if displayFlag |
---|
67 | disp('In VRML generation Code'); |
---|
68 | end |
---|
69 | |
---|
70 | % define global variable |
---|
71 | global GeneralDataFolder ScratchDataFolder LocalFolder; |
---|
72 | |
---|
73 | %maskSky = imerode(maskSky, strel('disk', 3) ); |
---|
74 | %maskG = imerode(maskSky, strel('disk', 3) ); |
---|
75 | |
---|
76 | %calculating the mean of the sky color |
---|
77 | imageActual = imread([GeneralDataFolder '/' ImgFolder '/' filename '.jpg']); |
---|
78 | meanSkyColor = permute( sum( sum( repmat(maskSky, [1 1 3]) .* ... |
---|
79 | double( imresize(imageActual, size(maskSky) ) ), 1), 2), [3 1 2]) ; |
---|
80 | meanGroundColor = permute( sum( sum( repmat(maskG, [1 1 3]) .* ... |
---|
81 | double( imresize(imageActual, size(maskG) ) ), 1), 2), [3 1 2]) ; |
---|
82 | meanSkyColor = meanSkyColor / sum(sum( maskSky ) ); |
---|
83 | meanGroundColor = meanGroundColor / sum(sum( maskG )); |
---|
84 | |
---|
85 | vrml_filename = [filename '_' DepthDirectory '.wrl']; |
---|
86 | %LowResImgIndexSuperpixelSepTemp = LowResImgIndexSuperpixelSep; |
---|
87 | %[VertYSize HoriXSize] = size(LowResImgIndexSuperpixelSepTemp); |
---|
88 | [dum VertYSize HoriXSize] = size(Position3D); |
---|
89 | %nu_patch = VertYSize* HoriXSize; |
---|
90 | |
---|
91 | Position3DCoord = reshape(Position3D,3,VertYSize*HoriXSize); |
---|
92 | Position3DCoord(3,:) = -Position3DCoord(3,:); % important to make z direction negative |
---|
93 | |
---|
94 | table = reshape(0:(VertYSize*HoriXSize-1),VertYSize,[]); |
---|
95 | Position3DCoordOrdeerIndex = zeros(1,92568); %RAJIV |
---|
96 | GridPosition3DCoordOrdeerIndex = zeros(1,92576); |
---|
97 | HBrokeBook(:,end) = 0; |
---|
98 | VBrokeBook(end,:) = 0; |
---|
99 | |
---|
100 | if displayFlag, |
---|
101 | disp('Going into FOR LOOP, which is bad in Matlab. This code should not take longer than 3 seconds'); |
---|
102 | end |
---|
103 | |
---|
104 | for i = 1:VertYSize-1 |
---|
105 | for j = 1:HoriXSize-1 |
---|
106 | % check which diagnoal to render |
---|
107 | if isempty(HBrokeBook) | isempty(VBrokeBook) |
---|
108 | 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)]; |
---|
109 | 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)]; |
---|
110 | else |
---|
111 | target = [HBrokeBook(i,j) VBrokeBook(i,j+1) HBrokeBook(i+1,j) VBrokeBook(i,j)]; |
---|
112 | if target == [0 0 1 1]; |
---|
113 | Index = [ table(i,j) table(i+1,j+1) table(i,j+1) table(i,j) table(i+1,j) table(i+1,j+1)]; |
---|
114 | GridIndex = [table(i,j) table(i+1,j+1) table(i,j+1) table(i,j)]; |
---|
115 | elseif target == [1 0 0 1]; |
---|
116 | Index = [ table(i+1,j) table(i+1,j+1) table(i,j+1) table(i,j) table(i+1,j) table(i,j+1)]; |
---|
117 | GridIndex = [table(i+1,j) table(i+1,j+1) table(i,j+1) table(i+1,j)]; |
---|
118 | elseif target == [1 1 0 0]; |
---|
119 | 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)]; |
---|
120 | GridIndex = [ table(i,j) table(i+1,j) table(i+1,j+1) table(i,j)]; |
---|
121 | elseif target == [0 1 1 0]; |
---|
122 | 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)]; |
---|
123 | GridIndex = [ table(i,j) table(i+1,j) table(i,j+1) table(i,j)]; |
---|
124 | else |
---|
125 | 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)]; |
---|
126 | 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)]; |
---|
127 | end |
---|
128 | end |
---|
129 | |
---|
130 | % Sky Exclude |
---|
131 | if maskSky(i,j)==1 |
---|
132 | %% check if the neighbors are sky as well, for Np length |
---|
133 | if (j+(Np-1))>HoriXSize |
---|
134 | jL=HoriXSize; |
---|
135 | else |
---|
136 | jL=j+(Np-1); |
---|
137 | end |
---|
138 | if (i+(Np-1))>VertYSize |
---|
139 | iL=VertYSize; |
---|
140 | else |
---|
141 | iL=i+(Np-1); |
---|
142 | end |
---|
143 | if ((sum(xor(maskSky(i,j:jL),ones(1,length(maskSky(i,j:jL)))))>0)||... |
---|
144 | sum(xor(maskSky(i:iL,j),ones(length(maskSky(i:iL,j)),1)))>0) |
---|
145 | Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index]; |
---|
146 | GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex]; |
---|
147 | %% also update the depth values for all the 6 points by |
---|
148 | %% changing them to a value of any non-sky point in the |
---|
149 | %% vicinity of Np down and to the right |
---|
150 | |
---|
151 | %% so first we find the point non-sky point which made us |
---|
152 | %% include this sky point |
---|
153 | iRep=i; |
---|
154 | indexVec=j:jL; |
---|
155 | nonSkyPoints=find(maskSky(i,indexVec)==0); |
---|
156 | if length(nonSkyPoints)>0 |
---|
157 | jRep=indexVec(a(1)); |
---|
158 | else |
---|
159 | jRep=j; |
---|
160 | indexVec=i:iL; |
---|
161 | nonSkyPoints=find(maskSky(indexVec,j)==0); |
---|
162 | iRep=indexVec(nonSkyPoints(1)); |
---|
163 | end |
---|
164 | %% now we update the points |
---|
165 | indexArray = [i i+1 i+1 i i+1 i; j j j+1 j j+1 j+1]; |
---|
166 | |
---|
167 | for iA=1:6 |
---|
168 | %if indexArray(1,iA)>VertYSize || indexArray(2,iA)>HoriXSize |
---|
169 | % indexArray(2,iA) |
---|
170 | % indexArray(1,iA) |
---|
171 | %end |
---|
172 | if maskSky(indexArray(1,iA),indexArray(2,iA))==1 |
---|
173 | newPosition3D = permute(ray(indexArray(1,iA),indexArray(2,iA),:),[3 2 1])*depth(iRep,jRep); |
---|
174 | Imgindex = sub2ind([VertYNuDepth HoriXNuDepth],indexArray(1,iA),indexArray(2,iA)); |
---|
175 | Position3Dcoord(:,Imgindex)=newPosition3D; |
---|
176 | end |
---|
177 | end |
---|
178 | end |
---|
179 | else |
---|
180 | Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index]; |
---|
181 | GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex]; |
---|
182 | %% also update the depth values |
---|
183 | indexArray = [i+1 i+1 i+1 i; j j+1 j+1 j+1]; |
---|
184 | for iA=1:4 |
---|
185 | if maskSky(indexArray(1,iA),indexArray(2,iA))==1 |
---|
186 | newPosition3D = permute(ray(indexArray(1,iA),indexArray(2,iA),:),[3 2 1])*depth(i,j); |
---|
187 | Imgindex = sub2ind([VertYNuDepth HoriXNuDepth],indexArray(1,iA),indexArray(2,iA)); |
---|
188 | Position3Dcoord(:,Imgindex)=newPosition3D; |
---|
189 | end |
---|
190 | end |
---|
191 | end |
---|
192 | |
---|
193 | |
---|
194 | end |
---|
195 | end |
---|
196 | size(Position3DCoordOrdeerIndex) |
---|
197 | size(GridPosition3DCoordOrdeerIndex) |
---|
198 | |
---|
199 | % calculate PositionTexCoord |
---|
200 | PositionTex = permute(ray(:,:,1:2)./repmat(cat(3,a,b),[VertYSize HoriXSize 1])+0.5,[3 1 2]); |
---|
201 | PositionTexCoord = PositionTex; |
---|
202 | |
---|
203 | % find the Sky Wall point |
---|
204 | Xmin=min(Position3DCoord(1,:))-Sep; |
---|
205 | Xmax=max(Position3DCoord(1,:))+Sep; |
---|
206 | Ymin=min(Position3DCoord(2,:))-Sep; |
---|
207 | Ymax=max(Position3DCoord(2,:))+Sep; |
---|
208 | Zmin=min(Position3DCoord(3,:))-Sep; |
---|
209 | Zmax=max(Position3DCoord(3,:))+Sep; |
---|
210 | %% the eight corners of the cube would then be |
---|
211 | cc=[]; |
---|
212 | cc = [cc [Xmin Ymin Zmax]']; %% 1 |
---|
213 | cc = [cc [Xmax Ymin Zmax]']; %% 2 |
---|
214 | cc = [cc [Xmax Ymin Zmin]']; %% 3 |
---|
215 | cc = [cc [Xmin Ymin Zmin]']; %% 4 |
---|
216 | |
---|
217 | cc = [cc [Xmin Ymax Zmax]']; %% 5 |
---|
218 | cc = [cc [Xmax Ymax Zmax]']; %% 6 |
---|
219 | cc = [cc [Xmax Ymax Zmin]']; %% 7 |
---|
220 | cc = [cc [Xmin Ymax Zmin]']; %% 8 |
---|
221 | % wallIndices = [front_wall right_wall back_wall left_wall]; |
---|
222 | wallIndices = [2 6 7 3 3 7 8 4 1 4 8 5 5 8 7 6 1 2 3 4 1 5 6 2]-1; |
---|
223 | |
---|
224 | % inital header |
---|
225 | disp('writing vrml..'); |
---|
226 | fp = fopen([ScratchDataFolder '/vrml/' vrml_filename],'w'); |
---|
227 | |
---|
228 | fprintf(fp, '#VRML V2.0 utf8\n'); |
---|
229 | |
---|
230 | % add navigate_info |
---|
231 | fprintf(fp, 'NavigationInfo {\n'); |
---|
232 | fprintf(fp, ' headlight TRUE\n'); |
---|
233 | fprintf(fp, ' type ["FLY", "ANY"]}\n\n'); |
---|
234 | |
---|
235 | % add viewpoint |
---|
236 | fprintf(fp, 'Viewpoint {\n'); |
---|
237 | fprintf(fp, ' position 0 0.0 0.0\n'); |
---|
238 | fprintf(fp, ' orientation 0 0 0 0\n'); |
---|
239 | fprintf(fp, ' fieldOfView 0.7\n'); |
---|
240 | fprintf(fp, ' description "Original"}\n'); |
---|
241 | |
---|
242 | %============== add background color====== |
---|
243 | fprintf(fp, 'DEF Back1 Background {\n'); |
---|
244 | %fprintf(fp, 'groundColor [.3 .29 .27]\n'); |
---|
245 | fprintf(fp, 'groundColor [%f %f %f]\n', meanGroundColor/255); |
---|
246 | %fprintf(fp, 'skyColor [0.31 0.54 0.76]}\n'); |
---|
247 | fprintf(fp, 'skyColor [%f %f %f]}\n', meanSkyColor/255); |
---|
248 | %========================================= |
---|
249 | |
---|
250 | % add Shape for texture faceset |
---|
251 | fprintf(fp, 'Shape{\n'); |
---|
252 | fprintf(fp, ' appearance Appearance {\n'); |
---|
253 | fprintf(fp, [' texture ImageTexture { url "./image/' filename '.jpg' '" }\n']); |
---|
254 | fprintf(fp, ' }\n'); |
---|
255 | fprintf(fp, ' geometry IndexedFaceSet {\n'); |
---|
256 | fprintf(fp, ' coord Coordinate {\n'); |
---|
257 | |
---|
258 | % insert coordinate in 3d |
---|
259 | % ======================= |
---|
260 | fprintf(fp, ' point [ \n'); |
---|
261 | fprintf(fp, ' %.2f %.2f %.2f,\n',Position3DCoord); |
---|
262 | fprintf(fp, ' ]\n'); |
---|
263 | fprintf(fp, ' }\n'); |
---|
264 | |
---|
265 | % insert coordinate index in 3d |
---|
266 | fprintf(fp, ' coordIndex [\n'); |
---|
267 | fprintf(fp, ' %g %g %g -1,\n',Position3DCoordOrdeerIndex); |
---|
268 | fprintf(fp, ' ]\n'); |
---|
269 | |
---|
270 | % insert texture coordinate |
---|
271 | fprintf(fp, ' texCoord TextureCoordinate {\n'); |
---|
272 | fprintf(fp, ' point [\n'); |
---|
273 | fprintf(fp, ' %.4g %.4g,\n',PositionTexCoord); |
---|
274 | fprintf(fp, ' ]\n'); |
---|
275 | fprintf(fp, ' }\n'); |
---|
276 | fprintf(fp, ' }\n'); |
---|
277 | fprintf(fp, '}\n'); |
---|
278 | |
---|
279 | if Grid == 1 |
---|
280 | % ======================================== |
---|
281 | fprintf(fp, 'Shape{\n'); |
---|
282 | fprintf(fp, ' appearance Appearance { material Material {emissiveColor 1 0 0 }}\n'); |
---|
283 | fprintf(fp, ' geometry IndexedLineSet {\n'); |
---|
284 | fprintf(fp, ' coord Coordinate {\n'); |
---|
285 | fprintf(fp, ' point [ \n'); |
---|
286 | fprintf(fp, ' %.2f %.2f %.2f,\n',Position3DCoord); |
---|
287 | fprintf(fp, ' ]\n'); |
---|
288 | fprintf(fp, ' }\n'); |
---|
289 | fprintf(fp, ' coordIndex [\n'); |
---|
290 | fprintf(fp, ' %g %g %g %g -1,\n',GridPosition3DCoordOrdeerIndex); |
---|
291 | fprintf(fp, ' ]\n'); |
---|
292 | %fprintf(fp, ' color Color { color [1 0 0, 0 1 0, 0 0 1]}\n'); |
---|
293 | %fprintf(fp, ' colorIndex [0 0 0 0 0 ]\n'); |
---|
294 | fprintf(fp, ' colorPerVertex FALSE\n'); |
---|
295 | fprintf(fp, ' }\n'); |
---|
296 | %================================== |
---|
297 | fprintf(fp, ' }\n'); |
---|
298 | fprintf(fp, '}\n'); |
---|
299 | end |
---|
300 | % close the file |
---|
301 | fclose(fp); |
---|
302 | |
---|
303 | disp('Finished writing vrml'); |
---|