source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/LearningCode/Rendering/Oldversion/vrml_test_faceset_grid.m @ 37

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

Added original make3d

File size: 12.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 [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
44displayFlag = true;
45
46global 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
50if 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
55elseif 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
59elseif 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
62elseif nargin < 16
63    Oy = Ox;
64end
65
66if displayFlag
67disp('In VRML generation Code');
68end
69
70% define global variable
71global 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
77imageActual = imread([GeneralDataFolder '/' ImgFolder '/' filename '.jpg']);
78meanSkyColor = permute( sum( sum( repmat(maskSky, [1 1 3]) .* ...
79                                double( imresize(imageActual, size(maskSky) ) ), 1), 2), [3 1 2]) ;
80meanGroundColor = permute( sum( sum( repmat(maskG, [1 1 3]) .* ...
81                                double( imresize(imageActual, size(maskG) ) ), 1), 2), [3 1 2]) ;
82meanSkyColor = meanSkyColor / sum(sum( maskSky ) );
83meanGroundColor = meanGroundColor / sum(sum( maskG ));
84
85vrml_filename = [filename '_' DepthDirectory '.wrl'];
86%LowResImgIndexSuperpixelSepTemp = LowResImgIndexSuperpixelSep;
87%[VertYSize HoriXSize] = size(LowResImgIndexSuperpixelSepTemp);
88[dum VertYSize HoriXSize] = size(Position3D);
89%nu_patch = VertYSize* HoriXSize;
90
91Position3DCoord = reshape(Position3D,3,VertYSize*HoriXSize);   
92Position3DCoord(3,:) = -Position3DCoord(3,:); % important to make z direction negative
93
94table = reshape(0:(VertYSize*HoriXSize-1),VertYSize,[]);
95Position3DCoordOrdeerIndex = zeros(1,92568); %RAJIV
96GridPosition3DCoordOrdeerIndex = zeros(1,92576);
97HBrokeBook(:,end) = 0;
98VBrokeBook(end,:) = 0;
99
100if displayFlag,
101disp('Going into FOR LOOP, which is bad in Matlab. This code should not take longer than 3 seconds');
102end
103
104for 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
195end
196size(Position3DCoordOrdeerIndex)
197size(GridPosition3DCoordOrdeerIndex)
198
199% calculate PositionTexCoord
200PositionTex = permute(ray(:,:,1:2)./repmat(cat(3,a,b),[VertYSize HoriXSize 1])+0.5,[3 1 2]);
201PositionTexCoord = PositionTex;   
202
203% find the Sky Wall point
204Xmin=min(Position3DCoord(1,:))-Sep;
205Xmax=max(Position3DCoord(1,:))+Sep;
206Ymin=min(Position3DCoord(2,:))-Sep;
207Ymax=max(Position3DCoord(2,:))+Sep;
208Zmin=min(Position3DCoord(3,:))-Sep;
209Zmax=max(Position3DCoord(3,:))+Sep;
210%% the eight corners of the cube would then be
211cc=[];
212cc = [cc [Xmin Ymin Zmax]']; %% 1
213cc = [cc [Xmax Ymin Zmax]']; %% 2
214cc = [cc [Xmax Ymin Zmin]']; %% 3
215cc = [cc [Xmin Ymin Zmin]']; %% 4
216
217cc = [cc [Xmin Ymax Zmax]']; %% 5
218cc = [cc [Xmax Ymax Zmax]']; %% 6
219cc = [cc [Xmax Ymax Zmin]']; %% 7
220cc = [cc [Xmin Ymax Zmin]']; %% 8
221% wallIndices = [front_wall right_wall back_wall left_wall];
222wallIndices = [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
225disp('writing vrml..');
226fp = fopen([ScratchDataFolder '/vrml/' vrml_filename],'w');
227
228fprintf(fp, '#VRML V2.0 utf8\n');
229
230% add navigate_info
231fprintf(fp, 'NavigationInfo {\n');
232fprintf(fp, '  headlight TRUE\n');
233fprintf(fp, '  type ["FLY", "ANY"]}\n\n');
234
235% add viewpoint
236fprintf(fp, 'Viewpoint {\n');
237fprintf(fp, '    position        0 0.0 0.0\n');
238fprintf(fp, '    orientation     0 0 0 0\n');
239fprintf(fp, '    fieldOfView     0.7\n');
240fprintf(fp, '    description "Original"}\n');
241
242%============== add background color======
243fprintf(fp, 'DEF Back1 Background {\n');
244%fprintf(fp, 'groundColor [.3 .29 .27]\n');
245fprintf(fp, 'groundColor [%f %f %f]\n', meanGroundColor/255);
246%fprintf(fp, 'skyColor [0.31 0.54 0.76]}\n');
247fprintf(fp, 'skyColor [%f %f %f]}\n', meanSkyColor/255);
248%=========================================
249
250% add Shape for texture faceset
251fprintf(fp, 'Shape{\n');
252fprintf(fp, '  appearance Appearance {\n');
253fprintf(fp, ['   texture ImageTexture { url "./image/' filename '.jpg' '" }\n']);
254fprintf(fp, '  }\n');
255fprintf(fp, '  geometry IndexedFaceSet {\n');
256fprintf(fp, '    coord Coordinate {\n');
257
258% insert coordinate in 3d
259% =======================
260fprintf(fp, '      point [ \n');
261fprintf(fp, '        %.2f %.2f %.2f,\n',Position3DCoord);
262fprintf(fp, '      ]\n');
263fprintf(fp, '    }\n');
264
265% insert coordinate index in 3d
266fprintf(fp, '    coordIndex [\n');
267fprintf(fp, '              %g %g %g -1,\n',Position3DCoordOrdeerIndex);
268fprintf(fp, '    ]\n');
269
270% insert texture coordinate
271fprintf(fp, '    texCoord TextureCoordinate {\n');
272fprintf(fp, '      point [\n');
273fprintf(fp, '              %.4g %.4g,\n',PositionTexCoord);
274fprintf(fp, '        ]\n');
275fprintf(fp, '    }\n');
276fprintf(fp, '  }\n');
277fprintf(fp, '}\n');
278
279if Grid == 1
280% ========================================
281fprintf(fp, 'Shape{\n');
282fprintf(fp, '  appearance Appearance { material Material {emissiveColor 1 0 0  }}\n');
283fprintf(fp, '    geometry IndexedLineSet {\n');
284fprintf(fp, '    coord Coordinate {\n');
285fprintf(fp, '      point [ \n');
286fprintf(fp, '        %.2f %.2f %.2f,\n',Position3DCoord);
287fprintf(fp, '      ]\n');
288fprintf(fp, '    }\n');
289fprintf(fp, '    coordIndex [\n');
290fprintf(fp, '              %g %g %g %g -1,\n',GridPosition3DCoordOrdeerIndex);
291fprintf(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');
294fprintf(fp, '    colorPerVertex FALSE\n');
295fprintf(fp, '    }\n');
296%==================================
297fprintf(fp, '  }\n');
298fprintf(fp, '}\n');
299end
300% close the file
301fclose(fp);
302
303disp('Finished writing vrml');
Note: See TracBrowser for help on using the repository browser.