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 OneShotTest(ImgPath, OutPutFolder,... |
---|
40 | taskName,...% taskname will append to the imagename and form the outputname |
---|
41 | ScratchFolder,... % ScratchFolder |
---|
42 | ParaFolder,... % All Parameter Folder |
---|
43 | Flag... % All Flags 1) intermediate storage flag |
---|
44 | ); |
---|
45 | |
---|
46 | addpath(genpath('../../LearningCode/')); |
---|
47 | addpath(genpath('../../third_party')); |
---|
48 | addpath(genpath('../../bin/mex')); |
---|
49 | |
---|
50 | % This function is the speed up version of the OneShot3d |
---|
51 | % Improvement Log: |
---|
52 | % 1) speedup segment Mex-file |
---|
53 | % 2) speedup SparseSample3d Mex-file |
---|
54 | % 3) eliminate reading image and filterbank calculation multiple times |
---|
55 | |
---|
56 | % Input: |
---|
57 | % ImgPath -- the path include the file name of the image |
---|
58 | % OutPutFolder -- the path of the output folder |
---|
59 | % ScratchFolder -- intermediante data storage place (used for learning and debug) |
---|
60 | |
---|
61 | % Parameter and Data Setting ========================= |
---|
62 | startTime = tic; |
---|
63 | fprintf('Starting with new optimization... '); |
---|
64 | |
---|
65 | if nargin < 2 |
---|
66 | disp('Eror: At least need two input argument needed'); |
---|
67 | return; |
---|
68 | elseif nargin < 3 |
---|
69 | taskName = ''; |
---|
70 | Flag = []; |
---|
71 | ScratchFolder = ['/afs/cs/group/reconstruction3d/scratch/IMStorage' ]; |
---|
72 | ParaFolder = '/afs/cs/group/reconstruction3d/scratch/Para/'; |
---|
73 | elseif nargin < 4 |
---|
74 | Flag = []; |
---|
75 | ScratchFolder = ['/afs/cs/group/reconstruction3d/scratch/IMStorage' ]; |
---|
76 | ParaFolder = '/afs/cs/group/reconstruction3d/scratch/Para/'; |
---|
77 | elseif nargin < 5 |
---|
78 | Flag = []; |
---|
79 | ParaFolder = '/afs/cs/group/reconstruction3d/scratch/Para/'; |
---|
80 | elseif nargin < 6 |
---|
81 | Flag = []; |
---|
82 | end |
---|
83 | |
---|
84 | % yalmiptest |
---|
85 | |
---|
86 | % parameter setting |
---|
87 | filename{1} = ImgPath( ( max( strfind( ImgPath, '/'))+1) :end); |
---|
88 | |
---|
89 | % Function that setup the Default |
---|
90 | Default = SetupDefault_New(... |
---|
91 | [ strrep(filename{1}, '.jpg', '') '_' taskName],... |
---|
92 | ParaFolder,... |
---|
93 | OutPutFolder,... |
---|
94 | ScratchFolder,... |
---|
95 | Flag); |
---|
96 | disp([ num2str( toc(startTime) ) ' seconds.']); |
---|
97 | |
---|
98 | % Image loading |
---|
99 | fprintf('Loading the image... '); |
---|
100 | img = imread(ImgPath); |
---|
101 | % imgCameraParameters = exifread(ImgPath); |
---|
102 | % if false %Default.Flag.DisplayFlag && (any( strcmp(fieldnames(imgCameraParameters),'FocalLength') ) || ... |
---|
103 | % any( strcmp(fieldnames(imgCameraParameters),'FNumber') ) || ... |
---|
104 | % any( strcmp(fieldnames(imgCameraParameters),'FocalPlaneXResolution') ) || ... |
---|
105 | % any( strcmp(fieldnames(imgCameraParameters),'FocalPlaneYResolution') ) ) |
---|
106 | % FocalPlaneResolutionUnit |
---|
107 | % disp('This image has known f and/or f/sx '); |
---|
108 | % end |
---|
109 | disp([ num2str( toc(startTime) ) ' seconds.']); |
---|
110 | |
---|
111 | % *************************************************** |
---|
112 | |
---|
113 | % Features =========================================== |
---|
114 | |
---|
115 | % 1) Basic Superpixel generation and Sup clean |
---|
116 | fprintf('Creating Superpixels... '); |
---|
117 | [MedSup, Sup, Default, SupNeighborTable] = gen_Sup_efficient(Default, img); |
---|
118 | disp([ num2str( toc(startTime) ) ' seconds.']); |
---|
119 | |
---|
120 | % 2) Texture Features and inner multiple Sups generation |
---|
121 | % load /afs/cs/group/reconstruction3d/scratch/Train400/data/MaskGSky.mat; |
---|
122 | % load /afs/cs/group/reconstruction3d/scratch/Train400/data/LowResImgIndexSuperpixelSep.mat; |
---|
123 | % load /afs/cs/group/reconstruction3d/scratch/Train400/data/MedSeg/MediResImgIndexSuperpixelSep1.mat |
---|
124 | % maskg = maskg{1}; |
---|
125 | % [TextureFeature TextSup]=GenTextureFeature_InnerMulSup(Default, img, Sup{2}, LowResImgIndexSuperpixelSep{1},... |
---|
126 | % imresize((MediResImgIndexSuperpixelSep),[Default.TrainVerYSize Default.TrainHoriXSize],'nearest'), 1, maskg); |
---|
127 | % comment compare with old value different only in 1:34 features since |
---|
128 | % superpixel changes |
---|
129 | fprintf('Creating Features and multiple segmentations... '); |
---|
130 | [TextureFeature TextSup]=GenTextureFeature_InnerMulSup(Default, img, Sup{2}, Sup{1},... |
---|
131 | imresize((MedSup),[Default.TrainVerYSize Default.TrainHoriXSize],'nearest'), 1);%, maskg); |
---|
132 | disp([ num2str( toc(startTime) ) ' seconds.']); |
---|
133 | |
---|
134 | % 3) Superpixel Features generation |
---|
135 | % [FeatureSupOld, NeighborListOld] = f_sup_old(Default, |
---|
136 | % LowResImgIndexSuperpixelSep{1}, MediResImgIndexSuperpixelSep); % old |
---|
137 | % data comparison |
---|
138 | % new code using prctile replace cause mean diff of 1e-4 |
---|
139 | fprintf('Calculating superpixel-shape features... '); |
---|
140 | [FeatureSup] = f_sup_old(Default, Sup{1}, MedSup, SupNeighborTable); |
---|
141 | disp([ num2str( toc(startTime) ) ' seconds.']); |
---|
142 | |
---|
143 | if Default.Flag.IntermediateStorage |
---|
144 | save([ ScratchFolder '/' strrep( filename{1},'.jpg','') '_IM.mat' ],'FeatureSup','TextureFeature','Sup','TextSup'); |
---|
145 | end |
---|
146 | |
---|
147 | %************************************SID |
---|
148 | % if Default.Flag.FeatureStorage |
---|
149 | % name = [strrep(filename{1}, '.jpg', '') '_' taskName]; |
---|
150 | % save([ScratchFolder '/' name '.mat'],'TextureFeature','FeatureSup'); |
---|
151 | % end |
---|
152 | |
---|
153 | if Default.Flag.FeaturesOnly |
---|
154 | return; |
---|
155 | end |
---|
156 | % at the end with 36130018 bytes in memory |
---|
157 | % Name Size Bytes Class |
---|
158 | % DefaTextureFeatureult 1x1 5596 struct array |
---|
159 | % DepthPara 0x0 0 char array |
---|
160 | % FeaPara 1x54 108 char array |
---|
161 | % FeatureSup 13x808 84032 double array |
---|
162 | % GroundPara 0x0 0 char array |
---|
163 | % ImgPath 1x78 156 char array |
---|
164 | % filename 1 66 cell array |
---|
165 | % MedSup 1200x900 8640000 double array |
---|
166 | % NeighborList 5134x2 82144 double array |
---|
167 | % OutPutFolder 0x0 0 char array |
---|
168 | % SFeaPara 1x54 108 char array |
---|
169 | % ScratchFlag 1x1 8 double array |
---|
170 | % ScratchFolder 0x0 0 char array |
---|
171 | % SkyPara 0x0 0 char array |
---|
172 | % Sup 1x3 402888 cell array |
---|
173 | % TextSup 6x2 1611552 cell array |
---|
174 | % TextureFeature 1x1 13688896 struct array |
---|
175 | % VarPara 0x0 0 char array |
---|
176 | % img 2272x1704x3 11614464 uint8 array |
---|
177 | % taskName 0x0 0 char array |
---|
178 | |
---|
179 | % *************************************************** |
---|
180 | |
---|
181 | % Inference ========================================== |
---|
182 | |
---|
183 | fprintf('Preparation for the Inference... '); |
---|
184 | % 1) Generate Ground and Sky mask |
---|
185 | [ maskg, maskSky] = gen_predicted_GS_efficient(Default, TextureFeature.Abs, FeatureSup); |
---|
186 | |
---|
187 | % 2) Clean Sup{1} (1st Scale) according to the sky mask |
---|
188 | [Sup{1}, SupOri, SupNeighborTable]=CleanedSupNew(Default,Sup{1},maskSky, SupNeighborTable); |
---|
189 | |
---|
190 | % 3) Generate predicted (depth:1 Variance:2 ) setup as a row verctor |
---|
191 | [Predicted]=gen_predicted(Default, TextureFeature.Abs, FeatureSup, [1 2]); |
---|
192 | |
---|
193 | if Default.Flag.BeforeInferenceStorage |
---|
194 | save([ ScratchFolder '/' strrep( filename{1},'.jpg','') '_BInf.mat' ], 'Sup', 'SupOri', 'MedSup', 'Predicted', 'maskg', 'maskSky'); |
---|
195 | end |
---|
196 | if Default.Flag.NonInference |
---|
197 | return; |
---|
198 | end |
---|
199 | disp([ num2str( toc(startTime) ) ' seconds.']); |
---|
200 | |
---|
201 | fprintf('Starting Inference... '); |
---|
202 | % 4) Plane Parameter MRF |
---|
203 | RunCompleteMRF_efficient( Default, img, Predicted, MedSup, Sup, SupOri, TextSup, SupNeighborTable, ... |
---|
204 | reshape( FeatureSup( TextureFeature.Abs(:,1)), Default.VertYNuDepth, []), ... |
---|
205 | maskSky, maskg); |
---|
206 | disp(['Finished Inference at: ' num2str( toc(startTime) ) ' seconds.']); |
---|
207 | |
---|
208 | % 5) output data |
---|
209 | fprintf('Writing superpixels and image... '); |
---|
210 | save([ Default.OutPutFolder Default.filename{1} '.mat'],'MedSup'); |
---|
211 | |
---|
212 | % 6) Image copy to OutPutFolder |
---|
213 | %system(['cp ' ImgPath ' ' OutPutFolder Default.filename{1} '.jpg']); |
---|
214 | copyfile(ImgPath, [OutPutFolder Default.filename{1} '.jpg'],'f'); |
---|
215 | disp([ num2str( toc(startTime) ) ' seconds.']); |
---|
216 | disp(['Done. Total time taken = ' num2str( toc(startTime) ) ' seconds.'] ); |
---|
217 | % *************************************************** |
---|
218 | |
---|
219 | return; |
---|