[37] | 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_goodSkyBoundary(ScratchDataFolder,... |
---|
| 40 | filename, Position3D, depth, ray, DepthDirectory,... |
---|
| 41 | HBrokeBook, VBrokeBook, Grid, maskSky, maskG,... |
---|
| 42 | Np, Sep, a, b, Ox,... |
---|
| 43 | Oy, AppendFlag) |
---|
| 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 | |
---|
| 72 | displayFlag = true; |
---|
| 73 | |
---|
| 74 | global 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 | |
---|
| 78 | if 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 |
---|
| 83 | elseif 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 |
---|
| 87 | elseif 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 |
---|
| 90 | elseif nargin < 16 |
---|
| 91 | Oy = Ox; |
---|
| 92 | end |
---|
| 93 | |
---|
| 94 | if displayFlag |
---|
| 95 | disp('In VRML generation Code'); |
---|
| 96 | end |
---|
| 97 | |
---|
| 98 | % define global variable |
---|
| 99 | global 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 | |
---|
| 125 | vrml_filename = [DepthDirectory '.wrl']; |
---|
| 126 | %LowResImgIndexSuperpixelSepTemp = LowResImgIndexSuperpixelSep; |
---|
| 127 | %[VertYSize HoriXSize] = size(LowResImgIndexSuperpixelSepTemp); |
---|
| 128 | [dum VertYSize HoriXSize] = size(Position3D); |
---|
| 129 | %nu_patch = VertYSize* HoriXSize; |
---|
| 130 | |
---|
| 131 | Position3DCoord = reshape(Position3D,3,VertYSize*HoriXSize); |
---|
| 132 | %Position3DCoord(3,:) = -Position3DCoord(3,:); % important to make z direction negative |
---|
| 133 | |
---|
| 134 | table = reshape(0:(VertYSize*HoriXSize-1),VertYSize,[]); |
---|
| 135 | Position3DCoordOrdeerIndex = zeros(1,92568); %RAJIV |
---|
| 136 | GridPosition3DCoordOrdeerIndex = zeros(1,92576); |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | if displayFlag, |
---|
| 140 | disp('Going into FOR LOOP, which is bad in Matlab. This code should not take longer than 3 seconds'); |
---|
| 141 | end |
---|
| 142 | |
---|
| 143 | for i = 1:VertYSize-1 |
---|
| 144 | for j = 1:HoriXSize-1 |
---|
| 145 | 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)]; |
---|
| 146 | GridIndex = [table(i,j) table(i+1,j+1) table(i,j+1) table(i,j)]; |
---|
| 147 | Position3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex Index]; |
---|
| 148 | GridPosition3DCoordOrdeerIndex = [Position3DCoordOrdeerIndex GridIndex]; |
---|
| 149 | end |
---|
| 150 | end |
---|
| 151 | |
---|
| 152 | temp = ray(:,:,1:2)./repmat(ray(:,:,3),[1 1 2]); |
---|
| 153 | PositionTex = permute(temp./repmat(cat(3,a,b),[VertYSize HoriXSize 1])+repmat(cat(3,Ox,Oy),[VertYSize HoriXSize 1]),[3 1 2]); |
---|
| 154 | PositionTexCoord = PositionTex; |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | % inital header |
---|
| 158 | disp('writing vrml..'); |
---|
| 159 | disp([ScratchDataFolder '/' vrml_filename]) |
---|
| 160 | |
---|
| 161 | if ~AppendFlag |
---|
| 162 | fp = fopen([ScratchDataFolder '/' vrml_filename],'w+'); |
---|
| 163 | |
---|
| 164 | fprintf(fp, '#VRML V2.0 utf8\n'); |
---|
| 165 | |
---|
| 166 | % add navigate_info |
---|
| 167 | fprintf(fp, 'NavigationInfo {\n'); |
---|
| 168 | fprintf(fp, ' headlight TRUE\n'); |
---|
| 169 | fprintf(fp, ' type ["FLY", "ANY"]}\n\n'); |
---|
| 170 | |
---|
| 171 | % add viewpoint |
---|
| 172 | fprintf(fp, 'Viewpoint {\n'); |
---|
| 173 | fprintf(fp, ' position 0 0.0 0.0\n'); |
---|
| 174 | fprintf(fp, ' orientation 0 0 0 0\n'); |
---|
| 175 | fprintf(fp, ' fieldOfView 0.7\n'); |
---|
| 176 | fprintf(fp, ' description "Original"}\n'); |
---|
| 177 | |
---|
| 178 | %============== add background color====== |
---|
| 179 | fprintf(fp, 'DEF Back1 Background {\n'); |
---|
| 180 | fprintf(fp, 'groundColor [.3 .29 .27]\n'); |
---|
| 181 | % fprintf(fp, 'groundColor [%f %f %f]\n', meanGroundColor/255); |
---|
| 182 | fprintf(fp, 'skyColor [0.31 0.54 0.76]}\n'); |
---|
| 183 | % fprintf(fp, 'skyColor [%f %f %f]}\n', meanSkyColor/255); |
---|
| 184 | %========================================= |
---|
| 185 | else |
---|
| 186 | fp = fopen([ScratchDataFolder '/' vrml_filename],'a+'); |
---|
| 187 | end |
---|
| 188 | |
---|
| 189 | % add Shape for texture faceset |
---|
| 190 | fprintf(fp, 'Shape{\n'); |
---|
| 191 | fprintf(fp, ' appearance Appearance {\n'); |
---|
| 192 | fprintf(fp, [' texture ImageTexture { url "./' filename '.jpg' '" }\n']); |
---|
| 193 | fprintf(fp, ' }\n'); |
---|
| 194 | fprintf(fp, ' geometry IndexedFaceSet {\n'); |
---|
| 195 | fprintf(fp, ' coord Coordinate {\n'); |
---|
| 196 | |
---|
| 197 | % insert coordinate in 3d |
---|
| 198 | % ======================= |
---|
| 199 | fprintf(fp, ' point [ \n'); |
---|
| 200 | fprintf(fp, ' %.2f %.2f %.2f,\n',Position3DCoord); |
---|
| 201 | fprintf(fp, ' ]\n'); |
---|
| 202 | fprintf(fp, ' }\n'); |
---|
| 203 | |
---|
| 204 | % insert coordinate index in 3d |
---|
| 205 | fprintf(fp, ' coordIndex [\n'); |
---|
| 206 | fprintf(fp, ' %g %g %g -1,\n',Position3DCoordOrdeerIndex); |
---|
| 207 | fprintf(fp, ' ]\n'); |
---|
| 208 | |
---|
| 209 | % insert texture coordinate |
---|
| 210 | fprintf(fp, ' texCoord TextureCoordinate {\n'); |
---|
| 211 | fprintf(fp, ' point [\n'); |
---|
| 212 | fprintf(fp, ' %.4g %.4g,\n',PositionTexCoord); |
---|
| 213 | fprintf(fp, ' ]\n'); |
---|
| 214 | fprintf(fp, ' }\n'); |
---|
| 215 | fprintf(fp, ' texCoordIndex [\n'); |
---|
| 216 | fprintf(fp, ' %g %g %g -1,\n',Position3DCoordOrdeerIndex); |
---|
| 217 | fprintf(fp, ' ]\n'); |
---|
| 218 | fprintf(fp, ' }\n'); |
---|
| 219 | fprintf(fp, '}\n'); |
---|
| 220 | |
---|
| 221 | if Grid == 1 |
---|
| 222 | % ======================================== |
---|
| 223 | fprintf(fp, 'Shape{\n'); |
---|
| 224 | fprintf(fp, ' appearance Appearance { material Material {emissiveColor 1 0 0 }}\n'); |
---|
| 225 | fprintf(fp, ' geometry IndexedLineSet {\n'); |
---|
| 226 | fprintf(fp, ' coord Coordinate {\n'); |
---|
| 227 | fprintf(fp, ' point [ \n'); |
---|
| 228 | fprintf(fp, ' %.2f %.2f %.2f,\n',Position3DCoord); |
---|
| 229 | fprintf(fp, ' ]\n'); |
---|
| 230 | fprintf(fp, ' }\n'); |
---|
| 231 | fprintf(fp, ' coordIndex [\n'); |
---|
| 232 | fprintf(fp, ' %g %g %g %g -1,\n',GridPosition3DCoordOrdeerIndex); |
---|
| 233 | fprintf(fp, ' ]\n'); |
---|
| 234 | %fprintf(fp, ' color Color { color [1 0 0, 0 1 0, 0 0 1]}\n'); |
---|
| 235 | %fprintf(fp, ' colorIndex [0 0 0 0 0 ]\n'); |
---|
| 236 | fprintf(fp, ' colorPerVertex FALSE\n'); |
---|
| 237 | fprintf(fp, ' }\n'); |
---|
| 238 | %================================== |
---|
| 239 | fprintf(fp, ' }\n'); |
---|
| 240 | fprintf(fp, '}\n'); |
---|
| 241 | end |
---|
| 242 | % close the file |
---|
| 243 | fclose(fp); |
---|
| 244 | |
---|
| 245 | disp('Finished writing vrml'); |
---|