[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(... |
---|
| 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 | |
---|
| 72 | displayFlag = true; |
---|
| 73 | |
---|
| 74 | global GeneralDataFolder ScratchDataFolder 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 ScratchDataFolder 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 = [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 | % initialize the HBrokeBook and VBrokeBook |
---|
| 139 | if isempty(HBrokeBook) | isempty(VBrokeBook) |
---|
| 140 | disp('empty HBrokeBook VBrokeBook in vrml_goodSkyBoundary') |
---|
| 141 | HBrokeBook = zeros(VertYSize,HoriXSize-1); |
---|
| 142 | VBrokeBook = zeros(VertYSize-1,HoriXSize); |
---|
| 143 | end |
---|
| 144 | HBrokeBook = HBrokeBook | maskSky(:,1:(end-1)) | maskSky(:,2:(end)); |
---|
| 145 | VBrokeBook = VBrokeBook | maskSky(1:(end-1),:) | maskSky(2:(end),:); |
---|
| 146 | |
---|
| 147 | if displayFlag, |
---|
| 148 | disp('Going into FOR LOOP, which is bad in Matlab. This code should not take longer than 3 seconds'); |
---|
| 149 | end |
---|
| 150 | |
---|
| 151 | for 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 |
---|
| 184 | end |
---|
| 185 | |
---|
| 186 | % calculate PositionTexCoord |
---|
| 187 | %PositionTex = permute(ray(:,:,1:2)./repmat(cat(3,a,b),[VertYSize HoriXSize 1])+0.5,[3 1 2]); |
---|
| 188 | temp = ray(:,:,1:2)./repmat(ray(:,:,3),[1 1 2]); |
---|
| 189 | PositionTex = permute(temp./repmat(cat(3,a,b),[VertYSize HoriXSize 1])+repmat(cat(3,Ox,Oy),[VertYSize HoriXSize 1]),[3 1 2]); |
---|
| 190 | PositionTexCoord = PositionTex; |
---|
| 191 | |
---|
| 192 | % find the Sky Wall point |
---|
| 193 | Xmin=min(Position3DCoord(1,:))-Sep; |
---|
| 194 | Xmax=max(Position3DCoord(1,:))+Sep; |
---|
| 195 | Ymin=min(Position3DCoord(2,:))-Sep; |
---|
| 196 | Ymax=max(Position3DCoord(2,:))+Sep; |
---|
| 197 | Zmin=min(Position3DCoord(3,:))-Sep; |
---|
| 198 | Zmax=max(Position3DCoord(3,:))+Sep; |
---|
| 199 | %% the eight corners of the cube would then be |
---|
| 200 | cc=[]; |
---|
| 201 | cc = [cc [Xmin Ymin Zmax]']; %% 1 |
---|
| 202 | cc = [cc [Xmax Ymin Zmax]']; %% 2 |
---|
| 203 | cc = [cc [Xmax Ymin Zmin]']; %% 3 |
---|
| 204 | cc = [cc [Xmin Ymin Zmin]']; %% 4 |
---|
| 205 | |
---|
| 206 | cc = [cc [Xmin Ymax Zmax]']; %% 5 |
---|
| 207 | cc = [cc [Xmax Ymax Zmax]']; %% 6 |
---|
| 208 | cc = [cc [Xmax Ymax Zmin]']; %% 7 |
---|
| 209 | cc = [cc [Xmin Ymax Zmin]']; %% 8 |
---|
| 210 | % wallIndices = [front_wall right_wall back_wall left_wall]; |
---|
| 211 | 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; |
---|
| 212 | |
---|
| 213 | % inital header |
---|
| 214 | disp('writing vrml..'); |
---|
| 215 | disp([ScratchDataFolder '/vrml/' vrml_filename]) |
---|
| 216 | fp = fopen([ScratchDataFolder '/vrml/' vrml_filename],'w+'); |
---|
| 217 | |
---|
| 218 | fprintf(fp, '#VRML V2.0 utf8\n'); |
---|
| 219 | |
---|
| 220 | % add navigate_info |
---|
| 221 | fprintf(fp, 'NavigationInfo {\n'); |
---|
| 222 | fprintf(fp, ' headlight TRUE\n'); |
---|
| 223 | fprintf(fp, ' type ["FLY", "ANY"]}\n\n'); |
---|
| 224 | |
---|
| 225 | % add viewpoint |
---|
| 226 | fprintf(fp, 'Viewpoint {\n'); |
---|
| 227 | fprintf(fp, ' position 0 0.0 0.0\n'); |
---|
| 228 | fprintf(fp, ' orientation 0 0 0 0\n'); |
---|
| 229 | fprintf(fp, ' fieldOfView 0.7\n'); |
---|
| 230 | fprintf(fp, ' description "Original"}\n'); |
---|
| 231 | |
---|
| 232 | %============== add background color====== |
---|
| 233 | fprintf(fp, 'DEF Back1 Background {\n'); |
---|
| 234 | %fprintf(fp, 'groundColor [.3 .29 .27]\n'); |
---|
| 235 | fprintf(fp, 'groundColor [%f %f %f]\n', meanGroundColor/255); |
---|
| 236 | %fprintf(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 |
---|
| 241 | fprintf(fp, 'Shape{\n'); |
---|
| 242 | fprintf(fp, ' appearance Appearance {\n'); |
---|
| 243 | fprintf(fp, [' texture ImageTexture { url "./image/' filename '.jpg' '" }\n']); |
---|
| 244 | fprintf(fp, ' }\n'); |
---|
| 245 | fprintf(fp, ' geometry IndexedFaceSet {\n'); |
---|
| 246 | fprintf(fp, ' coord Coordinate {\n'); |
---|
| 247 | |
---|
| 248 | % insert coordinate in 3d |
---|
| 249 | % ======================= |
---|
| 250 | fprintf(fp, ' point [ \n'); |
---|
| 251 | fprintf(fp, ' %.2f %.2f %.2f,\n',Position3DCoord); |
---|
| 252 | fprintf(fp, ' ]\n'); |
---|
| 253 | fprintf(fp, ' }\n'); |
---|
| 254 | |
---|
| 255 | % insert coordinate index in 3d |
---|
| 256 | fprintf(fp, ' coordIndex [\n'); |
---|
| 257 | fprintf(fp, ' %g %g %g -1,\n',Position3DCoordOrdeerIndex); |
---|
| 258 | fprintf(fp, ' ]\n'); |
---|
| 259 | |
---|
| 260 | % insert texture coordinate |
---|
| 261 | fprintf(fp, ' texCoord TextureCoordinate {\n'); |
---|
| 262 | fprintf(fp, ' point [\n'); |
---|
| 263 | fprintf(fp, ' %.4g %.4g,\n',PositionTexCoord); |
---|
| 264 | fprintf(fp, ' ]\n'); |
---|
| 265 | fprintf(fp, ' }\n'); |
---|
| 266 | fprintf(fp, ' texCoordIndex [\n'); |
---|
| 267 | fprintf(fp, ' %g %g %g -1,\n',Position3DCoordOrdeerIndex); |
---|
| 268 | fprintf(fp, ' ]\n'); |
---|
| 269 | fprintf(fp, ' }\n'); |
---|
| 270 | fprintf(fp, '}\n'); |
---|
| 271 | |
---|
| 272 | if Grid == 1 |
---|
| 273 | % ======================================== |
---|
| 274 | fprintf(fp, 'Shape{\n'); |
---|
| 275 | fprintf(fp, ' appearance Appearance { material Material {emissiveColor 1 0 0 }}\n'); |
---|
| 276 | fprintf(fp, ' geometry IndexedLineSet {\n'); |
---|
| 277 | fprintf(fp, ' coord Coordinate {\n'); |
---|
| 278 | fprintf(fp, ' point [ \n'); |
---|
| 279 | fprintf(fp, ' %.2f %.2f %.2f,\n',Position3DCoord); |
---|
| 280 | fprintf(fp, ' ]\n'); |
---|
| 281 | fprintf(fp, ' }\n'); |
---|
| 282 | fprintf(fp, ' coordIndex [\n'); |
---|
| 283 | fprintf(fp, ' %g %g %g %g -1,\n',GridPosition3DCoordOrdeerIndex); |
---|
| 284 | fprintf(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'); |
---|
| 287 | fprintf(fp, ' colorPerVertex FALSE\n'); |
---|
| 288 | fprintf(fp, ' }\n'); |
---|
| 289 | %================================== |
---|
| 290 | fprintf(fp, ' }\n'); |
---|
| 291 | fprintf(fp, '}\n'); |
---|
| 292 | end |
---|
| 293 | % close the file |
---|
| 294 | fclose(fp); |
---|
| 295 | |
---|
| 296 | disp('Finished writing vrml'); |
---|