source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/LaserDataCollection/makeDepthImagesSph_ImgbyImg_Triangle.m @ 37

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

Added original make3d

File size: 13.8 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 makeDepthImagesSph_ImgbyImg(dataNumber, displayFlag)
40% global depthBins;
41% global countBuf;
42if nargin < 2
43    displayFlag = false;
44end
45if nargin < 1
46    dataNumber = 1;
47end
48%% we first read the log file and then read the depth values in a
49%% two-dimensional array after binning and averaging values for each bin
50%% value
51depthDirectory = '.\dataset5\';
52imgDirectory = '.\dataset5\';
53vrmlDirectory = strcat(depthDirectory,'vrml/');
54
55%Win05:
56% system([ 'cat ' depthDirectory depthFilename ' | sed s/PTLASER/\/g | cat > ' depthDirectory depthFilename 'cleaned']);
57% Above command is passed in Linux to clear the text, leaving only numbers, which can be directly
58% imported into Matlab by using File > Import Data
59    dirList = dir([depthDirectory '*.txt']);
60    depthFileInitialname = dirList(dataNumber).name;
61   
62%     system([ 'cat ' depthDirectory depthFileInitialname ' | sed /PTLASER/\d | cat > ' depthDirectory depthFileInitialname 'image1']);
63%     system([ 'cat ' depthDirectory depthFileInitialname 'image1' ' | sed s/IMAGE/\/g | cat > ' depthDirectory depthFileInitialname 'image2']);
64%     system([ 'cat ' depthDirectory depthFileInitialname 'image2' ' | sed s/data/\\n\data/g | cat > ' depthDirectory depthFileInitialname 'image3']);
65%     system([ 'cat ' depthDirectory depthFileInitialname 'image3' ' | sed /data/\d | cat > ' depthDirectory depthFileInitialname 'image']);
66%
67%     system([ 'cat ' depthDirectory depthFileInitialname ' | sed s/PTLASER/\/g | cat > ' depthDirectory depthFileInitialname 'cleaned1']);
68%     system([ 'cat ' depthDirectory depthFileInitialname 'cleaned1' ' | sed /IMAGE/\d | cat > ' depthDirectory depthFileInitialname 'cleaned']);
69%
70%     system([ 'rm ' depthDirectory depthFileInitialname 'cleaned1']);
71%     system([ 'rm ' depthDirectory depthFileInitialname 'image1']);
72%     system([ 'rm ' depthDirectory depthFileInitialname 'image2']);
73%     system([ 'rm ' depthDirectory depthFileInitialname 'image3']);
74    depthFilename = strcat(depthFileInitialname,'cleaned');
75    imageFileForAnglesName = strcat(depthFileInitialname,'image');
76rawDepth = load([depthDirectory depthFilename]); %load([directory 'log' num2str(dataNumber) '.mat']);
77imageAngList = load([depthDirectory imageFileForAnglesName]);
78%% now we make the bins and store the rawDepth values in the appropriate
79%% bins
80panStartAngD  =   -185;%rawDepth(1,2);
81panEndAngD    =   185;%rawDepth(end,2);
82sweepStartAngD=   -90;
83sweepEndAngD  =   90;
84panRange = panEndAngD-panStartAngD;
85sweepRange = sweepEndAngD-sweepStartAngD;
86 
87panBinResD  =   .125; %panRange/size(rawDepth,1);
88sweepBinResD=   1; %sweepRange/rawDepth(1,4);
89panBins     = ceil(panRange/panBinResD); 
90sweepBins   = ceil(sweepRange/sweepBinResD);
91depthBins = zeros(panBins,sweepBins);
92countBuf = zeros(panBins,sweepBins);
93[depthBins,countBuf,minDepth]=readLogFileForDepth(rawDepth,depthBins,countBuf, ...
94    panEndAngD,panBinResD,sweepEndAngD,sweepBinResD);
95%% trim depth buffer to remove zero columns
96[depthBins,countBuf,panStartTrim,panEndTrim,sweepStartTrim,sweepEndTrim]=trimDepthBuf(depthBins,countBuf,panBins,sweepBins);
97panBins=size(depthBins,1);
98sweepBins=size(depthBins,2);
99panStartAngD  =   -185+(panStartTrim*panBinResD);%rawDepth(1,2);
100panEndAngD    =   185-(panEndTrim*panBinResD);%rawDepth(end,2);
101sweepStartAngD=   -90+(sweepStartTrim*sweepBinResD);
102sweepEndAngD  =   90-(sweepEndTrim*sweepBinResD);
103panRange = panEndAngD-panStartAngD;
104sweepRange = sweepEndAngD-sweepStartAngD;
105%% we now check for validity of the buffer
106[depthBins,countBuf]=checkIfValid(depthBins,countBuf,size(depthBins,1), ...
107    size(depthBins,2),minDepth);
108clear countBuf;
109%% we now do the triangle making
110%% for the time being we are skipping this part
111%% now for every i,j in the depth map, we need to find the x_im and y_im,
112%% based on the format that Min wants it in i.e. lets say that the image
113%% lines between (0,0), (1,0), (1,1) and (0,1) going counter clockwise with
114%% the bottom left point of the image being (0,0), and so the top left
115%% point being (0,1)
116% image2LaserPanAngleOffsetD = 0; %% check if this is 0 by trial and error
117% panImageStartAngD = -180;
118% panImageEndAngD = 180;
119% sweepImageStartAngD = 67-90;
120% sweepImageEndAngD = (180-68)-90;
121% panBinIdxStart=floor((panEndAngD-panImageStartAngD)/panBinResD);
122% panBinIdxEnd=floor((panEndAngD-panImageEndAngD)/panBinResD);
123% sweepBinIdxStart=floor((sweepEndAngD-sweepImageStartAngD)/sweepBinResD);
124% sweepBinIdxEnd=floor((sweepEndAngD-sweepImageEndAngD)/sweepBinResD);
125       
126laserPanAxisOffset = 0.2;
127laserTiltAxisOffset = 0.155;
128rigHeight = 1.5;
129distCoeffs = [-0.068382333138247237 -0.54357925933026252 -0.0029516085790312376 ...
130        -0.00024597914695977276];
131D2R = (pi/180);
132fx = 2400.2091651084;
133fy = 2407.3312729885838;
134Ox = 1110.7122391785729;%2272/2; %
135Oy = 833.72104535435108;%1704/2; %
136camIntMat = [ -fx, 0.0, Ox;
137    0.0, -fy, Oy;
138    0.0, 0.0, 1.0 ];
139UNDISTORT = 1;
140rollInit = pi;
141pitchInit = -pi/2;
142yawInit = 0.0;
143camPanAxisOffset = 0.20;
144camTiltAxisOffset = 0.155;
145imagePanAngD = imageAngList(:,2);
146imagePanAngD = (imagePanAngD<-180).*(imagePanAngD+360)+...
147    (imagePanAngD>-180).*(imagePanAngD);
148imagePanAngRndD = round(imageAngList(:,2));
149imageRangeAngD = 21;
150R = eulerAngles(rollInit,pitchInit,yawInit);
151%% roll is along the x-axis, pitch is along the y-axis and yaw is along
152%% the z axis
153t = [camPanAxisOffset 0.0 camTiltAxisOffset];
154camPos = [R t'; 0 0 0 1];
155camExtMat1 = inv(camPos);
156for imNum=1:length(imagePanAngD)
157    image2LaserPanAngleOffsetD = 0; %% check if this is 0 by trial and error
158    dirList = dir([imgDirectory strcat('*', ...
159        num2str(abs(imagePanAngRndD(imNum))),'*.jpg')]);
160    if (length(dirList)==0)
161        problem_list=1
162    end
163        imageFilename = dirList(1).name;
164    depthPixelFilename=regexprep(imageFilename,'img','depth_triangle');
165    depthPixelFilename=regexprep(depthPixelFilename,'.jpg','.mat');
166    depthPixelFilename=strcat(depthDirectory,depthPixelFilename);
167   
168    vrmlFilename=regexprep(imageFilename,'img','vrml_faceset');
169    vrmlFilename=regexprep(vrmlFilename,'.jpg','.wrl');
170%     vrmlFilename=strcat(vrmlDirectory,vrmlFilename);
171
172    imagePanAngCurrentD=(round(imagePanAngD(imNum)/panBinResD))*panBinResD;
173        panImageStartAngD = imagePanAngCurrentD-imageRangeAngD;
174        panImageEndAngD = imagePanAngCurrentD+imageRangeAngD;
175        sweepImageStartAngD = 63-90;
176        sweepImageEndAngD = (180-63)-90;
177    pixelCount = 1;
178    ltCount = 1;
179    rtCount = 1;
180   
181        panCount=1;
182%       imageFilenamePrev=' ';
183        for panBinAngleLoopD=panImageStartAngD:panBinResD:(panImageEndAngD+panBinResD)
184        panBinAngleLoopDt=panBinAngleLoopD;
185        if panBinAngleLoopD<-180 %% wrap around
186            panBinAngleLoopDt=panBinAngleLoopD+360;
187        end
188        if panBinAngleLoopD>180 %% wrap around
189            panBinAngleLoopDt=panBinAngleLoopD-360;
190        end
191        panBinIdx=floor((panEndAngD-panBinAngleLoopDt)/panBinResD);
192        sweepCount=1;
193        for sweepBinAngleLoopD=sweepImageStartAngD:sweepBinResD:(sweepImageEndAngD+sweepBinResD)
194            %% find if the row number is odd
195            %% if it is then two points are for left triangle and even
196            %% points for right triangle
197            %% if the row number is even, then two points are for right
198            %% triange and one is for left triangle
199            %% in this way we make the triangles, they look like
200            %%      ---*
201            %%      | /*
202            %%      |/**               
203         
204                sweepBinIdx=floor((sweepEndAngD-sweepBinAngleLoopD)/sweepBinResD);
205            %% we now calculate the world coordinate point from depth buffer
206            if ((panBinIdx>size(depthBins,1))||(panBinIdx<1)||...
207                    (sweepBinIdx>size(depthBins,2))||(sweepBinIdx<1))
208                waithere = 1
209            end           
210            depthVal = depthBins(panBinIdx,sweepBinIdx);
211            Wcoord=calcPointFromDepthBuf(depthVal,panBinIdx,sweepBinIdx, ...
212                panEndAngD,panBins,panRange,sweepEndAngD,sweepBins, ...
213                sweepRange,laserPanAxisOffset,laserTiltAxisOffset,rigHeight);
214            Wcoord_vec(panCount,sweepCount,:)=Wcoord;
215         
216%                       %% once we have the world coordinates, we get the color
217%                       %% corresponding to these world coordinates
218%                       %% which is the same as finding the x_im and y_im in the correct
219%                       %% image               
220%                       %% so the first step will be to find the right image
221%             currPanAngD = panEndAngD-(panBinIdx/panBins)*panRange;
222%             currPanAngD = (currPanAngD+image2LaserPanAngleOffsetD);
223%             if (currPanAngD>180)
224%                 currPanAngD = currPanAngD-360;
225%             elseif (currPanAngD<-180)
226%                 currPanAngD = currPanAngD+360;
227%             end
228%             [temp1,temp2]=min(abs(imagePanAngD-currPanAngD));
229%             rightImageNum = imagePanAngRndD(temp2);
230        %         if (imageFilenamePrev~=imageFilename)
231        %             A = imread([imgDirectory imageFilename]);
232        %             imageFilenamePrev=imageFilename;           
233        %         end
234       
235            %% the definition of roll, pitch and yaw needs to be checked with
236            %% Kyle
237            panAngR = imagePanAngD(imNum)*D2R;
238            tiltAngR = imageAngList(imNum,3)*D2R;
239            [pixel, isCorrect] = getPixelCoords(Wcoord,panAngR,tiltAngR, ...
240                rigHeight,distCoeffs,camIntMat,UNDISTORT,camExtMat1);
241            Pixel_vec(panCount,sweepCount,:)=[pixel isCorrect];
242           
243            %% the storage format in the pixelreservoir is to store for each
244            %% value of panBinIdx and sweepBinIdx, the imageNumber and the
245            %% pixel values in that image
246            if (isCorrect == true)               
247                pixelReservoir(pixelCount,:)=[Wcoord pixel depthVal];
248                pixelCount=pixelCount+1;               
249            end
250            if ((panCount/2)==floor(panCount/2)) %% panCount is even
251                %% now we just keep on checking for every sweepCount
252                if ((sweepCount/2)~=floor(sweepCount/2)) %% sweepCount in odd, so one left triangle has been completed
253                    if((Pixel_vec(panCount-1,sweepCount,3)==true)&&(Pixel_vec(panCount-1,sweepCount+1,3)==true)&&(Pixel_vec(panCount,sweepCount,3)==true))
254                        leftTriangle(ltCount,1,:)=assignTriangleValues(Pixel_vec(panCount-1,sweepCount,1:2), Wcoord_vec(panCount-1,sweepCount,1:3));
255                        leftTriangle(ltCount,2,:)=assignTriangleValues(Pixel_vec(panCount-1,sweepCount+1,1:2), Wcoord_vec(panCount-1,sweepCount+1,1:3));
256                        leftTriangle(ltCount,3,:)=assignTriangleValues(Pixel_vec(panCount,sweepCount,1:2), Wcoord_vec(panCount,sweepCount,1:3));
257                        ltCount=ltCount+1;                   
258                    end
259                else %% one right triangle has been complete
260                    if ((Pixel_vec(panCount-1,sweepCount,3)==true)&&(Pixel_vec(panCount,sweepCount-1,3)==true)&&(Pixel_vec(panCount,sweepCount,3)==true))
261                        rightTriangle(rtCount,1,:)=assignTriangleValues(Pixel_vec(panCount-1,sweepCount,1:2), Wcoord_vec(panCount-1,sweepCount,1:3));
262                        rightTriangle(rtCount,2,:)=assignTriangleValues(Pixel_vec(panCount,sweepCount-1,1:2), Wcoord_vec(panCount,sweepCount-1,1:3));
263                        rightTriangle(rtCount,3,:)=assignTriangleValues(Pixel_vec(panCount,sweepCount,1:2), Wcoord_vec(panCount,sweepCount,1:3));
264                        rtCount=rtCount+1;
265                    end
266                end
267            end
268
269            sweepCount=sweepCount+1;
270        end
271        panCount=panCount+1;
272        end
273    clear Pixel_vec;
274    clear Wcoord_vec;
275   
276    depthMapXRes=55;
277        depthMapYRes=305;
278
279%     [pseudoDepthMap] = sortPixel(pixelReservoir,depthMapXRes,depthMapYRes);
280    generate_vrml_faceset_triangle(vrmlDirectory, vrmlFilename, '\dataset5\', imageFilename, leftTriangle, rightTriangle);
281%     save(depthPixelFilename, 'leftTriangle', 'rightTriangle');
282    clear leftTriangle;
283    clear rightTriangle;
284
285   
286%     pixelCount
287%       plotKyleData(pseudoDepthMap,depthMapXRes,depthMapYRes, ...
288%         imgDirectory,imageFilename)
289   
290    rtCount
291    ltCount
292    clear pixelReservoir;
293end
294% displayScenario(depthDirectory,imgDirectory);
Note: See TracBrowser for help on using the repository browser.