source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/LearningCode/Inference/OldVersion/StraightLinePrior.m @ 37

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

Added original make3d

File size: 11.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 [StraightLinePriorSelectMatrix, RayPorjectImgMapY,RayPorjectImgMapX, MovedPatchBook, LineIndex,HBrokeBook,VBrokeBook] = ...
40          StraightLinePrior(p,Img,lineSegList,LearnedDepth,RayPorjectImgMapY,RayPorjectImgMapX,MovedPatchBook,HBrokeBook,VBrokeBook,Verti,a,b,Ox,Oy);
41
42global GeneralDataFolder ScratchDataFolder LocalFolder ClusterExecutionDirectory...
43    ImgFolder VertYNuPatch VertYNuDepth HoriXNuPatch HoriXNuDepth a_default b_default Ox_default Oy_default...
44    Horizon_default filename batchSize NuRow_default SegVertYSize SegHoriXSize WeiBatchSize PopUpVertY PopUpHoriX taskName;
45
46
47       
48        % initialize output
49        LineIndex = [];
50        StraightLinePriorSelectMatrix = [] ;
51
52        % take out the line seg whiich is too short (2 can be changed)
53        if Verti == 0
54           lineSegList(abs(max(min(round(lineSegList(:,1)),HoriXNuDepth),1)...
55                    -max(min(round(lineSegList(:,3)),HoriXNuDepth),1))<=2,:)=[];
56           figure(200); imagesc(Img); hold on;
57        else
58           lineSegList(abs(max(min(round(lineSegList(:,2)),VertYNuDepth),1)...
59                    -max(min(round(lineSegList(:,4)),VertYNuDepth),1))<=2,:)=[];
60           figure(300); imagesc(Img); hold on;
61
62        end
63       
64        [ImgYSize ImgXSize dummy] = size(Img);
65        NuSeg = size(lineSegList,1);
66
67        % store the line
68        tempLine = ((lineSegList+0.5)./repmat([HoriXNuDepth VertYNuDepth HoriXNuDepth VertYNuDepth],NuSeg,1)...
69                    .*repmat([ImgXSize ImgYSize ImgXSize ImgYSize],NuSeg,1))-0.5;
70        if Verti==0
71           drawseg(tempLine,200,2);
72           saveas(200,[ScratchDataFolder '/LineSeg/' filename{p} '_lineSegHori.jpg']);
73        else
74           drawseg(tempLine,300,2);
75           saveas(300,[ScratchDataFolder '/LineSeg/' filename{p} '_lineSegVert.jpg']);
76        end
77
78        if NuSeg == 0
79           LineIndex{1,1} = [];
80           LineIndex{1,2} = [];
81           LineIndex{1,3} = [];
82           LineIndex{1,4} = [];
83           StraightLinePriorSelectMatrix = sparse(1,VertYNuDepth*HoriXNuDepth);
84           disp('NuSeg==0')
85           return;
86        end
87
88        % Sort by segemtn length
89        List = sortrows([norms((lineSegList(:,1:2)-lineSegList(:,3:4))')' (1:NuSeg)'],1);
90        lineSegList = lineSegList(List(:,end),:);
91
92        % stick the patch to the straight line(If don't output ayPorjectImgMapY then no stick)
93        %RayPorjectImgMapY = repmat((1:VertYNuDepth)',1,HoriXNuDepth);
94        Count = 1
95        for k = NuSeg:-1:(1)
96            % first find fixed x point to flexible y point
97            if lineSegList(k,1)>lineSegList(k,3) % line always start from left to right
98                Start = lineSegList(k,[3 4]);
99                End = lineSegList(k,[1 2]);
100            else
101                End = lineSegList(k,[3 4]);
102                Start = lineSegList(k,[1 2]);
103            end
104
105            % find out all the close point to the line
106            X = max(ceil(Start(1)),1):min(floor(End(1)),HoriXNuDepth); %Define the fixed Horizontal position
107            Y = Start(2)+(End(2)-Start(2))/(End(1)-Start(1)).*(X - Start(1)); %Corresponded Vertical position
108            % make sure no Y <=1 and Y >= VertYNuDepth
109            mask = Y <= 1 | Y >= VertYNuDepth;
110            X(mask) = [];
111            Y(mask) = [];
112 
113            % keep the record of the index that been stick to the line
114            LineIndex{Count,1} =sub2ind([VertYNuDepth HoriXNuDepth ],min(max(floor(Y),1),VertYNuDepth),X);
115            VBrokeBook(setdiff(LineIndex{Count,1},...
116                sub2ind([VertYNuDepth HoriXNuDepth ],VertYNuDepth*ones(HoriXNuDepth,1),(1:HoriXNuDepth)'))) = 1;
117            % make sure no LineIndex{Count,1} == LineIndex{Count,2}
118            temp =sub2ind([VertYNuDepth HoriXNuDepth ],max(min(ceil(Y),VertYNuDepth),1),X);
119            temp(temp==LineIndex{Count,1}) = temp(temp==LineIndex{Count,1})+1;
120            LineIndex{Count,2} = temp;
121
122            % first find fixed y point to flexible x point
123            if lineSegList(k,2)>lineSegList(k,4) % line always start from top to bottom
124                Start = lineSegList(k,[3 4]);
125                End = lineSegList(k,[1 2]);
126            else
127                End = lineSegList(k,[3 4]);
128                Start = lineSegList(k,[1 2]);
129            end
130
131            VY = max(ceil(Start(2)),1):min(floor(End(2)),VertYNuDepth); %Define the fixed Horizontal position
132            VX = Start(1)+(End(1)-Start(1))/(End(2)-Start(2)).*(VY - Start(2)); %Corresponded Vertical position
133            % make sure no Y <=1 and Y >= VertYNuDepth
134            mask = VX <= 1 | VX >= HoriXNuDepth;
135            VX(mask) = [];
136            VY(mask) = [];
137
138            % keep the record of the index that been stick to the line
139            LineIndex{Count,3} =sub2ind([VertYNuDepth HoriXNuDepth ],VY,min(max(floor(VX),1),HoriXNuDepth));
140            HBrokeBook(setdiff(LineIndex{Count,3},...
141                sub2ind([VertYNuDepth HoriXNuDepth ],(1:VertYNuDepth)',HoriXNuDepth*ones(VertYNuDepth,1)))) = 1;
142            % make sure no LineIndex{Count,1} == LineIndex{Count,2}
143            temp = sub2ind([VertYNuDepth HoriXNuDepth ],VY,max(min(ceil(VX),HoriXNuDepth),1));
144            temp(temp==LineIndex{Count,3}) = temp(temp==LineIndex{Count,3})+1;
145            LineIndex{Count,4} = temp;
146
147            if Verti == 1
148              X = VX;
149              Y = VY;
150              IndexUpper = LineIndex{Count,3};
151              IndexLower = LineIndex{Count,4};
152            else
153              IndexUpper = LineIndex{Count,1};
154              IndexLower = LineIndex{Count,2};
155            end
156
157            % make sure X is not empty
158            if size(X,2) == 0
159               continue;
160            end
161 
162            % generate specific ray for whole line
163            RayLineY = ((VertYNuDepth+1-Y)-0.5)/VertYNuDepth - Oy;
164            RayLineX = ((X)-0.5)/HoriXNuDepth - Ox;
165            RayCenter = RayImPosition(RayLineY,RayLineX,a,b,Ox,Oy); %[ horiXSizeLowREs VertYSizeLowREs 3]
166            % generating the axis of the plane that these rays lay on
167            [U S V] = svd(permute(RayCenter,[3 2 1]));
168            Ns = U(:,3);
169            U = U(:,1:2);
170            S = S(1:2,1:2);
171            V = V(:,1:2);
172
173            % calculate all the points in this plane coordinate U
174
175            LSize = size(IndexUpper,2);
176            PlaneCoordUpper = S*V'*[spdiags(LearnedDepth(IndexUpper)',0,LSize,LSize)];
177            PlaneCoordLower = S*V'*[spdiags(LearnedDepth(IndexLower)',0,LSize,LSize)];
178         
179            if 0 %Sep(k)==1
180               % fit line for all the points in IndexUpper and IndexLower
181               cvx_begin
182                  cvx_quiet(true);
183                  variable m(1);
184                  variable c(1);
185                  minimize(norm([-PlaneCoordUpper(2,:)' -ones(LSize,1)]*[m;c]+PlaneCoordUpper(1,:)',1));
186               cvx_end
187
188               % generating NL vector
189               NL = cross(Ns,U(:,2)+m*U(:,1));
190               NLUpper = NL./norm(NL);
191
192               % fit line for all the points in IndexUpper and IndexLower
193              cvx_begin
194                  cvx_quiet(true);
195                  variable m(1);
196                  variable c(1);
197                  minimize(norm([-PlaneCoordLower(2,:)' -ones(LSize,1)]*[m;c]+PlaneCoordLower(1,:)',1));
198               cvx_end
199
200               % generating NL vector
201               NL = cross(Ns,U(:,2)+m*U(:,1));
202               NLLower = NL./norm(NL);
203            else
204               % fit line for all the points in IndexUpper and IndexLower
205               cvx_begin
206                  cvx_quiet(true);
207                  variable m(1);
208                  variable c(1);
209                  minimize(norm([[-PlaneCoordUpper(2,:)';-PlaneCoordLower(2,:)'] -ones(LSize*2,1)]*...
210                     [m;c]+[PlaneCoordUpper(1,:)';PlaneCoordLower(1,:)'],1));
211               cvx_end
212               % generating NL vector
213               NL = cross(Ns,U(:,2)+m*U(:,1));
214               NLLower = NL./norm(NL);
215               NLUpper = NLLower;
216            end
217
218            [c,i] = setdiff(IndexUpper,MovedPatchBook);
219            if Verti == 0
220               RayPorjectImgMapY(c) = Y(i);
221            else
222               RayPorjectImgMapX(c) = X(i);
223            end
224            for l = 1:(LSize-1)
225                if any(MovedPatchBook==IndexUpper(l)) ||any(MovedPatchBook==IndexUpper(l+1))
226                   continue;
227                end
228                SelectMatrix = sparse(1,VertYNuDepth*HoriXNuDepth);
229                if Verti == 1
230                   if abs(NLUpper'*[0; 1; 0]) > abs(NLUpper'*[0; 0; 1])
231                      NLUpper = [0; 1; 0];
232                   else
233                      NLUpper = [0; 0; 1];
234                   end
235                end
236                SelectMatrix(IndexUpper(l)) = NLUpper'*permute(RayCenter(1,l,:),[3 2 1]);
237                SelectMatrix(IndexUpper(l+1)) = -NLUpper'*permute(RayCenter(1,l+1,:),[3 2 1]);
238                StraightLinePriorSelectMatrix = [StraightLinePriorSelectMatrix; SelectMatrix];
239            end
240            MovedPatchBook = [MovedPatchBook c];
241            % Lower part
242            [c,i] = setdiff(IndexLower,MovedPatchBook);
243            if Verti == 0
244               RayPorjectImgMapY(c) = Y(i);
245            else
246               RayPorjectImgMapX(c) = X(i);
247            end
248            for l = 1:(LSize-1)
249                if any(MovedPatchBook==IndexLower(l)) ||any(MovedPatchBook==IndexLower(l+1))
250                   continue;
251                end
252                SelectMatrix = sparse(1,VertYNuDepth*HoriXNuDepth);
253                if Verti == 1
254                   if abs(NLLower'*[0; 1; 0]) > abs(NLLower'*[0; 0; 1])
255                      NLLower = [0; 1; 0];
256                   else
257                      NLLower = [0; 0; 1];
258                   end
259                end
260                SelectMatrix(IndexLower(l)) = NLLower'*permute(RayCenter(1,l,:),[3 2 1]);
261                SelectMatrix(IndexLower(l+1)) = -NLLower'*permute(RayCenter(1,l+1,:),[3 2 1]);
262                StraightLinePriorSelectMatrix = [StraightLinePriorSelectMatrix; SelectMatrix];
263            end
264            MovedPatchBook = [MovedPatchBook c];
265            Count = Count +1;
266        end
267return;
Note: See TracBrowser for help on using the repository browser.