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 [] = generate_vrml_faceset_triangle(vrmlDirectory, vrmlFilename, imgDirectory, imageFilename, leftTriangle, rightTriangle) |
---|
40 | % this function play with the VRML |
---|
41 | % using only FaceSet |
---|
42 | |
---|
43 | % if nargin < 6 |
---|
44 | % a = 0.70783777 %0.129; % horizontal physical size of image plane normalized to focal length (in meter) |
---|
45 | % b = 0.946584169%0.085; % vertical physical size of image plane normalized to focal length (in meter) |
---|
46 | % Ox = -0.010727086; % camera origin offset from the image center in horizontal direction |
---|
47 | % Oy = -0.0111130176; % camera origin offset from the image center in vertical direction |
---|
48 | % elseif nargin < 7 |
---|
49 | % b = a; |
---|
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 | % end |
---|
53 | % |
---|
54 | % % define global variable |
---|
55 | % global GeneralDataFolder ScratchDataFolder LocalFolder; |
---|
56 | % |
---|
57 | % vrml_filename = ['test_faceset_triangle' filename '_' DepthDirectory '.wrl']; |
---|
58 | % LowResImgIndexSuperpixelSepTemp = LowResImgIndexSuperpixelSep; |
---|
59 | imgDirectoryMod=strcat('.',imgDirectory); |
---|
60 | A = imread([imgDirectoryMod imageFilename]); |
---|
61 | |
---|
62 | % [VertYSize HoriXSize] = size(A); |
---|
63 | % nu_patch = VertYSize* HoriXSize; |
---|
64 | % PlaneParameterTureTemp = PlaneParameterTure; |
---|
65 | |
---|
66 | % calculate ray |
---|
67 | % ray = GenerateRay(HoriXSize,VertYSize,'center'); %[ horiXSizeLowREs VertYSizeLowREs 3] |
---|
68 | |
---|
69 | % calculate Position3D |
---|
70 | % DepthTureProj = 1./sum(permute(reshape(PlaneParameterTureTemp(1:3,LowResImgIndexSuperpixelSepTemp),3,VertYSize,[]),[2 3 1]).*ray,3); |
---|
71 | % Position3D = im_cr2w_cr(DepthTureProj,ray); |
---|
72 | |
---|
73 | % calculate Position3DCoord |
---|
74 | Position3DCoord = []; |
---|
75 | for i = 1:min(size(leftTriangle,1),size(rightTriangle,1)) |
---|
76 | Position3DCoord = [Position3DCoord assignTriangleValues(leftTriangle(i,3,3:5))' assignTriangleValues(leftTriangle(i,1,3:5))' assignTriangleValues(leftTriangle(i,2,3:5))' ... |
---|
77 | assignTriangleValues(rightTriangle(i,2,3:5))' assignTriangleValues(rightTriangle(i,1,3:5))' assignTriangleValues(rightTriangle(i,3,3:5))']; |
---|
78 | end |
---|
79 | |
---|
80 | % for j = 1:HoriXSize-1 |
---|
81 | % Position3DCoord = [Position3DCoord Position3D(:,i,j) Position3D(:,i+1,j) Position3D(:,i+1,j+1) Position3D(:,i,j) Position3D(:,i+1,j+1) Position3D(:,i,j+1)]; |
---|
82 | % end |
---|
83 | % end |
---|
84 | Position3DCoord(3,:) = -Position3DCoord(3,:); % important to make z direction negative |
---|
85 | |
---|
86 | % calculate PositionTexCoord |
---|
87 | % PositionTex = permute(ray(:,:,1:2)./repmat(cat(3,a,b),[VertYSize HoriXSize 1])+0.5,[3 1 2]); |
---|
88 | PositionTexCoord = []; |
---|
89 | for i = 1:min(size(leftTriangle,1),size(rightTriangle,1)) |
---|
90 | PositionTexCoord = [PositionTexCoord assignTriangleValues(leftTriangle(i,3,1:2))' assignTriangleValues(leftTriangle(i,1,1:2))' assignTriangleValues(leftTriangle(i,2,1:2))' ... |
---|
91 | assignTriangleValues(rightTriangle(i,2,1:2))' assignTriangleValues(rightTriangle(i,1,1:2))' assignTriangleValues(rightTriangle(i,3,1:2))']; |
---|
92 | end |
---|
93 | % PositionTexCoord = PositionTexCoord./([2272 1704]'); |
---|
94 | PositionTexCoord = PositionTexCoord./(repmat([2272 1704]',1,length(PositionTexCoord))); |
---|
95 | pathName = pwd; |
---|
96 | imageForVRMLfile=strcat(pathName,imgDirectory,imageFilename); |
---|
97 | |
---|
98 | % inital header |
---|
99 | disp('writing vrml..'); |
---|
100 | fp = fopen([vrmlDirectory vrmlFilename],'w'); |
---|
101 | |
---|
102 | fprintf(fp, '#VRML V2.0 utf8\n'); |
---|
103 | |
---|
104 | % add navigate_info |
---|
105 | fprintf(fp, 'NavigationInfo {\n'); |
---|
106 | fprintf(fp, ' headlight TRUE\n'); |
---|
107 | fprintf(fp, ' type ["FLY", "ANY"]}\n\n'); |
---|
108 | |
---|
109 | % add viewpoint |
---|
110 | fprintf(fp, 'Viewpoint {\n'); |
---|
111 | fprintf(fp, ' position 0 0.0 0.0\n'); |
---|
112 | fprintf(fp, ' orientation 0 0 0 0\n'); |
---|
113 | fprintf(fp, ' fieldOfView 0.1\n'); |
---|
114 | fprintf(fp, ' description "Original"}\n'); |
---|
115 | |
---|
116 | % add Shape for texture faceset |
---|
117 | fprintf(fp, 'Shape{\n'); |
---|
118 | fprintf(fp, ' appearance Appearance {\n'); |
---|
119 | fprintf(fp, [' texture ImageTexture { url "' imageForVRMLfile '" }\n']); |
---|
120 | fprintf(fp, ' }\n'); |
---|
121 | fprintf(fp, ' geometry IndexedFaceSet {\n'); |
---|
122 | fprintf(fp, ' coord Coordinate {\n'); |
---|
123 | |
---|
124 | % insert coordinate in 3d |
---|
125 | % ======================= |
---|
126 | fprintf(fp, ' point [ \n'); |
---|
127 | fprintf(fp, ' %f %f %f,\n',Position3DCoord); |
---|
128 | fprintf(fp, ' ]\n'); |
---|
129 | fprintf(fp, ' }\n'); |
---|
130 | |
---|
131 | % insert coordinate index in 3d |
---|
132 | fprintf(fp, ' coordIndex [\n'); |
---|
133 | Position3DCoordOrdeerIndex = 0:(size(Position3DCoord,2)-1); |
---|
134 | fprintf(fp, ' %g %g %g -1,\n',Position3DCoordOrdeerIndex); |
---|
135 | fprintf(fp, ' ]\n'); |
---|
136 | |
---|
137 | % insert texture coordinate |
---|
138 | fprintf(fp, ' texCoord TextureCoordinate {\n'); |
---|
139 | fprintf(fp, ' point [\n'); |
---|
140 | fprintf(fp, ' %g %g,\n',PositionTexCoord); |
---|
141 | fprintf(fp, ' ]\n'); |
---|
142 | fprintf(fp, ' }\n'); |
---|
143 | % ================================== |
---|
144 | fprintf(fp, ' }\n'); |
---|
145 | fprintf(fp, '}\n'); |
---|
146 | |
---|
147 | % close the file |
---|
148 | fclose(fp); |
---|
149 | disp('done :)'); |
---|