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

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

Added original make3d

File size: 12.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 [vrml_filename] = vrml_test_faceset_goodSkyBoundary(ScratchDataFolder,...
40          filename, Position3D, depth, ray, DepthDirectory,...
41          HBrokeBook, VBrokeBook, Grid, maskSky, maskG,...
42          Np, Sep, a, b, Ox,...
43          Oy)
44%function [] = vrml_test_faceset_triangle(filename,PlaneParameterTure,LowResImgIndexSuperpixelSep,DepthDirectory,a,b,Ox,Oy)
45% this function play with the VRML
46% using only FaceSet
47
48%%% Param descriptions from Rajiv
49% filename - the name of the image file
50% Position3D - the matrix of 3d coordinates for all the point (55x305)
51% depth - the matrix of depth values for each point (55x305)
52% ray - the ray vector for each point in the image (55x305)
53% DepthDirectory - the name of the directory used to store final
54%   vrml files in a subfolder vrml
55% HBrokeBook - an array that contains the information if the
56%   triangle made during writing the vrml file should be reversed
57%   in order to align to a straight line
58% VBrokeBook - same as HBrokeBook but in the vertical direction
59% Grid - boolean variable which decides if there shud be a grid drawn in the vrml
60% maskSky - the skymask cell array, we need this not to draw the sky from the image
61% maskG - ground mask
62% Np - parameter which is used when deciding if Np neighbor pixels
63%   of a pixel are sky, then the pixel being considered should also
64%   be declared to be sky, this is because the sky mask is not accurate
65% Sep - parameter not used any more, shud be removed
66% a, -->
67% b, -->
68% Ox,-->
69% Oy,--> camera parameters used to generate the Ray, not used anymore as we directly pass the ray, should be removed
70%%%
71
72displayFlag = true;
73
74global GeneralDataFolder  LocalFolder ClusterExecutionDirectory...
75    ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default...
76    Horizon_default batchSize NuRow_default SegVertYSize SegHoriXSize WeiBatchSize;
77
78if nargin < 13
79    a = 0.70783777 %0.129; % horizontal physical size of image plane normalized to focal length (in meter)
80    b = 0.946584169%0.085; % vertical physical size of image plane normalized to focal length (in meter)
81    Ox = -0.010727086; % camera origin offset from the image center in horizontal direction
82    Oy = -0.0111130176; % camera origin offset from the image center in vertical direction
83elseif nargin < 14
84    b = a;
85    Ox = -0.010727086; % camera origin offset from the image center in horizontal direction
86    Oy = -0.0111130176; % camera origin offset from the image center in vertical direction
87elseif nargin < 15
88    Ox = -0.010727086; % camera origin offset from the image center in horizontal direction
89    Oy = -0.0111130176; % camera origin offset from the image center in vertical direction
90elseif nargin < 16
91    Oy = Ox;
92end
93
94if displayFlag
95disp('In VRML generation Code');
96end
97
98% define global variable
99global GeneralDataFolder  LocalFolder;
100
101%maskSky = imerode(maskSky, strel('disk', 3) );
102%maskG = imerode(maskSky, strel('disk', 3) );
103
104%calculating the mean of the sky color
105% imageActual = imread([GeneralDataFolder '/' ImgFolder '/' filename '.jpg']);
106% meanSkyColor = permute( sum( sum( repmat(maskSky, [1 1 3]) .* ...
107%                               double( imresize(imageActual, size(maskSky) ) ), 1), 2), [3 1 2]) ;
108% meanGroundColor = permute( sum( sum( repmat(maskG, [1 1 3]) .* ...
109%                               double( imresize(imageActual, size(maskG) ) ), 1), 2), [3 1 2]) ;
110% meanSkyColor = meanSkyColor / sum(sum( maskSky ) );
111% if any(isnan(meanSkyColor))
112%    if displayFlag
113%       disp('meanSkyColor NaN');
114%    end
115%    meanSkyColor = [0.31 0.54 0.76];
116% end
117% meanGroundColor = meanGroundColor / sum(sum( maskG ));
118% if any(isnan(meanGroundColor))
119%    if displayFlag
120%       disp('meanGroundColor NaN');
121%    end
122%    meanGroundColor = [0 0 0];
123% end
124
125vrml_filename = [DepthDirectory '.wrl'];
126%LowResImgIndexSuperpixelSepTemp = LowResImgIndexSuperpixelSep;
127%[VertYSize HoriXSize] = size(LowResImgIndexSuperpixelSepTemp);
128[dum VertYSize HoriXSize] = size(Position3D);
129%nu_patch = VertYSize* HoriXSize;
130
131Position3DCoord = reshape(Position3D,3,VertYSize*HoriXSize);   
132%Position3DCoord(3,:) = -Position3DCoord(3,:); % important to make z direction negative
133
134table = reshape(0:(VertYSize*HoriXSize-1),VertYSize,[]);
135Position3DCoordOrdeerIndex = zeros(1,92568); %RAJIV
136GridPosition3DCoordOrdeerIndex = zeros(1,92576);
137
138% initialize the HBrokeBook and VBrokeBook
139if isempty(HBrokeBook) | isempty(VBrokeBook)
140   disp('empty HBrokeBook VBrokeBook in vrml_goodSkyBoundary')
141   HBrokeBook = zeros(VertYSize,HoriXSize-1);
142   VBrokeBook = zeros(VertYSize-1,HoriXSize);
143end
144HBrokeBook = HBrokeBook | maskSky(:,1:(end-1)) | maskSky(:,2:(end));
145VBrokeBook = VBrokeBook | maskSky(1:(end-1),:) | maskSky(2:(end),:);
146
147if displayFlag,
148disp('Going into FOR LOOP, which is bad in Matlab. This code should not take longer than 3 seconds');
149end
150
151for i = 1:VertYSize-1
152    for j = 1:HoriXSize-1
153        % check which diagnoal to render
154%        if isempty(HBrokeBook) | isempty(VBrokeBook)
155%           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)];
156%           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)];
157%        else
158           target = [HBrokeBook(i,j) VBrokeBook(i,j+1) HBrokeBook(i+1,j) VBrokeBook(i,j)];
159           if all(target == [0 0 1 1]);
160              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)];
161              GridIndex = [table(i,j) table(i+1,j+1) table(i,j+1) table(i,j)];
162           elseif all(target == [1 0 0 1]);
163              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)];
164              GridIndex = [table(i+1,j) table(i+1,j+1) table(i,j+1) table(i+1,j)];
165           elseif all(target == [1 1 0 0]);
166              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)];
167              GridIndex = [ table(i,j) table(i+1,j) table(i+1,j+1) table(i,j)];
168           elseif all(target == [0 1 1 0]);
169              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)];
170              GridIndex = [ table(i,j) table(i+1,j) table(i,j+1) table(i,j)];
171           elseif sum(target) <3 && ~all(target == [0 1 0 1])
172              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)];
173              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)];
174           else
175%              disp('sky');
176               Index = [];
177               GridIndex = [];
178           end
179%        end
180           Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index];
181           GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex];
182
183    end
184end
185
186% calculate PositionTexCoord
187%PositionTex = permute(ray(:,:,1:2)./repmat(cat(3,a,b),[VertYSize HoriXSize 1])+0.5,[3 1 2]);
188temp = ray(:,:,1:2)./repmat(ray(:,:,3),[1 1 2]);
189PositionTex = permute(temp./repmat(cat(3,a,b),[VertYSize HoriXSize 1])+repmat(cat(3,Ox,Oy),[VertYSize HoriXSize 1]),[3 1 2]);
190PositionTexCoord = PositionTex;   
191
192% find the Sky Wall point
193Xmin=min(Position3DCoord(1,:))-Sep;
194Xmax=max(Position3DCoord(1,:))+Sep;
195Ymin=min(Position3DCoord(2,:))-Sep;
196Ymax=max(Position3DCoord(2,:))+Sep;
197Zmin=min(Position3DCoord(3,:))-Sep;
198Zmax=max(Position3DCoord(3,:))+Sep;
199%% the eight corners of the cube would then be
200cc=[];
201cc = [cc [Xmin Ymin Zmax]']; %% 1
202cc = [cc [Xmax Ymin Zmax]']; %% 2
203cc = [cc [Xmax Ymin Zmin]']; %% 3
204cc = [cc [Xmin Ymin Zmin]']; %% 4
205
206cc = [cc [Xmin Ymax Zmax]']; %% 5
207cc = [cc [Xmax Ymax Zmax]']; %% 6
208cc = [cc [Xmax Ymax Zmin]']; %% 7
209cc = [cc [Xmin Ymax Zmin]']; %% 8
210% wallIndices = [front_wall right_wall back_wall left_wall];
211wallIndices = [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;
212
213% inital header
214disp('writing vrml..');
215disp([ScratchDataFolder '/' vrml_filename])
216fp = fopen([ScratchDataFolder '/' vrml_filename],'w+');
217
218fprintf(fp, '#VRML V2.0 utf8\n');
219
220% add navigate_info
221fprintf(fp, 'NavigationInfo {\n');
222fprintf(fp, '  headlight TRUE\n');
223fprintf(fp, '  type ["FLY", "ANY"]}\n\n');
224
225% add viewpoint
226fprintf(fp, 'Viewpoint {\n');
227fprintf(fp, '    position        0 0.0 0.0\n');
228fprintf(fp, '    orientation     0 0 0 0\n');
229fprintf(fp, '    fieldOfView     0.7\n');
230fprintf(fp, '    description "Original"}\n');
231
232%============== add background color======
233fprintf(fp, 'DEF Back1 Background {\n');
234fprintf(fp, 'groundColor [.3 .29 .27]\n');
235% fprintf(fp, 'groundColor [%f %f %f]\n', meanGroundColor/255);
236fprintf(fp, 'skyColor [0.31 0.54 0.76]}\n');
237% fprintf(fp, 'skyColor [%f %f %f]}\n', meanSkyColor/255);
238%=========================================
239
240% add Shape for texture faceset
241fprintf(fp, 'Shape{\n');
242fprintf(fp, '  appearance Appearance {\n');
243fprintf(fp, ['   texture ImageTexture { url "./' filename '.jpg' '" }\n']);
244fprintf(fp, '  }\n');
245fprintf(fp, '  geometry IndexedFaceSet {\n');
246fprintf(fp, '    coord Coordinate {\n');
247
248% insert coordinate in 3d
249% =======================
250fprintf(fp, '      point [ \n');
251fprintf(fp, '        %.2f %.2f %.2f,\n',Position3DCoord);
252fprintf(fp, '      ]\n');
253fprintf(fp, '    }\n');
254
255% insert coordinate index in 3d
256fprintf(fp, '    coordIndex [\n');
257fprintf(fp, '              %g %g %g -1,\n',Position3DCoordOrdeerIndex);
258fprintf(fp, '    ]\n');
259
260% insert texture coordinate
261fprintf(fp, '    texCoord TextureCoordinate {\n');
262fprintf(fp, '      point [\n');
263fprintf(fp, '              %.4g %.4g,\n',PositionTexCoord);
264fprintf(fp, '        ]\n');
265fprintf(fp, '    }\n');
266fprintf(fp, '    texCoordIndex [\n');
267fprintf(fp, '              %g %g %g -1,\n',Position3DCoordOrdeerIndex);
268fprintf(fp, '    ]\n');
269fprintf(fp, '  }\n');
270fprintf(fp, '}\n');
271
272if Grid == 1
273% ========================================
274fprintf(fp, 'Shape{\n');
275fprintf(fp, '  appearance Appearance { material Material {emissiveColor 1 0 0  }}\n');
276fprintf(fp, '    geometry IndexedLineSet {\n');
277fprintf(fp, '    coord Coordinate {\n');
278fprintf(fp, '      point [ \n');
279fprintf(fp, '        %.2f %.2f %.2f,\n',Position3DCoord);
280fprintf(fp, '      ]\n');
281fprintf(fp, '    }\n');
282fprintf(fp, '    coordIndex [\n');
283fprintf(fp, '              %g %g %g %g -1,\n',GridPosition3DCoordOrdeerIndex);
284fprintf(fp, '    ]\n');
285%fprintf(fp, '   color Color { color [1 0 0, 0 1 0, 0 0 1]}\n');
286%fprintf(fp, '   colorIndex [0 0 0 0 0 ]\n');
287fprintf(fp, '    colorPerVertex FALSE\n');
288fprintf(fp, '    }\n');
289%==================================
290fprintf(fp, '  }\n');
291fprintf(fp, '}\n');
292end
293% close the file
294fclose(fp);
295
296disp('Finished writing vrml');
Note: See TracBrowser for help on using the repository browser.